use of java.util.concurrent.ConcurrentSkipListMap in project perun by CESNET.
the class Api method serve.
@SuppressWarnings("ConstantConditions")
private void serve(HttpServletRequest req, HttpServletResponse resp, boolean isGet, boolean isPut) throws IOException {
Serializer ser = null;
String manager = "N/A";
String method = "N/A";
boolean isJsonp = false;
PerunRequest perunRequest = null;
ApiCaller caller;
String callbackName = req.getParameter("callback");
long timeStart = System.currentTimeMillis();
caller = (ApiCaller) req.getSession(true).getAttribute(APICALLER);
OutputStream out = resp.getOutputStream();
// init pending request in HTTP session
if (req.getSession().getAttribute(PERUNREQUESTS) == null) {
req.getSession().setAttribute(PERUNREQUESTS, new ConcurrentSkipListMap<String, PerunRequest>());
}
// store pending requests locally, because accessing it from session object after response is written would cause IllegalStateException
@SuppressWarnings("unchecked") ConcurrentSkipListMap<String, PerunRequest> pendingRequests = ((ConcurrentSkipListMap<String, PerunRequest>) req.getSession().getAttribute(PERUNREQUESTS));
// Check if it is request for list of pending operations.
if (req.getPathInfo().equals("/jsonp/" + PERUNREQUESTSURL)) {
// name used to identify pending request
String callbackId = req.getParameter("callbackId");
JsonSerializerJSONP serializer = new JsonSerializerJSONP(out, req, resp);
resp.setContentType(serializer.getContentType());
try {
// Create a copy of the PERUNREQUESTS and then pass it to the serializer
if (callbackId != null) {
// return single entry
serializer.write(pendingRequests.get(callbackId));
} else {
// return all pending requests
serializer.write(Arrays.asList(pendingRequests.values().toArray()));
}
} catch (RpcException e) {
serializer.writePerunRuntimeException(e);
}
out.close();
return;
}
// prepare result object
Object result = null;
PrintWriter printWriter = null;
try {
// [0] format, [1] class, [2] method
String[] fcm;
try {
if (req.getPathInfo() == null) {
throw new RpcException(RpcException.Type.NO_PATHINFO);
}
fcm = req.getPathInfo().substring(1).split("/");
if (fcm.length < 3 || fcm[0].isEmpty() || fcm[1].isEmpty() || fcm[2].isEmpty()) {
throw new RpcException(RpcException.Type.INVALID_URL, req.getPathInfo());
}
manager = fcm[1];
method = fcm[2];
ser = selectSerializer(fcm[0], manager, method, out, req, resp);
// what is the output format?
if ("jsonp".equalsIgnoreCase(fcm[0])) {
isJsonp = true;
}
if (ser instanceof PdfSerializer) {
resp.addHeader("Content-Disposition", "attachment; filename=\"output.pdf\"");
}
resp.setContentType(ser.getContentType());
} catch (RpcException rex) {
// selects the default serializer (json) before throwing the exception
ser = new JsonSerializer(out);
resp.setContentType(ser.getContentType());
throw rex;
}
// Initialize deserializer
Deserializer des;
if (isGet) {
des = new UrlDeserializer(req);
} else {
des = selectDeserializer(fcm[0], req);
}
// We have new request, so do the whole auth/authz stuff
if (caller == null) {
caller = new ApiCaller(getServletContext(), setupPerunPrincipal(req, des), setupPerunClient(req));
// Store the current session
req.getSession(true).setAttribute(APICALLER, caller);
} else if (!Objects.equals(caller.getSession().getPerunPrincipal().getExtSourceName(), getExtSourceName(req, des))) {
// If the user is coming from the URL protected by different authN mechanism, destroy and create session again
caller = new ApiCaller(getServletContext(), setupPerunPrincipal(req, des), setupPerunClient(req));
req.getSession(true).setAttribute(APICALLER, caller);
} else if (!Objects.equals(caller.getSession().getPerunPrincipal().getActor(), getActor(req, des)) && !caller.getSession().getPerunPrincipal().getExtSourceName().equals(ExtSourcesManager.EXTSOURCE_NAME_LOCAL)) {
// prevent cookie stealing (if remote user changed, rebuild session)
caller = new ApiCaller(getServletContext(), setupPerunPrincipal(req, des), setupPerunClient(req));
req.getSession(true).setAttribute(APICALLER, caller);
}
// Does user want to logout from perun?
if ("utils".equals(manager) && "logout".equals(method)) {
if (req.getSession(false) != null) {
req.getSession().removeAttribute(APICALLER);
// deletes the cookies
Cookie[] cookies = req.getCookies();
if (cookies != null) {
final String SHIBBOLETH_COOKIE_FORMAT = "^_shib.+$";
for (Cookie c : cookies) {
// if shibboleth cookie
if (c.getName().matches(SHIBBOLETH_COOKIE_FORMAT)) {
// remove it
c.setValue("0");
c.setMaxAge(0);
// add updated cookie to the response
resp.addCookie(c);
}
}
}
// Invalidate session
req.getSession().invalidate();
}
ser.write("Logout");
// closes the request
out.close();
return;
} else if ("utils".equals(manager) && "getGuiConfiguration".equals(method)) {
ser.write(BeansUtils.getAllPropertiesFromCustomConfiguration("perun-web-gui.properties"));
// closes the request
out.close();
return;
} else if ("utils".equals(manager) && "getAppsConfig".equals(method)) {
ser.write(PerunAppsConfig.getInstance());
// closes the request
out.close();
return;
} else if ("utils".equals(manager) && PERUNSTATUS.equals(method)) {
Date date = new Date();
Timestamp timestamp = new Timestamp(date.getTime());
Map<String, Integer> auditerConsumers;
// noinspection unchecked
auditerConsumers = (Map<String, Integer>) caller.call("auditMessagesManager", "getAllAuditerConsumers", des);
List<String> perunStatus = new ArrayList<>();
perunStatus.add("Version of Perun: " + getPerunRpcVersion());
perunStatus.add("Version of PerunDB: " + caller.call("databaseManager", "getCurrentDatabaseVersion", des));
perunStatus.add("Version of Servlet: " + getServletContext().getServerInfo());
perunStatus.add("Version of DB-driver: " + caller.call("databaseManager", "getDatabaseDriverInformation", des));
perunStatus.add("Version of DB: " + caller.call("databaseManager", "getDatabaseInformation", des));
perunStatus.add("Version of Java platform: " + System.getProperty("java.version"));
for (String consumerName : auditerConsumers.keySet()) {
Integer lastProcessedId = auditerConsumers.get(consumerName);
perunStatus.add("AuditerConsumer: '" + consumerName + "' with last processed id='" + lastProcessedId + "'");
}
perunStatus.add("LastMessageId: " + caller.call("auditMessagesManager", "getLastMessageId", des));
perunStatus.add("Timestamp: " + timestamp);
ser.write(perunStatus);
out.close();
return;
} else if ("utils".equals(manager) && PERUNSTATISTICS.equals(method)) {
Date date = new Date();
Timestamp timestamp = new Timestamp(date.getTime());
List<String> perunStatistics = new ArrayList<>();
perunStatistics.add("Timestamp: '" + timestamp + "'");
perunStatistics.add("USERS: '" + caller.call("usersManager", "getUsersCount", des) + "'");
perunStatistics.add("FACILITIES: '" + caller.call("facilitiesManager", "getFacilitiesCount", des) + "'");
perunStatistics.add("DESTINATIONS: '" + caller.call("servicesManager", "getDestinationsCount", des) + "'");
perunStatistics.add("VOS: '" + caller.call("vosManager", "getVosCount", des) + "'");
perunStatistics.add("RESOURCES: '" + caller.call("resourcesManager", "getResourcesCount", des) + "'");
perunStatistics.add("GROUPS: '" + caller.call("groupsManager", "getGroupsCount", des) + "'");
perunStatistics.add("AUDITMESSAGES: '" + caller.call("auditMessagesManager", "getAuditerMessagesCount", des) + "'");
ser.write(perunStatistics);
out.close();
return;
} else if ("utils".equals(manager) && PERUNSYSTEMTIME.equals(method)) {
long systemTimeInMillis = System.currentTimeMillis();
ser.write(systemTimeInMillis);
out.close();
}
// Store identification of the request only if supported by app (it passed unique callbackName)
if (callbackName != null) {
perunRequest = new PerunRequest(caller.getSession().getPerunPrincipal(), callbackName, manager, method, des.readAll());
// Add perunRequest into the queue of the requests for POST only
if (!isGet && !isPut) {
pendingRequests.put(callbackName, perunRequest);
}
}
PerunClient perunClient = caller.getSession().getPerunClient();
if (perunClient.getType() == PerunClient.Type.OAUTH) {
if (!perunClient.getScopes().contains(PerunClient.PERUN_API_SCOPE)) {
// user has not consented to scope perun_api for the client on the OAuth Authorization Server
throw new PrivilegeException("Scope " + PerunClient.PERUN_API_SCOPE + " is missing, either the client app " + perunClient.getId() + " has not asked for it, or the user has not granted it.");
}
}
// Process request and sent the response back
if (SCIMMANAGER.equals(manager)) {
// Process SCIM protocol
result = caller.getSCIMManager().process(caller.getSession(), method, des.readAll());
if (perunRequest != null)
perunRequest.setResult(result);
if (!(result instanceof Response))
throw new InternalErrorException("SCIM manager returned unexpected result: " + result);
resp.setStatus(((Response) result).getStatus());
String response = (String) ((Response) result).getEntity();
printWriter = new PrintWriter(resp.getOutputStream());
printWriter.println(response);
printWriter.flush();
} else {
// Save only exceptions from caller to result
try {
result = caller.call(manager, method, des);
if (perunRequest != null)
perunRequest.setResult(result);
} catch (Exception ex) {
result = ex;
throw ex;
}
ser.write(result);
}
} catch (PerunException pex) {
// If the output is JSONP, it cannot send the HTTP 400 code, because the web browser wouldn't accept this
if (!isJsonp) {
resp.setStatus(400);
}
log.warn("Perun exception {}: {}.", pex.getErrorId(), pex);
ser.writePerunException(pex);
} catch (PerunRuntimeException prex) {
// If the output is JSONP, it cannot send the HTTP 400 code, because the web browser wouldn't accept this
if (!isJsonp) {
resp.setStatus(400);
}
log.warn("PerunRuntime exception {}: {}.", prex.getErrorId(), prex);
ser.writePerunRuntimeException(prex);
} catch (IOException ioex) {
// IOException gets logged and is rethrown
// noinspection ThrowableNotThrown
log.warn("IO exception {}: {}.", Long.toHexString(System.currentTimeMillis()), ioex);
new RpcException(RpcException.Type.UNCATCHED_EXCEPTION, ioex);
throw ioex;
} catch (Exception ex) {
// If the output is JSONP, it cannot send the HTTP 400 code, because the web browser wouldn't accept this
if (!isJsonp) {
resp.setStatus(500);
}
log.warn("Perun exception {}: {}.", Long.toHexString(System.currentTimeMillis()), ex);
ser.writePerunRuntimeException(new RpcException(RpcException.Type.UNCATCHED_EXCEPTION, ex));
} finally {
if (!isGet && !isPut && perunRequest != null) {
// save result of this perunRequest
perunRequest.setEndTime(System.currentTimeMillis());
if (result instanceof Exception)
perunRequest.setResult(result);
perunRequest.setEndTime(System.currentTimeMillis());
}
// Check all resolved requests and remove them if they are old than timeToLiveWhenDone
Iterator<String> iterator = pendingRequests.keySet().iterator();
while (iterator.hasNext()) {
String key = iterator.next();
PerunRequest value = pendingRequests.get(key);
if (value != null) {
if (value.getEndTime() < 0)
continue;
if (System.currentTimeMillis() - value.getEndTime() > timeToLiveWhenDone) {
iterator.remove();
}
}
}
if (printWriter != null)
printWriter.close();
}
out.close();
if (Objects.equals(manager, "authzResolver") && Objects.equals(method, "keepAlive")) {
log.trace("Method {}.{} called by {} from {}, duration {} ms.", manager, method, caller.getSession().getPerunPrincipal().getActor(), caller.getSession().getPerunPrincipal().getExtSourceName(), (System.currentTimeMillis() - timeStart));
} else {
log.debug("Method {}.{} called by {} from {}, duration {} ms.", manager, method, caller.getSession().getPerunPrincipal().getActor(), caller.getSession().getPerunPrincipal().getExtSourceName(), (System.currentTimeMillis() - timeStart));
}
}
use of java.util.concurrent.ConcurrentSkipListMap in project Solbase by Photobucket.
the class IndexWriter method parseDoc.
@SuppressWarnings("unchecked")
public ParsedDoc parseDoc(Document doc, Analyzer analyzer, String indexName, int docNumber, List<String> sortFieldNames) throws CorruptIndexException, IOException {
// given doc, what are all of terms we indexed
List<Term> allIndexedTerms = new ArrayList<Term>();
Map<String, byte[]> fieldCache = new HashMap<String, byte[]>(1024);
// need to hold onto TermDocMetaData, so it can return this array
List<TermDocMetadata> metadatas = new ArrayList<TermDocMetadata>();
byte[] docId = Bytes.toBytes(docNumber);
int position = 0;
for (Fieldable field : (List<Fieldable>) doc.getFields()) {
// Indexed field
if (field.isIndexed() && field.isTokenized()) {
TokenStream tokens = field.tokenStreamValue();
if (tokens == null) {
tokens = analyzer.tokenStream(field.name(), new StringReader(field.stringValue()));
}
// collect term information per field
Map<Term, Map<ByteBuffer, List<Number>>> allTermInformation = new ConcurrentSkipListMap<Term, Map<ByteBuffer, List<Number>>>();
int lastOffset = 0;
if (position > 0) {
position += analyzer.getPositionIncrementGap(field.name());
}
// reset the TokenStream to the first token
tokens.reset();
// offsets
OffsetAttribute offsetAttribute = null;
if (field.isStoreOffsetWithTermVector())
offsetAttribute = (OffsetAttribute) tokens.addAttribute(OffsetAttribute.class);
// positions
PositionIncrementAttribute posIncrAttribute = null;
if (field.isStorePositionWithTermVector())
posIncrAttribute = (PositionIncrementAttribute) tokens.addAttribute(PositionIncrementAttribute.class);
TermAttribute termAttribute = (TermAttribute) tokens.addAttribute(TermAttribute.class);
// store normalizations of field per term per document
// rather
// than per field.
// this adds more to write but less to read on other side
Integer tokensInField = new Integer(0);
while (tokens.incrementToken()) {
tokensInField++;
Term term = new Term(field.name(), termAttribute.term());
allIndexedTerms.add(term);
// fetch all collected information for this term
Map<ByteBuffer, List<Number>> termInfo = allTermInformation.get(term);
if (termInfo == null) {
termInfo = new ConcurrentSkipListMap<ByteBuffer, List<Number>>();
allTermInformation.put(term, termInfo);
}
// term frequency
List<Number> termFrequency = termInfo.get(TermDocMetadata.termFrequencyKeyBytes);
if (termFrequency == null) {
termFrequency = new ArrayList<Number>();
termFrequency.add(new Integer(0));
termInfo.put(TermDocMetadata.termFrequencyKeyBytes, termFrequency);
}
// increment
termFrequency.set(0, termFrequency.get(0).intValue() + 1);
// position vector
if (field.isStorePositionWithTermVector()) {
position += (posIncrAttribute.getPositionIncrement() - 1);
List<Number> positionVector = termInfo.get(TermDocMetadata.positionVectorKeyBytes);
if (positionVector == null) {
positionVector = new ArrayList<Number>();
termInfo.put(TermDocMetadata.positionVectorKeyBytes, positionVector);
}
positionVector.add(++position);
}
// term offsets
if (field.isStoreOffsetWithTermVector()) {
List<Number> offsetVector = termInfo.get(TermDocMetadata.offsetVectorKeyBytes);
if (offsetVector == null) {
offsetVector = new ArrayList<Number>();
termInfo.put(TermDocMetadata.offsetVectorKeyBytes, offsetVector);
}
offsetVector.add(lastOffset + offsetAttribute.startOffset());
offsetVector.add(lastOffset + offsetAttribute.endOffset());
}
List<Number> sortValues = new ArrayList<Number>();
// init sortValues
for (int i = 0; i < Scorer.numSort; i++) {
sortValues.add(new Integer(-1));
}
int order = 0;
// extract sort field value and store it in term doc metadata obj
for (String fieldName : sortFieldNames) {
Fieldable fieldable = doc.getFieldable(fieldName);
if (fieldable instanceof EmbeddedSortField) {
EmbeddedSortField sortField = (EmbeddedSortField) fieldable;
int value = -1;
if (sortField.stringValue() != null) {
value = Integer.parseInt(sortField.stringValue());
}
int sortSlot = sortField.getSortSlot();
sortValues.set(sortSlot - 1, new Integer(value));
} else {
// TODO: this logic is used for real time indexing.
// hacky. depending on order of sort field names in array
int value = -1;
if (fieldable.stringValue() != null) {
value = Integer.parseInt(fieldable.stringValue());
}
sortValues.set(order++, new Integer(value));
}
}
termInfo.put(TermDocMetadata.sortFieldKeyBytes, sortValues);
}
List<Number> bnorm = null;
if (!field.getOmitNorms()) {
bnorm = new ArrayList<Number>();
float norm = doc.getBoost();
norm *= field.getBoost();
norm *= similarity.lengthNorm(field.name(), tokensInField);
bnorm.add(Similarity.encodeNorm(norm));
}
for (Map.Entry<Term, Map<ByteBuffer, List<Number>>> term : allTermInformation.entrySet()) {
Term tempTerm = term.getKey();
byte[] fieldTermKeyBytes = SolbaseUtil.generateTermKey(tempTerm);
// more writes but faster on read side.
if (!field.getOmitNorms()) {
term.getValue().put(TermDocMetadata.normsKeyBytes, bnorm);
}
TermDocMetadata data = new TermDocMetadata(docNumber, term.getValue(), fieldTermKeyBytes, tempTerm);
metadatas.add(data);
}
}
// Untokenized fields go in without a termPosition
if (field.isIndexed() && !field.isTokenized()) {
Term term = new Term(field.name(), field.stringValue());
allIndexedTerms.add(term);
byte[] fieldTermKeyBytes = SolbaseUtil.generateTermKey(term);
Map<ByteBuffer, List<Number>> termMap = new ConcurrentSkipListMap<ByteBuffer, List<Number>>();
termMap.put(TermDocMetadata.termFrequencyKeyBytes, Arrays.asList(new Number[] {}));
termMap.put(TermDocMetadata.positionVectorKeyBytes, Arrays.asList(new Number[] {}));
TermDocMetadata data = new TermDocMetadata(docNumber, termMap, fieldTermKeyBytes, term);
metadatas.add(data);
}
// Stores each field as a column under this doc key
if (field.isStored()) {
byte[] _value = field.isBinary() ? field.getBinaryValue() : Bytes.toBytes(field.stringValue());
// first byte flags if binary or not
byte[] value = new byte[_value.length + 1];
System.arraycopy(_value, 0, value, 0, _value.length);
value[value.length - 1] = (byte) (field.isBinary() ? Byte.MAX_VALUE : Byte.MIN_VALUE);
// logic to handle multiple fields w/ same name
byte[] currentValue = fieldCache.get(field.name());
if (currentValue == null) {
fieldCache.put(field.name(), value);
} else {
// append new data
byte[] newValue = new byte[currentValue.length + SolbaseUtil.delimiter.length + value.length - 1];
System.arraycopy(currentValue, 0, newValue, 0, currentValue.length - 1);
System.arraycopy(SolbaseUtil.delimiter, 0, newValue, currentValue.length - 1, SolbaseUtil.delimiter.length);
System.arraycopy(value, 0, newValue, currentValue.length + SolbaseUtil.delimiter.length - 1, value.length);
fieldCache.put(field.name(), newValue);
}
}
}
Put documentPut = new Put(SolbaseUtil.randomize(docNumber));
// Store each field as a column under this docId
for (Map.Entry<String, byte[]> field : fieldCache.entrySet()) {
documentPut.add(Bytes.toBytes("field"), Bytes.toBytes(field.getKey()), field.getValue());
}
// in case of real time update, we need to add back docId field
if (!documentPut.has(Bytes.toBytes("field"), Bytes.toBytes("docId"))) {
byte[] docIdStr = Bytes.toBytes(new Integer(docNumber).toString());
// first byte flags if binary or not
byte[] value = new byte[docIdStr.length + 1];
System.arraycopy(docIdStr, 0, value, 0, docIdStr.length);
value[value.length - 1] = (byte) (Byte.MIN_VALUE);
documentPut.add(Bytes.toBytes("field"), Bytes.toBytes("docId"), value);
}
// Finally, Store meta-data so we can delete this document
documentPut.add(Bytes.toBytes("allTerms"), Bytes.toBytes("allTerms"), SolbaseUtil.toBytes(allIndexedTerms).array());
ParsedDoc parsedDoc = new ParsedDoc(metadatas, doc, documentPut, fieldCache.entrySet(), allIndexedTerms);
return parsedDoc;
}
use of java.util.concurrent.ConcurrentSkipListMap in project j2objc by google.
the class ConcurrentSkipListMapTest method testContainsValue_NullPointerException.
/**
* containsValue(null) throws NPE
*/
public void testContainsValue_NullPointerException() {
ConcurrentSkipListMap c = new ConcurrentSkipListMap();
try {
c.containsValue(null);
shouldThrow();
} catch (NullPointerException success) {
}
}
use of java.util.concurrent.ConcurrentSkipListMap in project j2objc by google.
the class ConcurrentSkipListMapTest method testPollFirstEntry.
/**
* pollFirstEntry returns entries in order
*/
public void testPollFirstEntry() {
ConcurrentSkipListMap map = map5();
Map.Entry e = map.pollFirstEntry();
assertEquals(one, e.getKey());
assertEquals("A", e.getValue());
e = map.pollFirstEntry();
assertEquals(two, e.getKey());
map.put(one, "A");
e = map.pollFirstEntry();
assertEquals(one, e.getKey());
assertEquals("A", e.getValue());
e = map.pollFirstEntry();
assertEquals(three, e.getKey());
map.remove(four);
e = map.pollFirstEntry();
assertEquals(five, e.getKey());
try {
e.setValue("A");
shouldThrow();
} catch (UnsupportedOperationException success) {
}
e = map.pollFirstEntry();
assertNull(e);
}
use of java.util.concurrent.ConcurrentSkipListMap in project j2objc by google.
the class ConcurrentSkipListMapTest method testSubMapContents.
/**
* subMap returns map with keys in requested range
*/
public void testSubMapContents() {
ConcurrentSkipListMap map = map5();
NavigableMap sm = map.subMap(two, true, four, false);
assertEquals(two, sm.firstKey());
assertEquals(three, sm.lastKey());
assertEquals(2, sm.size());
assertFalse(sm.containsKey(one));
assertTrue(sm.containsKey(two));
assertTrue(sm.containsKey(three));
assertFalse(sm.containsKey(four));
assertFalse(sm.containsKey(five));
Iterator i = sm.keySet().iterator();
Object k;
k = (Integer) (i.next());
assertEquals(two, k);
k = (Integer) (i.next());
assertEquals(three, k);
assertFalse(i.hasNext());
Iterator r = sm.descendingKeySet().iterator();
k = (Integer) (r.next());
assertEquals(three, k);
k = (Integer) (r.next());
assertEquals(two, k);
assertFalse(r.hasNext());
Iterator j = sm.keySet().iterator();
j.next();
j.remove();
assertFalse(map.containsKey(two));
assertEquals(4, map.size());
assertEquals(1, sm.size());
assertEquals(three, sm.firstKey());
assertEquals(three, sm.lastKey());
assertEquals("C", sm.remove(three));
assertTrue(sm.isEmpty());
assertEquals(3, map.size());
}
Aggregations