List of usage examples for java.util.regex Matcher reset
public Matcher reset(CharSequence input)
From source file:net.yacy.cora.document.id.MultiProtocolURL.java
/** * Resolve '..' segments in the path./* w w w . jav a 2s . co m*/ * For standard pseudo algorithms, see : * <ul> * <li>https://tools.ietf.org/html/rfc3986#section-5.2.4</li> * <li>https://url.spec.whatwg.org/#path-state</li> * <li>https://www.w3.org/TR/url/#relative-path-state</li> * </ul> * @param path URL path part : must not be null * @return the path with '..' segments resolved */ private static final String resolveBackpath(final String path) { String p = path; if (p.isEmpty() || p.charAt(0) != '/') { p = "/" + p; } final Matcher qm = CommonPattern.QUESTION.matcher(p); // do not resolve backpaths in the post values final int end = qm.find() ? qm.start() : p.length(); final Matcher matcher = backPathPattern.matcher(p); while (matcher.find()) { if (matcher.start() > end) break; p = matcher.replaceAll(""); matcher.reset(p); } /* Let's remove any eventual remaining but inappropriate '..' segments at the beginning. * See https://tools.ietf.org/html/rfc3986#section-5.2.4 -> parts 2.C and 2.D */ while (p.startsWith("/../")) { p = p.substring(3); } if (p.equals("/..")) { p = "/"; } return p.equals("") ? "/" : p; }
From source file:edu.harvard.iq.dvn.ingest.statdataio.impl.plugins.spss.SPSSFileReader.java
int read_DataList(String dataListCommand) throws IOException { int readStatus = 0; // Read the first line (DATA LIST ...) to determine // the field separator: // This line should be "/"-terminated (?) dbgLog.fine("dataList command: " + dataListCommand); List<Integer> variableTypeList = new ArrayList<Integer>(); Set<Integer> decimalVariableSet = new HashSet<Integer>(); List<Integer> printFormatList = new ArrayList<Integer>(); // TODO: move Map<String, String> printFormatNameTable = new LinkedHashMap<String, String>(); // TODO: move String delimiterString = null; //String datalistRegex = "^data\\s+list\\s+list\\('(.)'\\)\\s+?/"; String datalistRegex = "^list\\s*\\('(.)'\\)"; Pattern datalistPattern = Pattern.compile(datalistRegex, java.util.regex.Pattern.CASE_INSENSITIVE); Matcher datalistMatcher = datalistPattern.matcher(dataListCommand); if (datalistMatcher.find()) { delimiterString = datalistMatcher.group(1); setDelimiterChar(delimiterString.charAt(0)); dbgLog.fine("found delimiter: " + delimiterString); } else {/*w w w. jav a 2s . c o m*/ throw new IOException( "Invalid SPSS Command Syntax: " + "no delimiter specified in the DATA LIST command."); } // Cut off the remaining lines containing the variable definitions: int separatorIndex = dataListCommand.indexOf("/"); if (separatorIndex == -1) { throw new IOException("Invalid SPSS Command Syntax: " + "missing / delimiter on the DATA LIST line."); // No slash found after the first line of the Data List command. } dataListCommand = dataListCommand.substring(separatorIndex + 1); // Parse the variable section. For a delimited file this should be // a list of variable name + data type pairs. // "fortran" type definitions are assumed. dbgLog.fine("parsing " + dataListCommand + " for variable declarations."); int variableCounter = 0; String varName = null; String varType = null; String varDeclarationRegex = "^\\s*(\\S+)\\s+\\((\\S*)\\)"; Pattern varDeclarationPattern = Pattern.compile(varDeclarationRegex); Matcher varDeclarationMatcher = varDeclarationPattern.matcher(dataListCommand); String stringVarDeclarationRegex = "^[aA]([0-9]+)"; Pattern stringVarDeclarationPattern = Pattern.compile(stringVarDeclarationRegex); String numVarDeclarationRegex = "^[fF]([0-9]+)\\.*([0-9]*)"; Pattern numVarDeclarationPattern = Pattern.compile(numVarDeclarationRegex); int endOfMatches = 0; while (varDeclarationMatcher.find()) { varName = varDeclarationMatcher.group(1); varType = varDeclarationMatcher.group(2); endOfMatches = varDeclarationMatcher.end(); dataListCommand = dataListCommand.substring(endOfMatches); varDeclarationMatcher.reset(dataListCommand); dbgLog.fine("found variable " + varName + ", type " + varType); if (varType == null || varType.equals("")) { throw new IOException( "Invalid variable declaration in DATA LIST command: no type specified for variable " + varName + "."); } variableNameList.add(varName); // unfVariableTypes list holds extended type definitions for the // UNF calculation; // we need to be able to differentiate between Integers and // real numbers, in addition to the "numeric" and "string" values. varType = varType.toUpperCase(); if (varType.startsWith("A")) { // String: Matcher stringVarDeclarationMatcher = stringVarDeclarationPattern.matcher(varType); if (stringVarDeclarationMatcher.find()) { variableTypeList.add(new Integer(stringVarDeclarationMatcher.group(1))); } else { // set to default if the string size is not explicitely // specified: variableTypeList.add(1); } formatCategoryTable.put(varName, SPSSConstants.FORMAT_CATEGORY_TABLE.get("A")); unfVariableTypes.put(varName, -1); printFormatList.add(1); //printFormatNameTable.put(varName, "A"); } else if (varType.startsWith("F")) { // "minimal" format value is 0 -- numeric variableTypeList.add(0); formatCategoryTable.put(varName, SPSSConstants.FORMAT_CATEGORY_TABLE.get("F")); if (varType.equals("F")) { // abbreviated numeric type definition; // defaults to f10.0 // for the purposes of the UNF calculations this is an integer: unfVariableTypes.put(varName, 0); printFormatList.add(5); //printFormatNameTable.put(varName, "F10.0"); } else { Matcher numVarDeclarationMatcher = numVarDeclarationPattern.matcher(varType); if (numVarDeclarationMatcher.find()) { Integer numLength = new Integer(numVarDeclarationMatcher.group(1)); Integer numDecimal = 0; String optionalToken = numVarDeclarationMatcher.group(2); if (optionalToken != null && !optionalToken.equals("")) { numDecimal = new Integer(optionalToken); if ((int) numDecimal > 0) { unfVariableTypes.put(varName, 1); decimalVariableSet.add(variableCounter); printFormatNameTable.put(varName, "F" + numLength + "." + numDecimal); } } printFormatList.add(5); // TODO: verify (should it be 0 instead?) } else { // This does not look like a valid numeric type // definition. throw new IOException("Invalid variable declaration in the DATA LIST command: " + "Illegal or unsupported numeric type definition for variable " + varName); } } } else if (varType.matches("^[1-9]$")) { // Another allowed SPSS abbreviation: // type (N) where N is [1-9] means a numeric decimal with // N decimal positions: variableTypeList.add(0); formatCategoryTable.put(varName, SPSSConstants.FORMAT_CATEGORY_TABLE.get("F")); Integer numDecimal = new Integer(varType); unfVariableTypes.put(varName, 1); decimalVariableSet.add(variableCounter); printFormatList.add(5); // TODO: verify (should it be 0 instead?) printFormatNameTable.put(varName, "F10." + numDecimal); // Check for various date and time formats that we support: } else if (SPSSConstants.FORMAT_CATEGORY_TABLE.get(varType) != null) { //if ( SPSSConstants.FORMAT_CATEGORY_TABLE.get(varType).equals("date") // || SPSSConstants.FORMAT_CATEGORY_TABLE.get(varType).equals("time") // || varType.equals("WKDAY") // || varType.equals("MONTH")) { if (varType.equalsIgnoreCase("DATE") || varType.equalsIgnoreCase("DATETIME")) { variableTypeList.add(1); formatCategoryTable.put(varName, SPSSConstants.FORMAT_CATEGORY_TABLE.get(varType)); unfVariableTypes.put(varName, -1); //printFormatList.add(); // TODO: confirm that this isn't needed. printFormatNameTable.put(varName, varType); } else { throw new IOException("Invalid variable declaration in DATA LIST command: " + "unsupported variable type definition for variable " + varName + "."); } } else { // invalid/unrecognized variable type definition. throw new IOException("Invalid variable declaration in DATA LIST command: " + "unknown or illegal type definition for variable " + varName + "."); } variableCounter++; } //dataListCommand = dataListCommand.substring(endOfMatches); // check to see if we've parsed the entire DATA LIST section: if (!dataListCommand.matches("^[ \t\n\r]*$")) { throw new IOException("Illegal control card syntax: " + "this portion of the DATA LIST command could not be parsed: " + dataListCommand); } smd.getFileInformation().put("varQnty", variableCounter); setVarQnty(variableCounter); dbgLog.fine("varQnty=" + getVarQnty()); smd.setVariableName(variableNameList.toArray(new String[variableNameList.size()])); // "minimal" variable types: SPSS type binary definition: // 0 means numeric, >0 means string. smd.setVariableTypeMinimal( ArrayUtils.toPrimitive(variableTypeList.toArray(new Integer[variableTypeList.size()]))); // This is how the "discrete" and "continuous" numeric values are // distinguished in the data set metadata: smd.setDecimalVariables(decimalVariableSet); //TODO: smd.getFileInformation().put("caseWeightVariableName", caseWeightVariableName); dbgLog.info("<<<<<<"); dbgLog.info("printFormatList = " + printFormatList); dbgLog.info("printFormatNameTable = " + printFormatNameTable); dbgLog.info("formatCategoryTable = " + formatCategoryTable); dbgLog.info(">>>>>>"); smd.setVariableFormat(printFormatList); smd.setVariableFormatName(printFormatNameTable); smd.setVariableFormatCategory(formatCategoryTable); //TODO: verify return variableCounter; }
From source file:org.structr.rest.resource.LogResource.java
private TreeMap<Long, Map<String, Object>> toHistogramCountMap(final LogState state) throws FrameworkException { final Pattern pattern = Pattern.compile(state.histogram()); final Matcher matcher = pattern.matcher(""); final TreeMap<Long, Map<String, Object>> countMap = new TreeMap<>(); for (final Map<String, Object> entry : state.entries()) { final String message = (String) entry.get(messageProperty.jsonName()); final long timestamp = (Long) entry.get(timestampProperty.jsonName()); Map<String, Object> obj = countMap.get(timestamp); if (obj == null) { obj = new LinkedHashMap<>(); }//w w w . j ava 2 s. c o m Integer count = (Integer) obj.get(totalProperty.jsonName()); if (count == null) { count = 1; } else { count = count + 1; } obj.put(totalProperty.jsonName(), count); // iterate over patterns matcher.reset(message); if (matcher.matches()) { final String key = matcher.group(1); final int multiplier = getMultiplier(message, state); Integer c = (Integer) obj.get(key); if (c == null) { c = multiplier; } else { c = c + multiplier; } obj.put(key, c); } countMap.put(timestamp, obj); } return countMap; }
From source file:org.ala.dao.FulltextSearchDaoImplSolr.java
/** * Applies a prefix and suffix to higlight the search terms in the * supplied list.//from w w w. j a v a 2 s .co m * * NC: This is a workaround as I can not get SOLR highlighting to work for partial term matches. * * @param names * @param m * @return */ private List<String> getHighlightedNames(List<String> names, java.util.regex.Matcher m, String prefix, String suffix) { LinkedHashSet<String> hlnames = null; List<String> lnames = null; if (names != null) { hlnames = new LinkedHashSet<String>(); for (String name : names) { String name1 = SolrUtils.concatName(name.trim()); m.reset(name1); if (m.find()) { //insert <b> and </b>at the start and end index name = name.substring(0, m.start()) + prefix + name.substring(m.start(), m.end()) + suffix + name.substring(m.end(), name.length()); hlnames.add(name); } } if (!hlnames.isEmpty()) { lnames = new ArrayList<String>(hlnames); Collections.sort(lnames); } else { lnames = new ArrayList<String>(); } } return lnames; }
From source file:org.structr.web.servlet.HtmlServlet.java
@Override protected void doHead(final HttpServletRequest request, final HttpServletResponse response) { final Authenticator auth = getConfig().getAuthenticator(); SecurityContext securityContext;//from w ww. j av a 2 s .c o m List<Page> pages = null; boolean requestUriContainsUuids = false; final App app; try { String path = request.getPathInfo(); // isolate request authentication in a transaction try (final Tx tx = StructrApp.getInstance().tx()) { securityContext = auth.initializeAndExamineRequest(request, response); tx.success(); } app = StructrApp.getInstance(securityContext); try (final Tx tx = app.tx()) { // Ensure access mode is frontend securityContext.setAccessMode(AccessMode.Frontend); request.setCharacterEncoding("UTF-8"); // Important: Set character encoding before calling response.getWriter() !!, see Servlet Spec 5.4 response.setCharacterEncoding("UTF-8"); response.setContentLength(0); boolean dontCache = false; logger.log(Level.FINE, "Path info {0}", path); // don't continue on redirects if (response.getStatus() == 302) { tx.success(); return; } final Principal user = securityContext.getUser(false); if (user != null) { // Don't cache if a user is logged in dontCache = true; } final RenderContext renderContext = RenderContext.getInstance(securityContext, request, response); renderContext.setResourceProvider(config.getResourceProvider()); final EditMode edit = renderContext.getEditMode(user); DOMNode rootElement = null; AbstractNode dataNode = null; String[] uriParts = PathHelper.getParts(path); if ((uriParts == null) || (uriParts.length == 0)) { // find a visible page rootElement = findIndexPage(securityContext, pages, edit); logger.log(Level.FINE, "No path supplied, trying to find index page"); } else { if (rootElement == null) { rootElement = findPage(securityContext, pages, path, edit); } else { dontCache = true; } } if (rootElement == null) { // No page found // Look for a file File file = findFile(securityContext, request, path); if (file != null) { //streamFile(securityContext, file, request, response, edit); tx.success(); return; } // store remaining path parts in request Matcher matcher = threadLocalUUIDMatcher.get(); for (int i = 0; i < uriParts.length; i++) { request.setAttribute(uriParts[i], i); matcher.reset(uriParts[i]); // set to "true" if part matches UUID pattern requestUriContainsUuids |= matcher.matches(); } if (!requestUriContainsUuids) { // Try to find a data node by name dataNode = findFirstNodeByName(securityContext, request, path); } else { dataNode = findNodeByUuid(securityContext, PathHelper.getName(path)); } if (dataNode != null && !(dataNode instanceof Linkable)) { // Last path part matches a data node // Remove last path part and try again searching for a page // clear possible entry points request.removeAttribute(POSSIBLE_ENTRY_POINTS_KEY); rootElement = findPage(securityContext, pages, StringUtils.substringBeforeLast(path, PathHelper.PATH_SEP), edit); renderContext.setDetailsDataObject(dataNode); // Start rendering on data node if (rootElement == null && dataNode instanceof DOMNode) { rootElement = ((DOMNode) dataNode); } } } // look for pages with HTTP Basic Authentication (must be done as superuser) if (rootElement == null) { final HttpBasicAuthResult authResult = checkHttpBasicAuth(request, response, path); switch (authResult.authState()) { // Element with Basic Auth found and authentication succeeded case Authenticated: final Linkable result = authResult.getRootElement(); if (result instanceof Page) { rootElement = (DOMNode) result; renderContext.pushSecurityContext(authResult.getSecurityContext()); } else if (result instanceof File) { //streamFile(authResult.getSecurityContext(), (File)result, request, response, EditMode.NONE); tx.success(); return; } break; // Page with Basic Auth found but not yet authenticated case MustAuthenticate: return; // no Basic Auth for given path, go on case NoBasicAuth: break; } } // Still nothing found, do error handling if (rootElement == null) { // Check if security context has set an 401 status if (response.getStatus() == HttpServletResponse.SC_UNAUTHORIZED) { try { UiAuthenticator.writeUnauthorized(response); } catch (IllegalStateException ise) { } } else { rootElement = notFound(response, securityContext); } } if (rootElement == null) { // no content response.setContentLength(0); response.getOutputStream().close(); tx.success(); return; } // check dont cache flag on page (if root element is a page) // but don't modify true to false dontCache |= rootElement.getProperty(Page.dontCache); if (EditMode.WIDGET.equals(edit) || dontCache) { setNoCacheHeaders(response); } if (!securityContext.isVisible(rootElement)) { rootElement = notFound(response, securityContext); if (rootElement == null) { tx.success(); return; } } if (securityContext.isVisible(rootElement)) { if (!EditMode.WIDGET.equals(edit) && !dontCache && notModifiedSince(request, response, rootElement, dontCache)) { response.getOutputStream().close(); } else { // prepare response response.setCharacterEncoding("UTF-8"); String contentType = rootElement.getProperty(Page.contentType); if (contentType == null) { // Default contentType = "text/html;charset=UTF-8"; } if (contentType.equals("text/html")) { contentType = contentType.concat(";charset=UTF-8"); } response.setContentType(contentType); setCustomResponseHeaders(response); response.getOutputStream().close(); } } else { notFound(response, securityContext); response.getOutputStream().close(); } tx.success(); } catch (Throwable fex) { fex.printStackTrace(); logger.log(Level.SEVERE, "Exception while processing request", fex); } } catch (FrameworkException t) { t.printStackTrace(); logger.log(Level.SEVERE, "Exception while processing request", t); UiAuthenticator.writeInternalServerError(response); } }
From source file:org.ala.dao.FulltextSearchDaoImplSolr.java
/** * if word highlight enabled then do the exact match, otherwise do the concat match * /*from w w w. j a v a 2 s. com*/ * @param names * @param term * @param prefix * @param suffix * @return */ private List<String> getHighlightedNames(List<String> names, String term, String prefix, String suffix) { LinkedHashSet<String> hlnames = null; List<String> lnames = null; String value = null; boolean isHighlight = false; //have word highlight if (prefix != null && suffix != null && prefix.trim().length() > 0 && suffix.trim().length() > 0 && term != null) { value = SolrUtils.cleanName(term.trim()); isHighlight = true; } else { value = SolrUtils.concatName(term); } Pattern p = Pattern.compile(value, Pattern.CASE_INSENSITIVE); java.util.regex.Matcher m = p.matcher(value); if (names != null) { hlnames = new LinkedHashSet<String>(); for (String name : names) { String name1 = null; name = name.trim(); if (isHighlight) { name1 = name; } else { name1 = SolrUtils.concatName(name); } m.reset(name1); if (m.find()) { //insert <b> and </b>at the start and end index name = name.substring(0, m.start()) + prefix + name.substring(m.start(), m.end()) + suffix + name.substring(m.end(), name.length()); hlnames.add(name); } } if (!hlnames.isEmpty()) { lnames = new ArrayList<String>(hlnames); Collections.sort(lnames); } else { lnames = new ArrayList<String>(); } } return lnames; }
From source file:org.structr.web.servlet.HtmlServlet.java
@Override protected void doGet(final HttpServletRequest request, final HttpServletResponse response) { final Authenticator auth = getConfig().getAuthenticator(); List<Page> pages = null; boolean requestUriContainsUuids = false; SecurityContext securityContext;//from w w w .j ava 2 s . co m final App app; try { final String path = request.getPathInfo(); // check for registration (has its own tx because of write access if (checkRegistration(auth, request, response, path)) { return; } // check for registration (has its own tx because of write access if (checkResetPassword(auth, request, response, path)) { return; } // isolate request authentication in a transaction try (final Tx tx = StructrApp.getInstance().tx()) { securityContext = auth.initializeAndExamineRequest(request, response); tx.success(); } app = StructrApp.getInstance(securityContext); try (final Tx tx = app.tx()) { // Ensure access mode is frontend securityContext.setAccessMode(AccessMode.Frontend); request.setCharacterEncoding("UTF-8"); // Important: Set character encoding before calling response.getWriter() !!, see Servlet Spec 5.4 response.setCharacterEncoding("UTF-8"); boolean dontCache = false; logger.log(Level.FINE, "Path info {0}", path); // don't continue on redirects if (response.getStatus() == 302) { tx.success(); return; } final Principal user = securityContext.getUser(false); if (user != null) { // Don't cache if a user is logged in dontCache = true; } final RenderContext renderContext = RenderContext.getInstance(securityContext, request, response); renderContext.setResourceProvider(config.getResourceProvider()); final EditMode edit = renderContext.getEditMode(user); DOMNode rootElement = null; AbstractNode dataNode = null; final String[] uriParts = PathHelper.getParts(path); if ((uriParts == null) || (uriParts.length == 0)) { // find a visible page rootElement = findIndexPage(securityContext, pages, edit); logger.log(Level.FINE, "No path supplied, trying to find index page"); } else { if (rootElement == null) { rootElement = findPage(securityContext, pages, path, edit); } else { dontCache = true; } } if (rootElement == null) { // No page found // Look for a file final File file = findFile(securityContext, request, path); if (file != null) { streamFile(securityContext, file, request, response, edit); tx.success(); return; } // store remaining path parts in request final Matcher matcher = threadLocalUUIDMatcher.get(); for (int i = 0; i < uriParts.length; i++) { request.setAttribute(uriParts[i], i); matcher.reset(uriParts[i]); // set to "true" if part matches UUID pattern requestUriContainsUuids |= matcher.matches(); } if (!requestUriContainsUuids) { // Try to find a data node by name dataNode = findFirstNodeByName(securityContext, request, path); } else { dataNode = findNodeByUuid(securityContext, PathHelper.getName(path)); } //if (dataNode != null && !(dataNode instanceof Linkable)) { if (dataNode != null) { // Last path part matches a data node // Remove last path part and try again searching for a page // clear possible entry points request.removeAttribute(POSSIBLE_ENTRY_POINTS_KEY); rootElement = findPage(securityContext, pages, StringUtils.substringBeforeLast(path, PathHelper.PATH_SEP), edit); renderContext.setDetailsDataObject(dataNode); // Start rendering on data node if (rootElement == null && dataNode instanceof DOMNode) { rootElement = ((DOMNode) dataNode); } } } // look for pages with HTTP Basic Authentication (must be done as superuser) if (rootElement == null) { final HttpBasicAuthResult authResult = checkHttpBasicAuth(request, response, path); switch (authResult.authState()) { // Element with Basic Auth found and authentication succeeded case Authenticated: final Linkable result = authResult.getRootElement(); if (result instanceof Page) { rootElement = (DOMNode) result; securityContext = authResult.getSecurityContext(); renderContext.pushSecurityContext(securityContext); } else if (result instanceof File) { streamFile(authResult.getSecurityContext(), (File) result, request, response, EditMode.NONE); tx.success(); return; } break; // Page with Basic Auth found but not yet authenticated case MustAuthenticate: tx.success(); return; // no Basic Auth for given path, go on case NoBasicAuth: break; } } // Still nothing found, do error handling if (rootElement == null) { rootElement = notFound(response, securityContext); } if (rootElement == null) { tx.success(); return; } // check dont cache flag on page (if root element is a page) // but don't modify true to false dontCache |= rootElement.getProperty(Page.dontCache); if (EditMode.WIDGET.equals(edit) || dontCache) { setNoCacheHeaders(response); } if (!securityContext.isVisible(rootElement)) { rootElement = notFound(response, securityContext); if (rootElement == null) { tx.success(); return; } } else { if (!EditMode.WIDGET.equals(edit) && !dontCache && notModifiedSince(request, response, rootElement, dontCache)) { ServletOutputStream out = response.getOutputStream(); out.flush(); //response.flushBuffer(); out.close(); } else { // prepare response response.setCharacterEncoding("UTF-8"); String contentType = rootElement.getProperty(Page.contentType); if (contentType == null) { // Default contentType = "text/html;charset=UTF-8"; } if (contentType.equals("text/html")) { contentType = contentType.concat(";charset=UTF-8"); } response.setContentType(contentType); setCustomResponseHeaders(response); final boolean createsRawData = rootElement.getProperty(Page.pageCreatesRawData); // async or not? if (isAsync && !createsRawData) { final AsyncContext async = request.startAsync(); final ServletOutputStream out = async.getResponse().getOutputStream(); final AtomicBoolean finished = new AtomicBoolean(false); final DOMNode rootNode = rootElement; threadPool.submit(new Runnable() { @Override public void run() { try (final Tx tx = app.tx()) { //final long start = System.currentTimeMillis(); // render rootNode.render(renderContext, 0); finished.set(true); //final long end = System.currentTimeMillis(); //System.out.println("Done in " + (end-start) + " ms"); tx.success(); } catch (Throwable t) { t.printStackTrace(); final String errorMsg = t.getMessage(); try { //response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, errorMsg); finished.set(true); } catch (IOException ex) { ex.printStackTrace(); } } } }); // start output write listener out.setWriteListener(new WriteListener() { @Override public void onWritePossible() throws IOException { try { final Queue<String> queue = renderContext.getBuffer().getQueue(); while (out.isReady()) { String buffer = null; synchronized (queue) { buffer = queue.poll(); } if (buffer != null) { out.print(buffer); } else { if (finished.get()) { async.complete(); response.setStatus(HttpServletResponse.SC_OK); // prevent this block from being called again break; } Thread.sleep(1); } } } catch (Throwable t) { t.printStackTrace(); } } @Override public void onError(Throwable t) { t.printStackTrace(); } }); } else { final StringRenderBuffer buffer = new StringRenderBuffer(); renderContext.setBuffer(buffer); // render rootElement.render(renderContext, 0); try { response.getOutputStream().write(buffer.getBuffer().toString().getBytes("utf-8")); response.getOutputStream().flush(); response.getOutputStream().close(); } catch (IOException ioex) { ioex.printStackTrace(); } } } } tx.success(); } catch (FrameworkException fex) { fex.printStackTrace(); logger.log(Level.SEVERE, "Exception while processing request", fex); } } catch (IOException | FrameworkException t) { t.printStackTrace(); logger.log(Level.SEVERE, "Exception while processing request", t); UiAuthenticator.writeInternalServerError(response); } }
From source file:org.oxymores.chronix.engine.RunnerShell.java
public static RunResult run(RunDescription rd, String logFilePath, boolean storeLogFile, boolean returnFullerLog) { RunResult res = new RunResult(); Process p;//from ww w. j a v a2 s. co m String nl = System.getProperty("line.separator"); Pattern pat = Pattern.compile("^set ([a-zA-Z]+[a-zA-Z0-9]*)=(.+)"); Matcher matcher = pat.matcher("Testing123Testing"); String encoding = getEncoding(rd); log.debug("Encoding is " + encoding); // /////////////////////////// // Build command List<String> argsStrings = buildCommand(rd); // ///////////////////////////////////////////////////////////////////////// // Create a process builder with the command line contained in the array ProcessBuilder pb = new ProcessBuilder(argsStrings); // Mix stdout and stderr (easier to put errors in context this way) pb.redirectErrorStream(true); // Create array containing environment Map<String, String> env = pb.environment(); for (int i = 0; i < rd.getEnvNames().size(); i++) { env.put(rd.getEnvNames().get(i), rd.getEnvValues().get(i)); } BufferedReader br = null; Writer output = null; try { // Start! log.debug("GO (" + rd.getSubMethod() + ")"); p = pb.start(); // Read output (err & out), write it to file InputStreamReader isr = new InputStreamReader(p.getInputStream(), encoding); br = new BufferedReader(isr); String line = null; int i = 0; LinkedHashMap<Integer, String> endBuffer = new LinkedHashMap<Integer, String>() { private static final long serialVersionUID = -6773540176968046737L; @Override protected boolean removeEldestEntry(java.util.Map.Entry<Integer, String> eldest) { return this.size() > Constants.MAX_RETURNED_BIG_LOG_END_LINES; } }; if (storeLogFile) { output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(logFilePath), "UTF-8")); } line = br.readLine(); while (line != null) { i++; // Local log file gets all lines if (storeLogFile) { output.write(line + nl); } // Small log gets first 500 lines or 10000 characters (the smaller of the two) if (i < Constants.MAX_RETURNED_SMALL_LOG_LINES && res.logStart.length() < Constants.MAX_RETURNED_SMALL_LOG_CHARACTERS) { res.logStart += nl + line; } // Scheduler internal log gets first line only if (i == 1) { log.debug(String.format("Job running. First line of output is: %s", line)); } // Fuller log gets first 10k lines, then last 1k lines. if (returnFullerLog) { if (i < Constants.MAX_RETURNED_BIG_LOG_LINES) { res.fullerLog += line; } else { endBuffer.put(i, line); } } // Analysis: there may be a new variable definition in the line matcher.reset(line); if (matcher.find()) { log.debug("Key detected :" + matcher.group(1)); log.debug("Value detected :" + matcher.group(2)); res.newEnvVars.put(matcher.group(1), matcher.group(2)); } line = br.readLine(); } IOUtils.closeQuietly(br); if (i > Constants.MAX_RETURNED_BIG_LOG_LINES && i < Constants.MAX_RETURNED_BIG_LOG_LINES + Constants.MAX_RETURNED_BIG_LOG_END_LINES && returnFullerLog) { res.fullerLog += Arrays.toString(endBuffer.entrySet().toArray()); } if (i >= Constants.MAX_RETURNED_BIG_LOG_LINES + Constants.MAX_RETURNED_BIG_LOG_END_LINES && returnFullerLog) { res.fullerLog += "\n\n\n*******\n LOG TRUNCATED - See full log on server\n********\n\n\n" + Arrays.toString(endBuffer.entrySet().toArray()); } // Done: close log file if (storeLogFile) { IOUtils.closeQuietly(output); File f = new File(logFilePath); res.logSizeBytes = f.length(); } } catch (IOException e) { log.error("error occurred while running job", e); res.logStart = e.getMessage(); res.returnCode = -1; IOUtils.closeQuietly(br); IOUtils.closeQuietly(output); return res; } // Return res.returnCode = p.exitValue(); res.logPath = logFilePath; res.envtUser = System.getProperty("user.name"); try { res.envtServer = InetAddress.getLocalHost().getHostName(); } catch (UnknownHostException e) { res.envtServer = "unknown"; } log.info(String.format("Job ended, RC is %s", res.returnCode)); return res; }
From source file:org.jets3t.apps.uploader.Uploader.java
/** * Replaces variables of the form ${variableName} in the input string with the value of that * variable name in the local uploaderProperties properties object, or with one of the * following special variables:/*from www . ja va 2 s . c o m*/ * <ul> * <li>fileName : Name of file being uploaded</li> * <li>fileSize : Size of the file being uploaded, eg 1.04 MB</li> * <li>filePath : Absolute path of the file being uploaded</li> * <li>maxFileSize : The maxiumum allowed file size in MB</li> * <li>maxFileCount : The maximum number of files that may be uploaded</li> * <li>validFileExtensions : A list of the file extensions allowed</li> * </ul> * If the variable named in the input string is not available, or has no value, the variable * reference is left in the result. * * @param message * string that may have variables to replace * @return * the input string with any variable referenced replaced with the variable's value. */ private String replaceMessageVariables(String message) { if (message == null) { return ""; } String result = message; // Replace upload file variables, if an upload file has been chosen. if (filesToUpload != null) { long filesSize = 0; StringBuffer fileNameList = new StringBuffer(); for (int i = 0; i < filesToUpload.length; i++) { filesSize += filesToUpload[i].length(); fileNameList.append(filesToUpload[i].getName()).append(" "); } result = result.replaceAll("\\$\\{fileNameList\\}", fileNameList.toString()); result = result.replaceAll("\\$\\{filesSize\\}", byteFormatter.formatByteSize(filesSize)); } result = result.replaceAll("\\$\\{maxFileSize\\}", String.valueOf(fileMaxSizeMB)); result = result.replaceAll("\\$\\{maxFileCount\\}", String.valueOf(fileMaxCount)); String extList = validFileExtensions.toString(); extList = extList.substring(1, extList.length() - 1); extList = extList.replaceAll(",", " "); result = result.replaceAll("\\$\\{validFileExtensions\\}", extList); Pattern pattern = Pattern.compile("\\$\\{.+?\\}"); Matcher matcher = pattern.matcher(result); int offset = 0; while (matcher.find(offset)) { String variable = matcher.group(); String variableName = variable.substring(2, variable.length() - 1); String replacement = null; if (userInputProperties != null && userInputProperties.containsKey(variableName)) { log.debug("Replacing variable '" + variableName + "' with value from a user input field"); replacement = userInputProperties.getProperty(variableName, null); } else if (parametersMap != null && parametersMap.containsKey(variableName)) { log.debug("Replacing variable '" + variableName + "' with value from Uploader's parameters"); replacement = (String) parametersMap.get(variableName); } else if (uploaderProperties != null && uploaderProperties.containsKey(variableName)) { log.debug("Replacing variable '" + variableName + "' with value from uploader.properties file"); replacement = uploaderProperties.getStringProperty(variableName, null); } if (replacement != null) { result = result.substring(0, matcher.start()) + replacement + result.substring(matcher.end()); offset = matcher.start() + 1; matcher.reset(result); } else { offset = matcher.start() + 1; } } if (!result.equals(message)) { log.debug("Replaced variables in text: " + message + " => " + result); } return result; }
From source file:it.infn.ct.nuclemd.Nuclemd.java
public String RemoveCarriageReturn(String InputFileName, String OutputFileName) { // Remove the carriage return char from a named file. FileInputStream fis;/*from ww w . j a va2 s.co m*/ try { fis = new FileInputStream(InputFileName); BufferedReader in = new BufferedReader(new InputStreamReader(fis)); File fout = new File(OutputFileName); FileOutputStream fos = new FileOutputStream(fout); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fos)); // The pattern matches control characters Pattern p = Pattern.compile("\r"); Matcher m = p.matcher(""); String aLine = null; try { while ((aLine = in.readLine()) != null) { m.reset(aLine); //Replaces control characters with an empty string. String result = m.replaceAll(""); out.write(result); out.newLine(); } out.close(); } catch (IOException ex) { Logger.getLogger(Nuclemd.class.getName()).log(Level.SEVERE, null, ex); } } catch (FileNotFoundException ex) { Logger.getLogger(Nuclemd.class.getName()).log(Level.SEVERE, null, ex); } log.info("\n- Writing the user's stripped file: [ " + OutputFileName.toString() + " ] to disk"); return OutputFileName; }