@Override public byte[] getRawFile(String path) throws SVNException { org.tmatesoft.svn.core.io.SVNRepository repository = getSVNRepository(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); repository.getFile(path, -1l, null, baos); return baos.toByteArray(); }
public BufferedRequestWrapper(HttpServletRequest req, String body) throws IOException { super(req); InputStream is = req.getInputStream(); baos = new ByteArrayOutputStream(); baos.write(body.getBytes()); buffer = baos.toByteArray(); }
/** @throws IOException If failed. */ private void initFavicon() throws IOException { assert favicon == null; InputStream in = getClass().getResourceAsStream("favicon.ico"); if (in != null) { BufferedInputStream bis = new BufferedInputStream(in); ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { byte[] buf = new byte[2048]; while (true) { int n = bis.read(buf); if (n == -1) break; bos.write(buf, 0, n); } favicon = bos.toByteArray(); } finally { U.closeQuiet(bis); } } }
private void processEncrypt(final ServletRequest servletRequest ,final HttpServletRequest httpRequest,final ServletResponse servletResponse, final FilterChain filterChain)throws IOException, ServletException{ InputStream inputStream = servletRequest.getInputStream(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte buf[] = new byte[1024]; int letti=-1; while ((letti = inputStream.read(buf)) > 0){ byteArrayOutputStream.write(buf, 0, letti); } final BufferedRequestWrapper bufferedRequest = new BufferedRequestWrapper(httpRequest,doEncrypt(byteArrayOutputStream)); final HttpServletResponse response = (HttpServletResponse) servletResponse; final ByteArrayPrintWriter pw = new ByteArrayPrintWriter(); HttpServletResponse wrappedResp = new HttpServletResponseWrapper(response) { public PrintWriter getWriter() { return pw.getWriter(); } public ServletOutputStream getOutputStream() { return pw.getStream(); } }; filterChain.doFilter(bufferedRequest, wrappedResp); }
// callRestfulApi - Calls restful API and returns results as a string public String callRestfulApi( String addr, HttpServletRequest request, HttpServletResponse response) { if (localCookie) CookieHandler.setDefault(cm); try { ByteArrayOutputStream output = new ByteArrayOutputStream(); URL url = new URL(API_ROOT + addr); URLConnection urlConnection = url.openConnection(); String cookieVal = getBrowserInfiniteCookie(request); if (cookieVal != null) { urlConnection.addRequestProperty("Cookie", "infinitecookie=" + cookieVal); urlConnection.setDoInput(true); urlConnection.setDoOutput(true); urlConnection.setRequestProperty("Accept-Charset", "UTF-8"); } IOUtils.copy(urlConnection.getInputStream(), output); String newCookie = getConnectionInfiniteCookie(urlConnection); if (newCookie != null && response != null) { setBrowserInfiniteCookie(response, newCookie, request.getServerPort()); } return output.toString(); } catch (IOException e) { System.out.println(e.getMessage()); return null; } } // TESTED
public static String getResTxtContent(ClassLoader cl, String p) throws IOException { String s = res2txt_cont.get(p); if (s != null) return s; InputStream is = null; try { is = cl.getResourceAsStream(p); // is = this.getClass().getResourceAsStream(p); if (is == null) return null; byte[] buf = new byte[1024]; ByteArrayOutputStream baos = new ByteArrayOutputStream(); int len; while ((len = is.read(buf)) >= 0) { baos.write(buf, 0, len); } byte[] cont = baos.toByteArray(); s = new String(cont, "UTF-8"); res2txt_cont.put(p, s); return s; } finally { if (is != null) is.close(); } }
protected Object exec(Object[] args, Context context) { int nargs = args.length; if (nargs != 1 && nargs != 2) { undefined(args, context); return null; } HttpServletRequest request = (HttpServletRequest) args[0]; String enc; if (nargs == 1) { enc = ServletEncoding.getDefaultInputEncoding(context); } else { enc = (String) args[1]; } String contentType = request.getContentType(); if (contentType != null && contentType.startsWith("multipart/form-data")) { throw new RuntimeException("not yet implemented"); } else { try { ByteArrayOutputStream bout = new ByteArrayOutputStream(); byte[] buf = new byte[512]; int n; InputStream in = request.getInputStream(); while ((n = in.read(buf, 0, buf.length)) != -1) { bout.write(buf, 0, n); } in.close(); String qs = new String(bout.toByteArray()); Map map = URLEncoding.parseQueryString(qs, enc); return new ServletParameter(map); } catch (IOException e) { throw new PnutsException(e, context); } } }
public BufferedRequestWrapper(HttpServletRequest req) throws IOException { super(req); InputStream is = req.getInputStream(); baos = new ByteArrayOutputStream(); byte buf[] = new byte[1024]; int letti; while ((letti = is.read(buf)) > 0) { baos.write(buf, 0, letti); } buffer = baos.toByteArray(); }
private void level2level3catalog( RadarType radarType, String pathInfo, PrintWriter pw, HttpServletRequest req, HttpServletResponse res) throws IOException { try { String type; if (pathInfo.contains("level2")) type = radarType.toString() + "/level2"; else type = radarType.toString() + "/level3"; ByteArrayOutputStream os = new ByteArrayOutputStream(10000); InvCatalogFactory factory = InvCatalogFactory.getDefaultFactory(false); factory.writeXML(cat, os, true); InvCatalogImpl tCat = factory.readXML(new ByteArrayInputStream(os.toByteArray()), catURI); Iterator parents = tCat.getDatasets().iterator(); while (parents.hasNext()) { ArrayList<InvDatasetImpl> delete = new ArrayList<InvDatasetImpl>(); InvDatasetImpl top = (InvDatasetImpl) parents.next(); Iterator tDatasets = top.getDatasets().iterator(); while (tDatasets.hasNext()) { InvDatasetImpl ds = (InvDatasetImpl) tDatasets.next(); if (ds instanceof InvDatasetScan) { InvDatasetScan ids = (InvDatasetScan) ds; if (ids.getPath() == null) continue; if (ids.getPath().contains(type)) { ids.setXlinkHref(ids.getPath() + "/dataset.xml"); } else { delete.add(ds); } } } // remove datasets for (InvDatasetImpl idi : delete) { top.removeDataset(idi); } } if (pathInfo.endsWith("xml")) { String catAsString = factory.writeXML(tCat); pw.println(catAsString); pw.flush(); } else { HtmlWriter.getInstance().writeCatalog(req, res, tCat, true); // show catalog as HTML } } catch (Throwable e) { log.error("RadarServer.level2level3catalog failed", e); if (!res.isCommitted()) res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } return; }
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, java.io.IOException { String p = req.getParameter("r"); if (p == null || (p = p.trim()).equals("")) { String pi = req.getPathInfo(); return; } byte[] cont = res2cont.get(p); if (cont != null) { resp.setContentType(Mime.getContentType(p)); ServletOutputStream os = resp.getOutputStream(); os.write(cont); os.flush(); return; } InputStream is = null; try { is = appCL.getResourceAsStream(p); // getInputStreamByPath(p) ; if (is == null) { is = new Object().getClass().getResourceAsStream(p); if (is == null) return; } byte[] buf = new byte[1024]; ByteArrayOutputStream baos = new ByteArrayOutputStream(); int len; while ((len = is.read(buf)) >= 0) { baos.write(buf, 0, len); } cont = baos.toByteArray(); res2cont.put(p, cont); resp.setContentType(Mime.getContentType(p)); ServletOutputStream os = resp.getOutputStream(); os.write(cont); os.flush(); return; } finally { if (is != null) is.close(); } }
Object evalScript( String script, StringBuffer scriptOutput, boolean captureOutErr, HttpServletRequest request, HttpServletResponse response) throws EvalError { // Create a PrintStream to capture output ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream pout = new PrintStream(baos); // Create an interpreter instance with a null inputstream, // the capture out/err stream, non-interactive Interpreter bsh = new Interpreter(null, pout, pout, false); // set up interpreter bsh.set("bsh.httpServletRequest", request); bsh.set("bsh.httpServletResponse", response); // Eval the text, gathering the return value or any error. Object result = null; String error = null; PrintStream sout = System.out; PrintStream serr = System.err; if (captureOutErr) { System.setOut(pout); System.setErr(pout); } try { // Eval the user text result = bsh.eval(script); } finally { if (captureOutErr) { System.setOut(sout); System.setErr(serr); } } pout.flush(); scriptOutput.append(baos.toString()); return result; }
/** * ************************************************************************ Sends an error to the * client. * * @param t The exception that caused the problem. * @param res The <code>HttpServletResponse</code> for the client. */ public static void handleException(Throwable t, HttpServletResponse res) { try { String message = t.getMessage(); if (message == null) message = "NULL message " + t.getClass().getName(); if (Debug.isSet("trustedMode")) { // security issue: only show stack if trusted ByteArrayOutputStream bs = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(bs); t.printStackTrace(ps); message = new String(bs.toByteArray()); } log.info( UsageLog.closingMessageForRequestContext( HttpServletResponse.SC_BAD_REQUEST, message.length())); log.error("handleException", t); t.printStackTrace(); // debugging - log.error not showing stack trace !! if (!res.isCommitted()) res.sendError(HttpServletResponse.SC_BAD_REQUEST, message); } catch (Throwable e) { log.error("handleException() had problem reporting Exception", e); t.printStackTrace(); } }
private String doEncrypt(ByteArrayOutputStream byteArrayOutputStream){ String byteString=new String(byteArrayOutputStream.toByteArray()); if(!StringUtils.isEmpty(byteString)){ log.info("Request : " + byteString); try { byteString= CryptionUtils.Decrypt(byteString); } catch (Exception e) { e.printStackTrace(); } log.info("decrypt "+byteString); } return byteString; }
private ObjectNode fileAsJson(String path, org.tmatesoft.svn.core.io.SVNRepository repository) throws SVNException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); SVNProperties prop = new SVNProperties(); repository.getFile(path, -1l, prop, baos); long size = repository.info(path, -1l).getSize(); boolean isBinary; String mimeType; String data = null; if (size > MAX_FILE_SIZE_CAN_BE_VIEWED) { isBinary = true; mimeType = "application/octet-stream"; } else { byte[] bytes = baos.toByteArray(); isBinary = FileUtil.isBinary(bytes); if (!isBinary) { data = new String(bytes); } mimeType = new Tika().detect(bytes, path); } String author = prop.getStringValue(SVNProperty.LAST_AUTHOR); User user = User.findByLoginId(author); ObjectNode result = Json.newObject(); result.put("type", "file"); result.put("revisionNo", prop.getStringValue(SVNProperty.COMMITTED_REVISION)); result.put("author", author); result.put("avatar", getAvatar(user)); result.put("userName", user.name); result.put("userLoginId", user.loginId); result.put("createdDate", prop.getStringValue(SVNProperty.COMMITTED_DATE)); result.put("size", size); result.put("isBinary", isBinary); result.put("mimeType", mimeType); result.put("data", data); return result; }
public void close() throws IOException { if (closed) { throw new IOException("This output stream has already been closed"); } gzipstream.finish(); byte[] bytes = baos.toByteArray(); response.addHeader("Content-Length", Integer.toString(bytes.length)); response.addHeader("Content-Encoding", "gzip"); output.write(bytes); output.flush(); output.close(); closed = true; }
@Override public String getPatch(String commitId) throws SVNException { // Prepare required arguments. SVNURL svnURL = SVNURL.fromFile(new File(getRepoPrefix() + ownerName + "/" + projectName)); long rev = Integer.parseInt(commitId); // Get diffClient. SVNClientManager clientManager = SVNClientManager.newInstance(); SVNDiffClient diffClient = clientManager.getDiffClient(); // Using diffClient, write the changes by commitId into // byteArrayOutputStream, as unified format. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); diffClient.doDiff( svnURL, null, SVNRevision.create(rev - 1), SVNRevision.create(rev), SVNDepth.INFINITY, true, byteArrayOutputStream); return byteArrayOutputStream.toString(); }
byte[] toByteArray() { return baos.toByteArray(); }
public void write(int param) throws IOException { baos.write(param); }