List of usage examples for javax.servlet.http HttpServletRequest getReader
public BufferedReader getReader() throws IOException;
BufferedReader
. From source file:de.tu_dortmund.ub.api.paia.auth.PaiaAuthEndpoint.java
protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException { ObjectMapper mapper = new ObjectMapper(); String format;//from ww w .j a v a 2 s. c o m String language; String redirect_url; this.logger.debug("PathInfo = " + httpServletRequest.getPathInfo()); this.logger.debug("QueryString = " + httpServletRequest.getQueryString()); String service = ""; String authorization = ""; String path = httpServletRequest.getPathInfo(); String[] params = path.substring(1, path.length()).split("/"); if (params.length == 1) { service = params[0]; } format = "html"; language = ""; // Hole 'Accept' und 'Authorization' aus dem Header; Enumeration<String> headerNames = httpServletRequest.getHeaderNames(); while (headerNames.hasMoreElements()) { String headerNameKey = (String) headerNames.nextElement(); this.logger.debug("headerNameKey = " + headerNameKey + " / headerNameValue = " + httpServletRequest.getHeader(headerNameKey)); if (headerNameKey.equals("Accept")) { this.logger.debug("headerNameKey = " + httpServletRequest.getHeader(headerNameKey)); if (httpServletRequest.getHeader(headerNameKey).contains("text/html")) { format = "html"; } else if (httpServletRequest.getHeader(headerNameKey).contains("application/xml")) { format = "xml"; } else if (httpServletRequest.getHeader(headerNameKey).contains("application/json")) { format = "json"; } } if (headerNameKey.equals("Accept-Language")) { language = httpServletRequest.getHeader(headerNameKey); this.logger.debug("Accept-Language: " + language); } if (headerNameKey.equals("Authorization")) { authorization = httpServletRequest.getHeader(headerNameKey); } } this.logger.debug("Service: " + service); if (httpServletRequest.getParameter("format") != null && !httpServletRequest.getParameter("format").equals("")) { format = httpServletRequest.getParameter("format"); } this.logger.info("format = " + format); if (format.equals("html") && Lookup.lookupAll(ObjectToHtmlTransformation.class).size() == 0) { this.logger.error(HttpServletResponse.SC_BAD_REQUEST + ": " + "html not implemented!"); // Error handling mit suppress_response_codes=true if (httpServletRequest.getParameter("suppress_response_codes") != null) { httpServletResponse.setStatus(HttpServletResponse.SC_OK); } // Error handling mit suppress_response_codes=false (=default) else { httpServletResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST); } // Json fr Response body RequestError requestError = new RequestError(); requestError.setError( this.config.getProperty("error." + Integer.toString(HttpServletResponse.SC_BAD_REQUEST))); requestError.setCode(HttpServletResponse.SC_BAD_REQUEST); requestError.setDescription(this.config .getProperty("error." + Integer.toString(HttpServletResponse.SC_BAD_REQUEST) + ".description")); requestError.setErrorUri(this.config .getProperty("error." + Integer.toString(HttpServletResponse.SC_BAD_REQUEST) + ".uri")); this.sendRequestError(httpServletResponse, requestError, format, language, ""); } else { // redirect_url redirect_url = ""; if (httpServletRequest.getParameter("redirect_url") != null && !httpServletRequest.getParameter("redirect_url").equals("")) { if (httpServletRequest.getParameter("redirect_url").contains("redirect_url=")) { String tmp[] = httpServletRequest.getParameter("redirect_url").split("redirect_url="); redirect_url = tmp[0] + "redirect_url=" + URLEncoder.encode(tmp[1], "UTF-8"); } else { redirect_url = httpServletRequest.getParameter("redirect_url"); } } this.logger.info("redirect_url = " + redirect_url); // language if (language.startsWith("de")) { language = "de"; } else if (language.startsWith("en")) { language = "en"; } else if (httpServletRequest.getParameter("l") != null) { language = httpServletRequest.getParameter("l"); } else { language = "de"; } this.logger.info("language = " + language); if (authorization.equals("") && httpServletRequest.getParameter("access_token") != null) { authorization = httpServletRequest.getParameter("access_token"); } if (authorization.equals("")) { // if exists PaiaService-Cookie: read content Cookie[] cookies = httpServletRequest.getCookies(); if (cookies != null) { for (Cookie cookie : cookies) { if (cookie.getName().equals("PaiaService")) { String value = URLDecoder.decode(cookie.getValue(), "UTF-8"); this.logger.info(value); LoginResponse loginResponse = mapper.readValue(value, LoginResponse.class); authorization = loginResponse.getAccess_token(); break; } } } } this.logger.debug("Access_token: " + authorization); StringBuffer jb = new StringBuffer(); String line = null; try { BufferedReader reader = httpServletRequest.getReader(); while ((line = reader.readLine()) != null) jb.append(line); } catch (Exception e) { /*report an error*/ } String requestBody = jb.toString(); this.logger.info(requestBody); httpServletResponse.setHeader("Access-Control-Allow-Origin", this.config.getProperty("Access-Control-Allow-Origin")); httpServletResponse.setHeader("Cache-Control", this.config.getProperty("Cache-Control")); // 2. Schritt: Service if (service.equals("login") || service.equals("logout") || service.equals("change") || service.equals("renew")) { this.provideService(httpServletRequest, httpServletResponse, service, authorization, requestBody, format, language, redirect_url); } else { this.logger.error(HttpServletResponse.SC_METHOD_NOT_ALLOWED + ": " + "POST for '" + service + "' not allowed!"); // Error handling mit suppress_response_codes=true if (httpServletRequest.getParameter("suppress_response_codes") != null) { httpServletResponse.setStatus(HttpServletResponse.SC_OK); } // Error handling mit suppress_response_codes=false (=default) else { httpServletResponse.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED); } // Json fr Response body RequestError requestError = new RequestError(); requestError.setError(this.config .getProperty("error." + Integer.toString(HttpServletResponse.SC_METHOD_NOT_ALLOWED))); requestError.setCode(HttpServletResponse.SC_METHOD_NOT_ALLOWED); requestError.setDescription(this.config.getProperty( "error." + Integer.toString(HttpServletResponse.SC_METHOD_NOT_ALLOWED) + ".description")); requestError.setErrorUri(this.config.getProperty( "error." + Integer.toString(HttpServletResponse.SC_METHOD_NOT_ALLOWED) + ".uri")); this.sendRequestError(httpServletResponse, requestError, format, language, redirect_url); } } }
From source file:com.telefonica.iot.cygnus.handlers.NGSIRestHandler.java
@Override public List<Event> getEvents(javax.servlet.http.HttpServletRequest request) throws Exception { // Set some MDC logging fields to 'N/A' for this thread // Value for the component field is inherited from main thread (CygnusApplication.java) org.apache.log4j.MDC.put(CommonConstants.LOG4J_CORR, CommonConstants.NA); org.apache.log4j.MDC.put(CommonConstants.LOG4J_TRANS, CommonConstants.NA); org.apache.log4j.MDC.put(CommonConstants.LOG4J_SVC, CommonConstants.NA); org.apache.log4j.MDC.put(CommonConstants.LOG4J_SUBSVC, CommonConstants.NA); // Result/*from w ww.java 2 s. c o m*/ ArrayList<Event> ngsiEvents = new ArrayList<>(); // Update the counters numReceivedEvents++; // Check the headers looking for not supported content type and/or invalid FIWARE service and service path Enumeration headerNames = request.getHeaderNames(); String corrId = null; String contentType = null; String service = defaultService; String servicePath = defaultServicePath; while (headerNames.hasMoreElements()) { String headerName = ((String) headerNames.nextElement()).toLowerCase(Locale.ENGLISH); String headerValue = request.getHeader(headerName); LOGGER.debug("[NGSIRestHandler] Header " + headerName + " received with value " + headerValue); switch (headerName) { case CommonConstants.HEADER_CORRELATOR_ID: corrId = headerValue; break; case CommonConstants.HTTP_HEADER_CONTENT_TYPE: if (wrongContentType(headerValue)) { LOGGER.warn("[NGSIRestHandler] Bad HTTP notification (" + headerValue + " content type not supported)"); throw new HTTPBadRequestException(headerValue + " content type not supported"); } else { contentType = headerValue; } // if else break; case CommonConstants.HEADER_FIWARE_SERVICE: if (wrongServiceHeaderLength(headerValue)) { LOGGER.warn("[NGSIRestHandler] Bad HTTP notification ('" + CommonConstants.HEADER_FIWARE_SERVICE + "' header length greater than " + NGSIConstants.SERVICE_HEADER_MAX_LEN + ")"); throw new HTTPBadRequestException("'" + CommonConstants.HEADER_FIWARE_SERVICE + "' header length greater than " + NGSIConstants.SERVICE_HEADER_MAX_LEN + ")"); } else { service = headerValue; } // if else break; case CommonConstants.HEADER_FIWARE_SERVICE_PATH: String[] splitValues = headerValue.split(","); for (String splitValue : splitValues) { if (wrongServicePathHeaderLength(splitValue)) { LOGGER.warn("[NGSIRestHandler] Bad HTTP notification ('" + CommonConstants.HEADER_FIWARE_SERVICE_PATH + "' header value length greater than " + NGSIConstants.SERVICE_PATH_HEADER_MAX_LEN + ")"); throw new HTTPBadRequestException("'fiware-servicePath' header length greater than " + NGSIConstants.SERVICE_PATH_HEADER_MAX_LEN + ")"); } else if (wrongServicePathHeaderInitialCharacter(splitValue)) { LOGGER.warn("[NGSIRestHandler] Bad HTTP notification ('" + CommonConstants.HEADER_FIWARE_SERVICE_PATH + "' header value must start with '/'"); throw new HTTPBadRequestException("'" + CommonConstants.HEADER_FIWARE_SERVICE_PATH + "' header value must start with '/'"); } // if else } // for servicePath = headerValue; break; default: LOGGER.debug("[NGSIRestHandler] Unnecessary header"); } // switch } // while // Get a service and servicePath and store it in the log4j Mapped Diagnostic Context (MDC) MDC.put(CommonConstants.LOG4J_SVC, service == null ? defaultService : service); MDC.put(CommonConstants.LOG4J_SUBSVC, servicePath == null ? defaultServicePath : servicePath); // If the configuration is invalid, nothing has to be done but to return null if (invalidConfiguration) { serviceMetrics.add(service, servicePath, 1, request.getContentLength(), 0, 0, 0, 0, 0, 0, 0); LOGGER.debug("[NGSIRestHandler] Invalid configuration, thus returning an empty list of Flume events"); return new ArrayList<>(); } // if // Check the method String method = request.getMethod().toUpperCase(Locale.ENGLISH); if (!method.equals("POST")) { serviceMetrics.add(service, servicePath, 1, request.getContentLength(), 0, 1, 0, 0, 0, 0, 0); LOGGER.warn("[NGSIRestHandler] Bad HTTP notification (" + method + " method not supported)"); // It would be more precise to use 405 Method Not Allowed (along with the explanatory "Allow" header // in the response. However, we are limited to the ones provided by Flume // (see https://flume.apache.org/releases/content/1.9.0/apidocs/org/apache/flume/FlumeException.html) // so we HTTPBadRequestException for 400 Bad Request instead throw new HTTPBadRequestException(method + " method not supported"); } // if // Check the notificationTarget String target = request.getRequestURI(); if (!target.equals(notificationTarget)) { serviceMetrics.add(service, servicePath, 1, request.getContentLength(), 0, 1, 0, 0, 0, 0, 0); LOGGER.warn("[NGSIRestHandler] Bad HTTP notification (" + target + " target not supported)"); throw new HTTPBadRequestException(target + " target not supported"); } // if // Check if received content type is null if (contentType == null) { serviceMetrics.add(service, servicePath, 1, request.getContentLength(), 0, 1, 0, 0, 0, 0, 0); LOGGER.warn("[NGSIRestHandler] Missing content type. Required 'application/json; charset=utf-8'"); throw new HTTPBadRequestException("Missing content type. Required 'application/json; charset=utf-8'"); } // if // Get an internal transaction ID. String transId = CommonUtils.generateUniqueId(null, null); // Get also a correlator ID if not sent in the notification. Id correlator ID is not notified // then correlator ID and transaction ID must have the same value. corrId = CommonUtils.generateUniqueId(corrId, transId); // Store both of them in the log4j Mapped Diagnostic Context (MDC), this way it will be accessible // by the whole source code. MDC.put(CommonConstants.LOG4J_CORR, corrId); MDC.put(CommonConstants.LOG4J_TRANS, transId); LOGGER.info("[NGSIRestHandler] Starting internal transaction (" + transId + ")"); // Get the data content String data = ""; String line; try (BufferedReader reader = request.getReader()) { while ((line = reader.readLine()) != null) { data += line; } // while } // try if (data.length() == 0) { serviceMetrics.add(service, servicePath, 1, request.getContentLength(), 0, 1, 0, 0, 0, 0, 0); LOGGER.warn("[NGSIRestHandler] Bad HTTP notification (No content in the request)"); throw new HTTPBadRequestException("No content in the request"); } // if LOGGER.info("[NGSIRestHandler] Received data (" + data + ")"); // Parse the original data into a NotifyContextRequest object NotifyContextRequest ncr; Gson gson = new Gson(); try { ncr = gson.fromJson(data, NotifyContextRequest.class); LOGGER.debug("[NGSIRestHandler] Parsed NotifyContextRequest: " + ncr.toString()); } catch (JsonSyntaxException e) { serviceMetrics.add(service, servicePath, 1, request.getContentLength(), 0, 1, 0, 0, 0, 0, 0); LOGGER.error("[NGSIRestHandler] Runtime error (" + e.getMessage() + ")"); return null; } // try catch // Split the notified service path and check if it matches the number of notified context responses String[] servicePaths = servicePath.split(","); if (servicePaths.length != ncr.getContextResponses().size()) { serviceMetrics.add(service, servicePath, 1, request.getContentLength(), 0, 1, 0, 0, 0, 0, 0); LOGGER.warn("[NGSIRestHandler] Bad HTTP notification ('" + CommonConstants.HEADER_FIWARE_SERVICE_PATH + "' header value does not match the number of notified context responses"); throw new HTTPBadRequestException("'" + CommonConstants.HEADER_FIWARE_SERVICE_PATH + "' header value does not match the number of notified context responses"); } // if // Iterate on the NotifyContextRequest object in order to create an event per ContextElement String ids = ""; for (int i = 0; i < ncr.getContextResponses().size(); i++) { ContextElementResponse cer = ncr.getContextResponses().get(i); LOGGER.debug("[NGSIRestHandler] NGSI event created for ContextElementResponse: " + cer.toString()); // Create the appropiate headers Map<String, String> headers = new HashMap<>(); headers.put(CommonConstants.HEADER_FIWARE_SERVICE, service); LOGGER.debug("[NGSIRestHandler] Header added to NGSI event (" + CommonConstants.HEADER_FIWARE_SERVICE + ": " + service + ")"); headers.put(CommonConstants.HEADER_FIWARE_SERVICE_PATH, servicePaths[i]); LOGGER.debug("[NGSIRestHandler] Header added to NGSI event (" + CommonConstants.HEADER_FIWARE_SERVICE_PATH + ": " + servicePaths[i] + ")"); headers.put(CommonConstants.HEADER_CORRELATOR_ID, corrId); LOGGER.debug("[NGSIRestHandler] Header added to NGSI event (" + CommonConstants.HEADER_CORRELATOR_ID + ": " + corrId + ")"); headers.put(NGSIConstants.FLUME_HEADER_TRANSACTION_ID, transId); LOGGER.debug("[NGSIRestHandler] Header added to NGSI event (" + NGSIConstants.FLUME_HEADER_TRANSACTION_ID + ": " + transId + ")"); // Create the NGSI event and add it to the list NGSIEvent ngsiEvent = new NGSIEvent( // Headers headers, // Bytes version of the notified ContextElement (cer.getContextElement().toString() + CommonConstants.CONCATENATOR).getBytes(), // Object version of the notified ContextElement cer.getContextElement(), // Will be set with the mapped object version of the notified ContextElement, by // NGSINameMappingsInterceptor (if configured). Currently, null null); ngsiEvents.add(ngsiEvent); if (ids.isEmpty()) { ids += ngsiEvent.hashCode(); } else { ids += "," + ngsiEvent.hashCode(); } // if else } // for // Return the NGSIEvent list serviceMetrics.add(service, servicePath, 1, request.getContentLength(), 0, 0, 0, 0, 0, 0, 0); LOGGER.debug("[NGSIRestHandler] NGSI events put in the channel, ids=" + ids); numProcessedEvents++; return ngsiEvents; }
From source file:io.warp10.continuum.egress.EgressFetchHandler.java
@Override public void handle(String target, Request baseRequest, HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { boolean fromArchive = false; boolean splitFetch = false; boolean writeTimestamp = false; if (Constants.API_ENDPOINT_FETCH.equals(target)) { baseRequest.setHandled(true);/* www .j av a2 s .co m*/ fromArchive = false; } else if (Constants.API_ENDPOINT_AFETCH.equals(target)) { baseRequest.setHandled(true); fromArchive = true; } else if (Constants.API_ENDPOINT_SFETCH.equals(target)) { baseRequest.setHandled(true); splitFetch = true; } else if (Constants.API_ENDPOINT_CHECK.equals(target)) { baseRequest.setHandled(true); resp.setStatus(HttpServletResponse.SC_OK); return; } else { return; } try { // Labels for Sensision Map<String, String> labels = new HashMap<String, String>(); labels.put(SensisionConstants.SENSISION_LABEL_TYPE, target); // // Add CORS header // resp.setHeader("Access-Control-Allow-Origin", "*"); String start = null; String stop = null; long now = Long.MIN_VALUE; long timespan = 0L; String nowParam = null; String timespanParam = null; String dedupParam = null; String showErrorsParam = null; if (splitFetch) { nowParam = req.getHeader(Constants.getHeader(Configuration.HTTP_HEADER_NOW_HEADERX)); timespanParam = req.getHeader(Constants.getHeader(Configuration.HTTP_HEADER_TIMESPAN_HEADERX)); showErrorsParam = req.getHeader(Constants.getHeader(Configuration.HTTP_HEADER_SHOW_ERRORS_HEADERX)); } else { start = req.getParameter(Constants.HTTP_PARAM_START); stop = req.getParameter(Constants.HTTP_PARAM_STOP); nowParam = req.getParameter(Constants.HTTP_PARAM_NOW); timespanParam = req.getParameter(Constants.HTTP_PARAM_TIMESPAN); dedupParam = req.getParameter(Constants.HTTP_PARAM_DEDUP); showErrorsParam = req.getParameter(Constants.HTTP_PARAM_SHOW_ERRORS); } String maxDecoderLenParam = req.getParameter(Constants.HTTP_PARAM_MAXSIZE); int maxDecoderLen = null != maxDecoderLenParam ? Integer.parseInt(maxDecoderLenParam) : Constants.DEFAULT_PACKED_MAXSIZE; String suffix = req.getParameter(Constants.HTTP_PARAM_SUFFIX); if (null == suffix) { suffix = Constants.DEFAULT_PACKED_CLASS_SUFFIX; } boolean unpack = null != req.getParameter(Constants.HTTP_PARAM_UNPACK); long chunksize = Long.MAX_VALUE; if (null != req.getParameter(Constants.HTTP_PARAM_CHUNKSIZE)) { chunksize = Long.parseLong(req.getParameter(Constants.HTTP_PARAM_CHUNKSIZE)); } if (chunksize <= 0) { throw new IOException("Invalid chunksize."); } boolean showErrors = null != showErrorsParam; boolean dedup = null != dedupParam && "true".equals(dedupParam); if (null != start && null != stop) { long tsstart = fmt.parseDateTime(start).getMillis() * Constants.TIME_UNITS_PER_MS; long tsstop = fmt.parseDateTime(stop).getMillis() * Constants.TIME_UNITS_PER_MS; if (tsstart < tsstop) { now = tsstop; timespan = tsstop - tsstart; } else { now = tsstart; timespan = tsstart - tsstop; } } else if (null != nowParam && null != timespanParam) { if ("now".equals(nowParam)) { now = TimeSource.getTime(); } else { try { now = Long.parseLong(nowParam); } catch (Exception e) { now = fmt.parseDateTime(nowParam).getMillis() * Constants.TIME_UNITS_PER_MS; } } timespan = Long.parseLong(timespanParam); } if (Long.MIN_VALUE == now) { resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Missing now/timespan or start/stop parameters."); return; } String selector = splitFetch ? null : req.getParameter(Constants.HTTP_PARAM_SELECTOR); // // Extract token from header // String token = req.getHeader(Constants.getHeader(Configuration.HTTP_HEADER_TOKENX)); // If token was not found in header, extract it from the 'token' parameter if (null == token && !splitFetch) { token = req.getParameter(Constants.HTTP_PARAM_TOKEN); } String fetchSig = req.getHeader(Constants.getHeader(Configuration.HTTP_HEADER_FETCH_SIGNATURE)); // // Check token signature if it was provided // boolean signed = false; if (splitFetch) { // Force showErrors showErrors = true; signed = true; } if (null != fetchSig) { if (null != fetchPSK) { String[] subelts = fetchSig.split(":"); if (2 != subelts.length) { throw new IOException("Invalid fetch signature."); } long nowts = System.currentTimeMillis(); long sigts = new BigInteger(subelts[0], 16).longValue(); long sighash = new BigInteger(subelts[1], 16).longValue(); if (nowts - sigts > 10000L) { throw new IOException("Fetch signature has expired."); } // Recompute hash of ts:token String tstoken = Long.toString(sigts) + ":" + token; long checkedhash = SipHashInline.hash24(fetchPSK, tstoken.getBytes(Charsets.ISO_8859_1)); if (checkedhash != sighash) { throw new IOException("Corrupted fetch signature"); } signed = true; } else { throw new IOException("Fetch PreSharedKey is not set."); } } ReadToken rtoken = null; String format = splitFetch ? "wrapper" : req.getParameter(Constants.HTTP_PARAM_FORMAT); if (!splitFetch) { try { rtoken = Tokens.extractReadToken(token); if (rtoken.getHooksSize() > 0) { throw new IOException("Tokens with hooks cannot be used for fetching data."); } } catch (WarpScriptException ee) { throw new IOException(ee); } if (null == rtoken) { resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Missing token."); return; } } boolean showAttr = "true".equals(req.getParameter(Constants.HTTP_PARAM_SHOWATTR)); boolean sortMeta = "true".equals(req.getParameter(Constants.HTTP_PARAM_SORTMETA)); // // Extract the class and labels selectors // The class selector and label selectors are supposed to have // values which use percent encoding, i.e. explicit percent encoding which // might have been re-encoded using percent encoding when passed as parameter // // Set<Metadata> metadatas = new HashSet<Metadata>(); List<Iterator<Metadata>> iterators = new ArrayList<Iterator<Metadata>>(); if (!splitFetch) { if (null == selector) { throw new IOException("Missing '" + Constants.HTTP_PARAM_SELECTOR + "' parameter."); } String[] selectors = selector.split("\\s+"); for (String sel : selectors) { Matcher m = SELECTOR_RE.matcher(sel); if (!m.matches()) { resp.sendError(HttpServletResponse.SC_BAD_REQUEST); return; } String classSelector = URLDecoder.decode(m.group(1), "UTF-8"); String labelsSelection = m.group(2); Map<String, String> labelsSelectors; try { labelsSelectors = GTSHelper.parseLabelsSelectors(labelsSelection); } catch (ParseException pe) { throw new IOException(pe); } // // Force 'producer'/'owner'/'app' from token // labelsSelectors.remove(Constants.PRODUCER_LABEL); labelsSelectors.remove(Constants.OWNER_LABEL); labelsSelectors.remove(Constants.APPLICATION_LABEL); labelsSelectors.putAll(Tokens.labelSelectorsFromReadToken(rtoken)); List<Metadata> metas = null; List<String> clsSels = new ArrayList<String>(); List<Map<String, String>> lblsSels = new ArrayList<Map<String, String>>(); clsSels.add(classSelector); lblsSels.add(labelsSelectors); try { metas = directoryClient.find(clsSels, lblsSels); metadatas.addAll(metas); } catch (Exception e) { // // If metadatas is not empty, create an iterator for it, then clear it // if (!metadatas.isEmpty()) { iterators.add(metadatas.iterator()); metadatas.clear(); } iterators.add(directoryClient.iterator(clsSels, lblsSels)); } } } else { // // Add an iterator which reads splits from the request body // boolean gzipped = false; if (null != req.getHeader("Content-Type") && "application/gzip".equals(req.getHeader("Content-Type"))) { gzipped = true; } BufferedReader br = null; if (gzipped) { GZIPInputStream is = new GZIPInputStream(req.getInputStream()); br = new BufferedReader(new InputStreamReader(is)); } else { br = req.getReader(); } final BufferedReader fbr = br; MetadataIterator iterator = new MetadataIterator() { private List<Metadata> metadatas = new ArrayList<Metadata>(); private boolean done = false; private String lasttoken = ""; @Override public void close() throws Exception { fbr.close(); } @Override public Metadata next() { if (!metadatas.isEmpty()) { Metadata meta = metadatas.get(metadatas.size() - 1); metadatas.remove(metadatas.size() - 1); return meta; } else { if (hasNext()) { return next(); } else { throw new NoSuchElementException(); } } } @Override public boolean hasNext() { if (!metadatas.isEmpty()) { return true; } if (done) { return false; } String line = null; try { line = fbr.readLine(); } catch (IOException ioe) { throw new RuntimeException(ioe); } if (null == line) { done = true; return false; } // // Decode/Unwrap/Deserialize the split // byte[] data = OrderPreservingBase64.decode(line.getBytes(Charsets.US_ASCII)); if (null != fetchAES) { data = CryptoUtils.unwrap(fetchAES, data); } if (null == data) { throw new RuntimeException("Invalid wrapped content."); } TDeserializer deserializer = new TDeserializer(new TCompactProtocol.Factory()); GTSSplit split = new GTSSplit(); try { deserializer.deserialize(split, data); } catch (TException te) { throw new RuntimeException(te); } // // Check the expiry // long instant = System.currentTimeMillis(); if (instant - split.getTimestamp() > maxSplitAge || instant > split.getExpiry()) { throw new RuntimeException("Split has expired."); } this.metadatas.addAll(split.getMetadatas()); // We assume there was at least one metadata instance in the split!!! return true; } }; iterators.add(iterator); } List<Metadata> metas = new ArrayList<Metadata>(); metas.addAll(metadatas); if (!metas.isEmpty()) { iterators.add(metas.iterator()); } // // Loop over the iterators, storing the read metadata to a temporary file encrypted on disk // Data is encrypted using a onetime pad // final byte[] onetimepad = new byte[(int) Math.min(65537, System.currentTimeMillis() % 100000)]; new Random().nextBytes(onetimepad); final File cache = File.createTempFile( Long.toHexString(System.currentTimeMillis()) + "-" + Long.toHexString(System.nanoTime()), ".dircache"); cache.deleteOnExit(); FileWriter writer = new FileWriter(cache); TSerializer serializer = new TSerializer(new TCompactProtocol.Factory()); int padidx = 0; for (Iterator<Metadata> itermeta : iterators) { try { while (itermeta.hasNext()) { Metadata metadata = itermeta.next(); try { byte[] bytes = serializer.serialize(metadata); // Apply onetimepad for (int i = 0; i < bytes.length; i++) { bytes[i] = (byte) (bytes[i] ^ onetimepad[padidx++]); if (padidx >= onetimepad.length) { padidx = 0; } } OrderPreservingBase64.encodeToWriter(bytes, writer); writer.write('\n'); } catch (TException te) { } } if (!itermeta.hasNext() && (itermeta instanceof MetadataIterator)) { try { ((MetadataIterator) itermeta).close(); } catch (Exception e) { } } } catch (Throwable t) { throw t; } finally { if (itermeta instanceof MetadataIterator) { try { ((MetadataIterator) itermeta).close(); } catch (Exception e) { } } } } writer.close(); // // Create an iterator based on the cache // MetadataIterator cacheiterator = new MetadataIterator() { BufferedReader reader = new BufferedReader(new FileReader(cache)); private Metadata current = null; private boolean done = false; private TDeserializer deserializer = new TDeserializer(new TCompactProtocol.Factory()); int padidx = 0; @Override public boolean hasNext() { if (done) { return false; } if (null != current) { return true; } try { String line = reader.readLine(); if (null == line) { done = true; return false; } byte[] raw = OrderPreservingBase64.decode(line.getBytes(Charsets.US_ASCII)); // Apply one time pad for (int i = 0; i < raw.length; i++) { raw[i] = (byte) (raw[i] ^ onetimepad[padidx++]); if (padidx >= onetimepad.length) { padidx = 0; } } Metadata metadata = new Metadata(); try { deserializer.deserialize(metadata, raw); this.current = metadata; return true; } catch (TException te) { LOG.error("", te); } } catch (IOException ioe) { LOG.error("", ioe); } return false; } @Override public Metadata next() { if (null != this.current) { Metadata metadata = this.current; this.current = null; return metadata; } else { throw new NoSuchElementException(); } } @Override public void close() throws Exception { this.reader.close(); cache.delete(); } }; iterators.clear(); iterators.add(cacheiterator); metas = new ArrayList<Metadata>(); PrintWriter pw = resp.getWriter(); AtomicReference<Metadata> lastMeta = new AtomicReference<Metadata>(null); AtomicLong lastCount = new AtomicLong(0L); long fetchtimespan = timespan; for (Iterator<Metadata> itermeta : iterators) { while (itermeta.hasNext()) { metas.add(itermeta.next()); // // Access the data store every 'FETCH_BATCHSIZE' GTS or at the end of each iterator // if (metas.size() > FETCH_BATCHSIZE || !itermeta.hasNext()) { try (GTSDecoderIterator iterrsc = storeClient.fetch(rtoken, metas, now, fetchtimespan, fromArchive, writeTimestamp)) { GTSDecoderIterator iter = iterrsc; if (unpack) { iter = new UnpackingGTSDecoderIterator(iter, suffix); timespan = Long.MIN_VALUE + 1; } if ("text".equals(format)) { textDump(pw, iter, now, timespan, false, dedup, signed, showAttr, lastMeta, lastCount, sortMeta); } else if ("fulltext".equals(format)) { textDump(pw, iter, now, timespan, true, dedup, signed, showAttr, lastMeta, lastCount, sortMeta); } else if ("raw".equals(format)) { rawDump(pw, iter, dedup, signed, timespan, lastMeta, lastCount, sortMeta); } else if ("wrapper".equals(format)) { wrapperDump(pw, iter, dedup, signed, fetchPSK, timespan, lastMeta, lastCount); } else if ("json".equals(format)) { jsonDump(pw, iter, now, timespan, dedup, signed, lastMeta, lastCount); } else if ("tsv".equals(format)) { tsvDump(pw, iter, now, timespan, false, dedup, signed, lastMeta, lastCount, sortMeta); } else if ("fulltsv".equals(format)) { tsvDump(pw, iter, now, timespan, true, dedup, signed, lastMeta, lastCount, sortMeta); } else if ("pack".equals(format)) { packedDump(pw, iter, now, timespan, dedup, signed, lastMeta, lastCount, maxDecoderLen, suffix, chunksize, sortMeta); } else if ("null".equals(format)) { nullDump(iter); } else { textDump(pw, iter, now, timespan, false, dedup, signed, showAttr, lastMeta, lastCount, sortMeta); } } catch (Throwable t) { LOG.error("", t); Sensision.update(SensisionConstants.CLASS_WARP_FETCH_ERRORS, Sensision.EMPTY_LABELS, 1); if (showErrors) { pw.println(); StringWriter sw = new StringWriter(); PrintWriter pw2 = new PrintWriter(sw); t.printStackTrace(pw2); pw2.close(); sw.flush(); String error = URLEncoder.encode(sw.toString(), "UTF-8"); pw.println(Constants.EGRESS_FETCH_ERROR_PREFIX + error); } throw new IOException(t); } finally { if (!itermeta.hasNext() && (itermeta instanceof MetadataIterator)) { try { ((MetadataIterator) itermeta).close(); } catch (Exception e) { } } } // // Reset 'metas' // metas.clear(); } } if (!itermeta.hasNext() && (itermeta instanceof MetadataIterator)) { try { ((MetadataIterator) itermeta).close(); } catch (Exception e) { } } } Sensision.update(SensisionConstants.SENSISION_CLASS_CONTINUUM_FETCH_REQUESTS, labels, 1); } catch (Exception e) { if (!resp.isCommitted()) { resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage()); return; } } }
From source file:com.inverse2.ajaxtoaster.AjaxToasterServlet.java
/** * Processes requests from the client for both HTTP <code>GET</code> * and <code>POST</code> methods. * * @param request servlet request/*from w w w.j a v a 2s . c om*/ * @param response servlet response */ protected void processRequest(String requestType, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String responseFormat = response_format_prop; // flags that the user has not set the response format boolean defaultResponseFormat = response_format_prop.equals("XML") ? true : false; ServiceOperationInterface service = null; String callbackFunction = null; log.info(">> Start processRequest(" + requestType + ") at " + new Date()); try { ServletContext context = getServletContext(); String scriptName = request.getParameter(PARAM_SCRIPTNAME1); // look for "service=xxxx" String contextPath = ""; /* If the service parameter is not specified then use the URL to get the service name... */ if (scriptName == null) { scriptName = request.getPathInfo(); contextPath = request.getContextPath(); /* //Put this in for debugging... System.out.println("****** -> pathInfo [" + request.getPathInfo() + "]"); System.out.println("****** -> pathTranslated [" + request.getPathTranslated() + "]"); System.out.println("****** -> contextPath [" + request.getContextPath() + "]"); System.out.println("****** -> localAddr [" + request.getLocalAddr() + "]"); System.out.println("****** -> localName [" + request.getLocalName() + "]"); System.out.println("****** -> requestURI [" + request.getRequestURI() + "]");//***** System.out.println("****** -> servletPath [" + request.getServletPath() + "]"); */ if (scriptName == null) { scriptName = "UNSPECIFIED_SERVICE"; } } /* See if the URI is mapped to another service... */ ServiceMapping serviceMapping; serviceMapping = serviceMapper.getURIMapping(""/*contextPath*/, scriptName, requestType); if (serviceMapping != null) { log.info("Redirect URI to [" + serviceMapping.getServiceName() + "]"); scriptName = serviceMapping.getServiceName(); /* If the URI has been mapped then see if the "Accept" header specifies the return type required... */ String accept = request.getHeader("Accept"); if (accept.indexOf("text/xml") != -1) { responseFormat = "XML"; defaultResponseFormat = false; } if (accept.indexOf("text/json") != -1) { responseFormat = "JSON"; defaultResponseFormat = false; } } if (scriptName.startsWith("/")) { scriptName = scriptName.substring(1, scriptName.length()); } /** * If "log" service invoked then process it... */ if (scriptName.equals("log")) { returnHTMLLog(response); return; } /** * If "health" service invoked then process it... */ if (scriptName.equals("health")) { returnHealth(response); return; } /* Check for the flag to return XML or JSON objects... */ if (request.getParameter(PARAM_RETURNXML) != null) { println(">> Servlet will return XML object."); responseFormat = "XML"; defaultResponseFormat = false; } else if (request.getParameter(PARAM_RETURNJSON) != null) { println(">> Servlet will return XML object."); responseFormat = "JSON"; defaultResponseFormat = false; } else if (request.getParameter(PARAM_RETURNRAW) != null) { println(">> Servlet will return raw text object."); responseFormat = "RAW"; defaultResponseFormat = false; } /* Check for the callback function parameter... */ callbackFunction = request.getParameter(PARAM_CALLBACK); /** * Check to see if the client wants a "Service Mapping Description" (SMD) for the 'service'... */ if (request.getParameter(PARAM_SMD) != null) { log.info("Client wants SMD for [" + scriptName + "]"); try { ServicePool pool = null; Map availableServices = null; ServiceMappingDescription smd = null; ServiceScriptPool serviceScriptPool = null; String serviceScriptName = null; String returnString = null; pool = (ServicePool) context.getAttribute(ATTRIB_SERVICE_POOL); availableServices = pool.getAvailableServices(); smd = new ServiceMappingDescription(request.getRequestURL().toString(), request.getRequestURL().toString() + "?smd", null); for (Iterator it = availableServices.values().iterator(); it.hasNext();) { serviceScriptPool = (ServiceScriptPool) it.next(); serviceScriptName = serviceScriptPool.getPoolName(); /** * If the service script name begins with the passed in script name then add it to the * service mapping description... */ log.debug("scriptName = [" + scriptName + "], serviceScriptName = [" + serviceScriptName + "]"); if (scriptName.equals("") || serviceScriptName.startsWith(scriptName + "/") || serviceScriptName.equals(scriptName)) { smd.addOperation(serviceScriptName); service = serviceScriptPool.getService(); smd.setOperationDescription(service.getScriptDescription()); smd.setOperationTransport(service.getHTTPMethods()); smd.setOperationEnvelope("URL"); smd.setOperationContentType(service.getResponseFormat()); smd.setOperationParameters(serviceScriptPool.getServiceParameters()); smd.setOperationReturns(serviceScriptPool.getServiceReturns()); } } returnString = smd.getSMDJSONString(); writeResponse(returnString, "JSONRAW", callbackFunction, response); } catch (Exception ex) { log.error("Exception getting SMD: " + ex.toString()); ex.printStackTrace(); } return; } /** * Get the service and run it... */ println(">> Client wants to invoke the service [" + scriptName + "]"); try { service = getServiceScript(scriptName); } catch (Exception ex) { errorResponse(response, "Could not get an instance of the service [" + scriptName + "]: " + ex.toString(), responseFormat, callbackFunction); return; } if (service == null) { errorResponse(response, "Service [" + scriptName + "] not found.", responseFormat, callbackFunction); return; } /** * If the script exists in the toaster pool then invoke it */ println(">> Checking login required"); try { if (service.getLoginRequired().equals("true")) { HttpSession session = request.getSession(false); Object loggedIn = null; if (session != null) { loggedIn = session.getAttribute(ATTRIB_LOGGED_IN); } log.trace("**** SESSION = " + session); log.trace("**** Logged In = " + loggedIn); if (session == null || loggedIn == null || loggedIn.equals("true") == false) { errorResponse(response, "The service " + scriptName + " requires you to be logged in to run it.", responseFormat, callbackFunction); freeServiceScript(service); return; } /* Check that the logged in user is authorised to run the service... */ String validUsers; String[] validUsersArray; String user; String loggedInUser; boolean validUser; validUsers = service.getValidUsers(); validUsersArray = validUsers.split("[,]"); loggedInUser = (String) session.getAttribute(ATTRIB_LOGGED_IN_USER); validUser = false; for (int idx = 0; idx < validUsersArray.length; idx++) { user = validUsersArray[idx].trim(); if (user.equals("*")) { validUser = true; break; } if (user.equals(loggedInUser)) { validUser = true; break; } } if (validUser == false) { log.error("The user [" + loggedInUser + "] is not authorised to invoke the service [" + scriptName + "]"); errorResponse(response, "You are not authorised to invoke the service [" + scriptName + "]", responseFormat, callbackFunction); freeServiceScript(service); return; } } } catch (Exception ex) { errorResponse(response, "Could not check if login required for this service. " + ex.toString(), responseFormat, callbackFunction); return; } boolean scriptInputSet = false; /* * Go through the set of parameters passed to us and set them up in the service instance... */ for (Enumeration e = request.getParameterNames(); e.hasMoreElements();) { String parameterName = (String) e.nextElement(); if (parameterName.equals(PARAM_SCRIPTNAME1) == true || parameterName.equals(PARAM_SCRIPTNAME2) == true || parameterName.equals(PARAM_RETURNXML) == true || parameterName.equals(PARAM_RETURNJSON) == true || parameterName.equals(PARAM_CALLBACK) == true) { continue; } String parameterValue = (String) request.getParameter(parameterName); if (parameterName.equals(PARAM_INPUTXML) == true) { service.setInputXML(parameterValue); scriptInputSet = true; continue; } if (parameterName.equals(PARAM_INPUTJSON) == true) { try { // The input object is a JSON object... so convert it into XML... JSONObject json = new JSONObject(parameterValue); service.setInputXML(XML.toString(json)); scriptInputSet = true; println("JSON converted to \n" + XML.toString(json)); } catch (JSONException ex) { errorResponse(response, "Could not create JSON object." + ex.toString() + ". " + ex.getStackTrace(), responseFormat, callbackFunction); freeServiceScript(service); return; } continue; } /* Any leftover parameters are query parameters. */ println("Query Parameter found... Setting " + parameterName + " to " + parameterValue); service.setParameter(parameterName, parameterValue); } // End of parameters for loop /* If there is content in the request then, unless we have already set it, this is the input to the script... */ if (requestType.equals("POST") && scriptInputSet == false) { try { BufferedReader reader = request.getReader(); StringBuffer buf = new StringBuffer(); String line; String postData; while ((line = reader.readLine()) != null) { buf.append(line); } postData = buf.toString(); log.debug("POST DATA: " + postData); if (postData.startsWith("<")) { service.setInputXML(postData); scriptInputSet = true; } else { try { // The input object is a JSON object... so convert it into XML... JSONObject json = new JSONObject(postData); service.setInputXML(XML.toString(json)); scriptInputSet = true; log.debug("POST JSON converted to \n" + XML.toString(json)); } catch (JSONException ex) { errorResponse(response, "Could not convert POSTed JSON object." + ex.toString() + ". " + ex.getStackTrace(), responseFormat, callbackFunction); freeServiceScript(service); return; } } } catch (Exception ex) { log.warn("Exception getting posted data: " + ex.toString()); errorResponse(response, "Could not convert posted data.", responseFormat, callbackFunction); freeServiceScript(service); return; } } /* If the service name has been redirected then set any parameters that where embedded in the URI... */ if (serviceMapping != null) { Properties serviceParameters = serviceMapping.getParameters(); String paramName; String paramValue; for (Enumeration<Object> en = serviceParameters.keys(); en.hasMoreElements();) { paramName = (String) en.nextElement(); paramValue = (String) serviceParameters.get(paramName); service.setParameter(paramName, paramValue); } } String serviceResultString = null; /** * Run the service script... */ service.setSessionRequest(request); service.setSessionResponse(response); service.setCallbackFunction(callbackFunction); /* Check if the service has a predefined output format... */ /* If the user has specified a format then that is used.. */ String operationResponseFormat; operationResponseFormat = service.getResponseFormat(); if (defaultResponseFormat == true && operationResponseFormat != null && operationResponseFormat.equals("") == false) { responseFormat = operationResponseFormat; } service.setInvokeResponseFormat(responseFormat); /* If this is a priviledged operation then pass in a reference to the servlet... */ String priviledgedOperation = service.getPriviledged(); if (priviledgedOperation.compareToIgnoreCase("true") == 0 || priviledgedOperation.compareToIgnoreCase("yes") == 0 || priviledgedOperation.compareToIgnoreCase("y") == 0) { service.setPriviledgedHelper(this); } serviceResultString = service.invokeOperation(); if (serviceResultString == null) { errorResponse(response, "Error invoking the operation.<br><b>" + service.getScriptMessage() + "</b>", responseFormat, callbackFunction); freeServiceScript(service); return; } /* Return the results... */ if (serviceResultString != null && serviceResultString.equals("") == false) { writeResponse(serviceResultString, responseFormat, callbackFunction, response); } println(">> Service script executed successfully."); /* Free the service instance... */ freeServiceScript(service); } catch (Exception ex) { errorResponse(response, "Exception processing request: " + ex.toString(), responseFormat, callbackFunction); ex.printStackTrace(); try { freeServiceScript(service); } catch (Exception x) { log.warn("Exception freeing a service instance: " + x.toString()); } return; } println(">> Finished processRequest() at " + new Date()); }