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 rest.li by linkedin.
the class TestArrayTemplate method testArray.
public static <ArrayTemplate extends AbstractArrayTemplate<E>, E> void testArray(Class<ArrayTemplate> templateClass, ArrayDataSchema schema, List<E> input, List<E> adds) {
try {
// constructors and addAll
ArrayTemplate array1 = templateClass.newInstance();
array1.addAll(input);
assertEquals(input, array1);
/*
Constructor[] constructors = templateClass.getConstructors();
for (Constructor c : constructors)
{
out.println(c);
}
*/
try {
int size = input.size();
// constructor(int capacity)
Constructor<ArrayTemplate> capacityConstructor = templateClass.getConstructor(int.class);
ArrayTemplate array = capacityConstructor.newInstance(input.size());
assertEquals(array, Collections.emptyList());
array.addAll(input);
assertEquals(input, array);
array.clear();
assertEquals(size, input.size());
// constructor(Collection<E>)
Constructor<ArrayTemplate> collectionConstructor = templateClass.getConstructor(Collection.class);
array = collectionConstructor.newInstance(input);
assertEquals(input, array);
array.clear();
assertEquals(size, input.size());
// constructor(DataList)
Constructor<ArrayTemplate> dataListConstructor = templateClass.getConstructor(DataList.class);
array = dataListConstructor.newInstance(array1.data());
assertEquals(array1, array);
assertEquals(input, array);
array.clear();
assertEquals(array1, array);
} catch (Exception e) {
assertSame(e, null);
}
// test wrapping
array1.clear();
array1.addAll(input);
DataList dataList2 = new DataList();
// with schema arg
ArrayTemplate array2 = DataTemplateUtil.wrap(dataList2, schema, templateClass);
for (E e : input) {
if (e instanceof DataTemplate) {
dataList2.add(((DataTemplate<?>) e).data());
} else if (e instanceof Enum) {
dataList2.add(e.toString());
} else {
dataList2.add(e);
}
}
assertEquals(array1, array2);
// without schema arg
ArrayTemplate array2a = DataTemplateUtil.wrap(dataList2, templateClass);
assertEquals(array1, array2a);
assertSame(array2.data(), array2a.data());
// schema()
ArrayDataSchema schema1 = array1.schema();
assertTrue(schema1 != null);
assertEquals(schema1.getType(), DataSchema.Type.ARRAY);
assertEquals(schema1, schema);
// add(E element), get(int index)
ArrayTemplate array3 = templateClass.newInstance();
for (int i = 0; i < adds.size(); ++i) {
E value = adds.get(i);
assertTrue(array3.add(value));
assertEquals(array3.get(i), value);
assertSame(array3.get(i), value);
assertTrue(array3.toString().contains(value.toString()));
}
assertEquals(array3, adds);
// add(int index, E element), get(int index)
ArrayTemplate array4 = templateClass.newInstance();
for (int i = 0; i < adds.size(); ++i) {
E value = adds.get(adds.size() - i - 1);
array4.add(0, value);
assertEquals(array4.get(0), value);
assertSame(array4.get(0), value);
}
assertEquals(array4, adds);
// clear(), isEmpty(), size()
assertEquals(array4.size(), adds.size());
assertFalse(array4.isEmpty());
array4.clear();
assertTrue(array4.isEmpty());
assertEquals(array4.size(), 0);
// equals()
array4.clear();
array4.addAll(input);
assertTrue(array4.equals(array4));
assertTrue(array4.equals(input));
assertFalse(array4.equals(null));
assertFalse(array4.equals(adds));
for (int i = 0; i <= input.size(); ++i) {
List<E> subList = input.subList(0, i);
ArrayTemplate a = templateClass.newInstance();
a.addAll(subList);
if (i == input.size()) {
assertTrue(array4.equals(subList));
assertTrue(array4.equals(a));
} else {
assertFalse(array4.equals(subList));
assertFalse(array4.equals(a));
}
}
// hashcode()
ArrayTemplate array5 = templateClass.newInstance();
array5.addAll(input);
assertEquals(array5.hashCode(), array5.data().hashCode());
array5.addAll(adds);
assertEquals(array5.hashCode(), array5.data().hashCode());
array5.clear();
int lastHash = 0;
for (int i = 0; i < input.size(); ++i) {
array5.add(input.get(i));
int newHash = array5.hashCode();
if (i > 0) {
assertFalse(newHash == lastHash);
}
lastHash = newHash;
}
// indexOf(Object o), lastIndexOf(Object o)
ArrayTemplate array6 = templateClass.newInstance();
array6.addAll(adds);
for (E e : adds) {
assertEquals(array6.indexOf(e), adds.indexOf(e));
assertEquals(array6.lastIndexOf(e), adds.lastIndexOf(e));
}
// remove(int index), subList(int fromIndex, int toIndex)
ArrayTemplate array7 = templateClass.newInstance();
array7.addAll(input);
ArrayTemplate array8 = templateClass.newInstance();
array8.addAll(input);
for (int i = 0; i < input.size(); ++i) {
array7.remove(0);
assertEquals(array7, input.subList(i + 1, input.size()));
assertEquals(array7, array8.subList(i + 1, input.size()));
}
// removeRange(int fromIndex, int toIndex), subList(int fromIndex, int toIndex)
for (int from = 0; from < input.size(); ++from) {
for (int to = from + 1; to <= input.size(); ++to) {
ArrayTemplate arrayRemove = templateClass.newInstance();
arrayRemove.addAll(input);
InternalList<E> reference = new InternalList<E>(input);
arrayRemove.removeRange(from, to);
reference.removeRange(from, to);
assertEquals(reference, arrayRemove);
}
}
// set(int index, E element)
ArrayTemplate array9 = templateClass.newInstance();
array9.addAll(input);
InternalList<E> reference9 = new InternalList<E>(input);
for (int i = 0; i < input.size() / 2; ++i) {
int k = input.size() - i - 1;
E lo = array9.get(i);
E hi = array9.get(k);
E hiPrev = array9.set(k, lo);
E loPrev = array9.set(i, hi);
E refHiPrev = reference9.set(k, lo);
E refLoPrev = reference9.set(i, hi);
assertEquals(hiPrev, refHiPrev);
assertEquals(loPrev, refLoPrev);
assertEquals(array9.get(i), reference9.get(i));
assertEquals(array9.get(k), reference9.get(k));
}
// clone and copy return types
TestDataTemplateUtil.assertCloneAndCopyReturnType(templateClass);
// clone
Exception exc = null;
ArrayTemplate array10 = templateClass.newInstance();
array10.addAll(input);
try {
@SuppressWarnings("unchecked") ArrayTemplate array10Clone = (ArrayTemplate) array10.clone();
assertTrue(array10Clone.getClass() == templateClass);
assertEquals(array10Clone, array10);
assertTrue(array10Clone != array10);
for (int i = 0; i < array10.size(); i++) {
assertSame(array10Clone.data().get(i), array10.data().get(i));
}
array10Clone.remove(0);
assertEquals(array10Clone.size(), array10.size() - 1);
assertFalse(array10Clone.equals(array10));
assertTrue(array10.containsAll(array10Clone));
array10.remove(0);
assertEquals(array10Clone, array10);
} catch (CloneNotSupportedException e) {
exc = e;
}
assert (exc == null);
// copy
ArrayTemplate array10a = templateClass.newInstance();
array10a.addAll(input);
try {
@SuppressWarnings("unchecked") ArrayTemplate array10aCopy = (ArrayTemplate) array10a.copy();
assertTrue(array10aCopy.getClass() == templateClass);
assertEquals(array10a, array10aCopy);
boolean hasComplex = false;
for (int i = 0; i < array10a.size(); i++) {
if (array10a.data().get(i) instanceof DataComplex) {
assertNotSame(array10aCopy.data().get(i), array10a.data().get(i));
hasComplex = true;
}
}
assertTrue(DataTemplate.class.isAssignableFrom(array10a._elementClass) == false || hasComplex);
assertTrue(noCommonDataComplex(array10a.data(), array10aCopy.data()));
boolean mutated = false;
for (Object items : array10aCopy.data()) {
mutated |= TestUtil.mutateChild(items);
}
assertEquals(mutated, hasComplex);
if (mutated) {
assertNotEquals(array10aCopy, array10a);
} else {
assertEquals(array10aCopy, array10a);
array10aCopy.remove(0);
assertNotEquals(array10aCopy, array10a);
}
} catch (CloneNotSupportedException e) {
exc = e;
}
assert (exc == null);
// contains
for (int i = 0; i < input.size(); ++i) {
ArrayTemplate array = templateClass.newInstance();
E v = input.get(i);
array.add(v);
for (int k = 0; k < input.size(); ++k) {
if (k == i)
assertTrue(array.contains(v));
else
assertFalse(array.contains(input.get(k)));
}
}
// containsAll
ArrayTemplate arrayContainsAll = templateClass.newInstance();
arrayContainsAll.addAll(input);
arrayContainsAll.addAll(adds);
InternalList<E> referenceContainsAll = new InternalList<E>(input);
referenceContainsAll.addAll(adds);
for (int from = 0; from < arrayContainsAll.size(); ++from) {
for (int to = from + 1; to <= arrayContainsAll.size(); ++to) {
boolean testResult = arrayContainsAll.containsAll(referenceContainsAll.subList(from, to));
boolean referenceResult = referenceContainsAll.containsAll(referenceContainsAll.subList(from, to));
assertEquals(testResult, referenceResult);
assertTrue(testResult);
assertTrue(referenceResult);
}
boolean testResult2 = arrayContainsAll.subList(from, arrayContainsAll.size()).containsAll(referenceContainsAll);
boolean referenceResult2 = referenceContainsAll.subList(from, arrayContainsAll.size()).containsAll(referenceContainsAll);
// out.println("from " + from + " test " + testResult2 + " ref " + referenceResult2);
assertEquals(testResult2, referenceResult2);
}
// removeAll
InternalList<E> referenceListRemoveAll = new InternalList<E>(input);
referenceListRemoveAll.addAll(adds);
for (int from = 0; from < referenceListRemoveAll.size(); ++from) {
for (int to = from + 1; to <= referenceListRemoveAll.size(); ++to) {
ArrayTemplate arrayRemoveAll = templateClass.newInstance();
arrayRemoveAll.addAll(referenceListRemoveAll);
InternalList<E> referenceRemoveAll = new InternalList<E>(referenceListRemoveAll);
boolean testResult = arrayRemoveAll.removeAll(referenceListRemoveAll.subList(from, to));
boolean referenceResult = referenceRemoveAll.removeAll(referenceListRemoveAll.subList(from, to));
// out.println("from " + from + " to " + to + " test " + testResult + " " + arrayRemoveAll + " ref " + referenceResult + " " + referenceRemoveAll);
assertEquals(arrayRemoveAll, referenceRemoveAll);
assertEquals(testResult, referenceResult);
assertTrue(testResult);
assertTrue(referenceResult);
}
}
// retainAll
InternalList<E> referenceListRetainAll = new InternalList<E>(input);
referenceListRetainAll.addAll(adds);
for (int from = 0; from < referenceListRetainAll.size(); ++from) {
for (int to = from + 1; to <= referenceListRetainAll.size(); ++to) {
ArrayTemplate arrayRetainAll = templateClass.newInstance();
arrayRetainAll.addAll(referenceListRetainAll);
InternalList<E> referenceRetainAll = new InternalList<E>(referenceListRetainAll);
boolean testResult = arrayRetainAll.removeAll(referenceListRetainAll.subList(from, to));
boolean referenceResult = referenceRetainAll.removeAll(referenceListRetainAll.subList(from, to));
// out.println("from " + from + " to " + to + " test " + testResult + " " + arrayRetainAll + " ref " + referenceResult + " " + referenceRetainAll);
assertEquals(arrayRetainAll, referenceRetainAll);
assertEquals(testResult, referenceResult);
assertTrue(testResult);
assertTrue(referenceResult);
}
}
// Iterator
ArrayTemplate arrayIt = templateClass.newInstance();
arrayIt.addAll(input);
arrayIt.addAll(adds);
for (Iterator<E> it = arrayIt.iterator(); it.hasNext(); ) {
it.next();
it.remove();
}
assertTrue(arrayIt.isEmpty());
// ListIterator hasNext, hasPrevious, next, previous
ArrayTemplate arrayListIt = templateClass.newInstance();
arrayListIt.addAll(input);
arrayListIt.addAll(adds);
for (int i = 0; i <= arrayListIt.size(); ++i) {
ListIterator<E> it = arrayListIt.listIterator(i);
if (i > 0) {
int save = it.nextIndex();
assertTrue(it.hasPrevious());
assertEquals(it.previous(), arrayListIt.get(i - 1));
it.next();
assertEquals(it.nextIndex(), save);
} else {
assertFalse(it.hasPrevious());
}
if (i < arrayListIt.size()) {
int save = it.previousIndex();
assertTrue(it.hasNext());
assertEquals(it.next(), arrayListIt.get(i));
it.previous();
assertEquals(it.previousIndex(), save);
} else {
assertFalse(it.hasNext());
}
assertEquals(it.nextIndex(), i);
assertEquals(it.previousIndex(), i - 1);
}
// ListIterator remove
for (ListIterator<E> it = arrayListIt.listIterator(); it.hasNext(); ) {
it.next();
it.remove();
}
assertTrue(arrayListIt.isEmpty());
// ListIterator add
arrayListIt.clear();
{
ListIterator<E> it = arrayListIt.listIterator();
for (E e : adds) {
it.add(e);
}
}
assertEquals(arrayListIt, adds);
// ListIterator set
for (int i = 0; i < adds.size(); ++i) {
ListIterator<E> it = arrayListIt.listIterator(i);
it.next();
E value = adds.get(adds.size() - i - 1);
it.set(value);
}
for (int i = 0; i < adds.size(); ++i) {
E value = adds.get(adds.size() - i - 1);
assertEquals(arrayListIt.get(i), value);
}
} catch (InstantiationException exc) {
fail("Unexpected exception", exc);
} catch (IllegalAccessException exc) {
fail("Unexpected exception", exc);
}
}
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);
}
use of java.util.ListIterator in project robovm by robovm.
the class AbstractListTest method test_listIteratorI.
public void test_listIteratorI() {
AbstractList al1 = new ArrayList();
AbstractList al2 = new ArrayList();
al1.add(0);
al1.add(1);
al1.add(2);
al1.add(3);
al1.add(4);
al2.add(2);
al2.add(3);
al2.add(4);
Iterator li1 = al1.listIterator(2);
Iterator li2 = al2.listIterator();
while (li1.hasNext() && li2.hasNext()) {
assertEquals(li1.next(), li2.next());
}
assertSame(li1.hasNext(), li2.hasNext());
try {
al1.listIterator(-1);
fail("IndexOutOfBoundsException expected");
} catch (IndexOutOfBoundsException ee) {
//expected
}
try {
al1.listIterator(al1.size() + 1);
fail("IndexOutOfBoundsException expected");
} catch (IndexOutOfBoundsException ee) {
//expected
}
}
use of java.util.ListIterator in project robovm by robovm.
the class AbstractListTest method test_subList_addAll.
/**
* java.util.AbstractList#subList(int, int)
*/
public void test_subList_addAll() {
// Regression for HARMONY-390
List mainList = new ArrayList();
Object[] mainObjects = { "a", "b", "c" };
mainList.addAll(Arrays.asList(mainObjects));
List subList = mainList.subList(1, 2);
assertFalse("subList should not contain \"a\"", subList.contains("a"));
assertFalse("subList should not contain \"c\"", subList.contains("c"));
assertTrue("subList should contain \"b\"", subList.contains("b"));
Object[] subObjects = { "one", "two", "three" };
subList.addAll(Arrays.asList(subObjects));
assertFalse("subList should not contain \"a\"", subList.contains("a"));
assertFalse("subList should not contain \"c\"", subList.contains("c"));
Object[] expected = { "b", "one", "two", "three" };
ListIterator iter = subList.listIterator();
for (int i = 0; i < expected.length; i++) {
assertTrue("subList should contain " + expected[i], subList.contains(expected[i]));
assertTrue("should be more elements", iter.hasNext());
assertEquals("element in incorrect position", expected[i], iter.next());
}
}
Aggregations