use of java.util.ListIterator in project Openfire by igniterealtime.
the class SipSecurityManager method handleChallenge.
/**
* Uses securityAuthority to determinie a set of valid user credentials for
* the specified Response (Challenge) and appends it to the challenged
* request so that it could be retransmitted.
* <p/>
* Fredrik Wickstrom reported that dialog cseq counters are not incremented
* when resending requests. He later uncovered additional problems and
* proposed a way to fix them (his proposition was taken into account).
*
* @param challenge the 401/407 challenge response
* @param challengedTransaction the transaction established by the challenged request
* @return a transaction containing a reoriginated request with the
* necessary authorization header.
* @throws SipSecurityException
*/
public ClientTransaction handleChallenge(Response challenge, ClientTransaction challengedTransaction) throws SipSecurityException, SipException, InvalidArgumentException, ParseException {
try {
String branchID = challengedTransaction.getBranchId();
Request challengedRequest = challengedTransaction.getRequest();
Request reoriginatedRequest = (Request) challengedRequest.clone();
ListIterator authHeaders = null;
if (challenge == null || reoriginatedRequest == null)
throw new NullPointerException("A null argument was passed to handle challenge.");
if (challenge.getStatusCode() == Response.UNAUTHORIZED)
authHeaders = challenge.getHeaders(WWWAuthenticateHeader.NAME);
else if (challenge.getStatusCode() == Response.PROXY_AUTHENTICATION_REQUIRED)
authHeaders = challenge.getHeaders(ProxyAuthenticateHeader.NAME);
if (authHeaders == null)
throw new SecurityException("Could not find WWWAuthenticate or ProxyAuthenticate headers");
// Remove all authorization headers from the request (we'll re-add
// them
// from cache)
reoriginatedRequest.removeHeader(AuthorizationHeader.NAME);
reoriginatedRequest.removeHeader(ProxyAuthorizationHeader.NAME);
// rfc 3261 says that the cseq header should be augmented for the
// new
// request. do it here so that the new dialog (created together with
// the new client transaction) takes it into account.
// Bug report - Fredrik Wickstrom
CSeqHeader cSeq = (CSeqHeader) reoriginatedRequest.getHeader((CSeqHeader.NAME));
cSeq.setSequenceNumber(cSeq.getSequenceNumber() + 1);
ClientTransaction retryTran = transactionCreator.getNewClientTransaction(reoriginatedRequest);
WWWAuthenticateHeader authHeader = null;
CredentialsCacheEntry ccEntry = null;
while (authHeaders.hasNext()) {
authHeader = (WWWAuthenticateHeader) authHeaders.next();
String realm = authHeader.getRealm();
// Check whether we have cached credentials for authHeader's
// realm
// make sure that if such credentials exist they get removed.
// The
// challenge means that there's something wrong with them.
ccEntry = cachedCredentials.remove(realm);
// Try to guess user name and facilitate user
UserCredentials defaultCredentials = new UserCredentials();
FromHeader from = (FromHeader) reoriginatedRequest.getHeader(FromHeader.NAME);
URI uri = from.getAddress().getURI();
if (uri.isSipURI()) {
Log.debug("handleChallenge", SIPConfig.getAuthUserName());
String user = SIPConfig.getAuthUserName() != null ? SIPConfig.getAuthUserName() : ((SipURI) uri).getUser();
defaultCredentials.setAuthUserName(user == null ? SIPConfig.getUserName() : user);
}
boolean ccEntryHasSeenTran = false;
if (ccEntry != null)
ccEntryHasSeenTran = ccEntry.processResponse(branchID);
// get a new pass
if (// we don't have credentials for the
ccEntry == null || // specified realm
((!authHeader.isStale() && ccEntryHasSeenTran))) {
if (ccEntry == null) {
ccEntry = new CredentialsCacheEntry();
ccEntry.userCredentials = defaultCredentials;
}
// put the returned user name in the properties file
// so that it appears as a default one next time user is
// prompted for pass
SIPConfig.setUserName(ccEntry.userCredentials.getUserName());
} else // encode and send what we have
if (ccEntry != null && (!ccEntryHasSeenTran || authHeader.isStale())) {
}
// if user canceled or sth else went wrong
if (ccEntry.userCredentials == null)
throw new SecurityException("Unable to authenticate with realm " + realm);
AuthorizationHeader authorization = this.getAuthorization(reoriginatedRequest.getMethod(), reoriginatedRequest.getRequestURI().toString(), reoriginatedRequest.getContent() == null ? "" : reoriginatedRequest.getContent().toString(), authHeader, ccEntry.userCredentials);
ccEntry.processRequest(retryTran.getBranchId());
cachedCredentials.cacheEntry(realm, ccEntry);
reoriginatedRequest.addHeader(authorization);
// if there was trouble with the user - make sure we fix it
if (uri.isSipURI()) {
((SipURI) uri).setUser(ccEntry.userCredentials.getUserName());
Address add = from.getAddress();
add.setURI(uri);
from.setAddress(add);
reoriginatedRequest.setHeader(from);
if (challengedRequest.getMethod().equals(Request.REGISTER)) {
ToHeader to = (ToHeader) reoriginatedRequest.getHeader(ToHeader.NAME);
add.setURI(uri);
to.setAddress(add);
reoriginatedRequest.setHeader(to);
}
// very ugly but very necessary
sipManCallback.setCurrentlyUsedURI(uri.toString());
Log.debug("URI: " + uri.toString());
}
// if this is a register - fix to as well
}
return retryTran;
} catch (Exception e) {
Log.debug("ERRO REG: " + e.toString());
return null;
}
}
use of java.util.ListIterator in project OpenGrok by OpenGrok.
the class FileHistoryCache method storeFile.
/**
* Store history object (encoded as XML and compressed with gzip) in a file.
*
* @param history history object to store
* @param file file to store the history object into
* @param repo repository for the file
* @throws HistoryException
*/
private void storeFile(History histNew, File file, Repository repo) throws HistoryException {
File cacheFile = getCachedFile(file);
History history = histNew;
File dir = cacheFile.getParentFile();
if (!dir.isDirectory() && !dir.mkdirs()) {
throw new HistoryException("Unable to create cache directory '" + dir + "'.");
}
// Incremental update of the history for this file.
History histOld;
try {
histOld = readCache(cacheFile);
// Merge old history with the new history.
List<HistoryEntry> listOld = histOld.getHistoryEntries();
if (!listOld.isEmpty()) {
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
List<HistoryEntry> listNew = histNew.getHistoryEntries();
ListIterator li = listNew.listIterator(listNew.size());
while (li.hasPrevious()) {
listOld.add(0, (HistoryEntry) li.previous());
}
history = new History(listOld);
// to this somewhat crude solution.
if (env.isTagsEnabled() && repo.hasFileBasedTags()) {
for (HistoryEntry ent : history.getHistoryEntries()) {
ent.setTags(null);
}
repo.assignTagsInHistory(history);
}
}
} catch (IOException ex) {
// Ideally we would want to catch the case when incremental update
// is done but the cached file is not there however we do not have
// the data to do it here.
}
writeHistoryToFile(dir, history, cacheFile);
}
use of java.util.ListIterator in project mapdb by jankotek.
the class CopyOnWriteArrayListTest method testListIterator2.
/**
* listIterator only returns those elements after the given index
*/
public void testListIterator2() {
List full = populatedArray(3);
ListIterator i = full.listIterator(1);
int j;
for (j = 0; i.hasNext(); j++) assertEquals(j + 1, i.next());
assertEquals(2, j);
}
use of java.util.ListIterator in project android_frameworks_base by ParanoidAndroid.
the class RequestQueue method dump.
/**
* debug tool: prints request queue to log
*/
synchronized void dump() {
HttpLog.v("dump()");
StringBuilder dump = new StringBuilder();
int count = 0;
Iterator<Map.Entry<HttpHost, LinkedList<Request>>> iter;
if (!mPending.isEmpty()) {
iter = mPending.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<HttpHost, LinkedList<Request>> entry = iter.next();
String hostName = entry.getKey().getHostName();
StringBuilder line = new StringBuilder("p" + count++ + " " + hostName + " ");
LinkedList<Request> reqList = entry.getValue();
ListIterator reqIter = reqList.listIterator(0);
while (iter.hasNext()) {
Request request = (Request) iter.next();
line.append(request + " ");
}
dump.append(line);
dump.append("\n");
}
}
HttpLog.v(dump.toString());
}
use of java.util.ListIterator in project jodd by oblac.
the class JoddArrayListTest method testListIterator.
@Test
public void testListIterator() {
JoddArrayList<String> jal = new JoddArrayList<>();
add(jal, 0, 2);
addFirst(jal, 10, 12);
add(jal, 20, 22);
addFirst(jal, 30, 32);
StringBuilder sb = new StringBuilder();
Iterator<String> iterator = jal.listIterator(2);
while (iterator.hasNext()) {
String next = iterator.next();
sb.append(next);
}
assertEquals("<11><10><0><1><20><21>", sb.toString());
try {
iterator.next();
fail();
} catch (Exception ignore) {
}
iterator = jal.listIterator(2);
sb.setLength(0);
while (iterator.hasNext()) {
String next = iterator.next();
if (next.contains("1")) {
iterator.remove();
} else {
sb.append(next);
}
}
assertEquals("<0><20>", sb.toString());
assertEquals("[<31>,<30>,<0>,<20>]", jal.toString());
// list iterator specifics
ListIterator li = jal.listIterator(3);
assertTrue(li.hasPrevious());
assertTrue(li.hasNext());
assertEquals(3, li.nextIndex());
assertEquals(2, li.previousIndex());
li.next();
li.previous();
li.next();
assertEquals(4, li.nextIndex());
assertEquals(3, li.previousIndex());
assertFalse(li.hasNext());
// again
li = jal.listIterator();
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("<31>");
arrayList.add("<30>");
arrayList.add("<0>");
arrayList.add("<20>");
ListIterator li2 = arrayList.listIterator();
assertFalse(li.hasPrevious());
assertFalse(li2.hasPrevious());
li.next();
li2.next();
li.add("A");
li2.add("A");
li.next();
li2.next();
// removes last returned one
li.remove();
// removes last returned one
li2.remove();
assertEquals("[<31>,A,<0>,<20>]", jal.toString());
assertEquals("[<31>,A,<0>,<20>]", StringUtil.remove(arrayList.toString(), ' '));
li.previous();
li2.previous();
li.previous();
li2.previous();
li.remove();
li2.remove();
assertEquals("[A,<0>,<20>]", jal.toString());
assertEquals("[A,<0>,<20>]", StringUtil.remove(arrayList.toString(), ' '));
checkNulls(jal);
}
Aggregations