List of usage examples for org.apache.commons.httpclient HttpMethodBase getResponseBodyAsStream
@Override public InputStream getResponseBodyAsStream() throws IOException
From source file:edu.unc.lib.dl.ui.util.FileIOUtil.java
public static void stream(OutputStream outStream, HttpMethodBase method) throws IOException { try (InputStream in = method.getResponseBodyAsStream(); BufferedInputStream reader = new BufferedInputStream(in)) { byte[] buffer = new byte[4096]; int count = 0; int length = 0; while ((length = reader.read(buffer)) >= 0) { try { outStream.write(buffer, 0, length); if (count++ % 5 == 0) { outStream.flush();/*www . j a v a 2 s.c o m*/ } } catch (IOException e) { // Differentiate between socket being closed when writing vs // reading throw new ClientAbortException(e); } } try { outStream.flush(); } catch (IOException e) { throw new ClientAbortException(e); } } }
From source file:com.buzzdavidson.spork.util.XmlUtils.java
/** * Reads an XML Document from the given HttpMethodBase's response stream. * @param method method from which to read document * @return XML Document/*from ww w . ja v a2 s. co m*/ */ public static Document readResponseDocument(HttpMethodBase method) { try { return readDocument(method.getResponseBodyAsStream()); } catch (Exception ex) { throw new RuntimeException("Failed to read XML document", ex); } }
From source file:com.google.gsa.valve.modules.utils.HTTPAuthZProcessor.java
/** * If the document is non HTML, this method processes its content in order to * rewrite the URLs it includes/*from ww w . ja v a2 s .co m*/ * * @param response * @param method * @throws IOException */ public static void processNonHTML(HttpServletResponse response, HttpMethodBase method) throws IOException { logger.debug("Processing a non HTML document"); InputStream is = new BufferedInputStream(method.getResponseBodyAsStream()); //HTTP Output OutputStream os = response.getOutputStream(); byte[] buffer = new byte[BUFFER_BLOCK_SIZE]; int read = is.read(buffer); while (read >= 0) { if (read > 0) { os.write(buffer, 0, read); } read = is.read(buffer); } //protection buffer = null; is.close(); os.close(); }
From source file:com.google.gsa.valve.modules.utils.HTTPAuthZProcessor.java
/** * If the document is HTML, this method processes its content in order to * rewrite the URLs it includes//www.j a va 2 s. co m * * @param response HTTP response * @param method HTTP method * @param url document url * @param loginUrl login url * @param contenType content Type * * @throws IOException * @throws ParserException */ public static void processHTML(HttpServletResponse response, HttpMethodBase method, String url, String loginUrl, String contentType) throws IOException, ParserException { logger.debug("Processing an HTML document"); String stream = null; Parser parser = null; NodeVisitor visitor = null; // Retrieve HTML stream stream = readFully(new InputStreamReader(method.getResponseBodyAsStream())); // Protection if (stream != null) { logger.debug("Stream content size: " + stream.length()); // Parse HTML stream to replace any links to include the path to the valve parser = Parser.createParser(stream, null); // Instantiate visitor visitor = new HTTPVisitor(url, loginUrl); // Parse nodes parser.visitAllNodesWith(visitor); // Get writer PrintWriter out = response.getWriter(); // Push HTML content if (out != null) { out.flush(); out.print(((HTTPVisitor) visitor).getModifiedHTML()); out.close(); logger.debug("Wrote: " + ((HTTPVisitor) visitor).getModifiedHTML().length()); } response.setHeader("Content-Type", contentType); // Garbagge collect stream = null; parser = null; visitor = null; } }
From source file:cz.muni.fi.pa165.creatures.rest.client.services.utils.CRUDServiceHelper.java
/** * Helper methods which executes an HTTP {@code method} and writes the * output to the standard output (e.g. console). Return codes of the HTTP * responses are present so we can verify what happened at the server side. * * @param method method to send to the server side *///from www .ja va 2s .co m public static void send(HttpMethodBase method) { HttpClient httpClient = new HttpClient(); try { int result = httpClient.executeMethod(method); System.out.println(RESPONSE_STATUS_CODE + result); ResponseCode response = ResponseCode.fromInt(result); if (response != ResponseCode.NOT_FOUND && response != ResponseCode.SERVER_ERROR && response != ResponseCode.FORBIDDEN) { System.out.println(RESPONSE_HEADER); Header[] headers = method.getResponseHeaders(); for (Header h : headers) { System.out.println(h.toString()); } InputStreamReader isr = new InputStreamReader(method.getResponseBodyAsStream()); BufferedReader br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) { System.out.println(line); } } } catch (ConnectException ex) { logger.log(Level.WARNING, CONNECTION_EXCEPTION_MSG); } catch (IOException ex) { logger.log(Level.INFO, ex.getMessage()); } finally { method.releaseConnection(); } }
From source file:edu.unc.lib.dl.cdr.sword.server.MethodAwareInputStream.java
public MethodAwareInputStream(HttpMethodBase method) throws IOException { this.originalStream = method.getResponseBodyAsStream(); this.method = method; }
From source file:de.michaeltamm.W3cMarkupValidator.java
private String getResponseBody(final HttpMethodBase httpMethod) throws IOException { final InputStream in = httpMethod.getResponseBodyAsStream(); try {/*from w w w. j a va 2 s . co m*/ final ByteArrayOutputStream out = new ByteArrayOutputStream(1000000); final byte[] buffer = new byte[4096]; int n; while ((n = in.read(buffer)) > 0) { out.write(buffer, 0, n); } return out.toString(httpMethod.getResponseCharSet()); } finally { try { in.close(); } catch (IOException ignored) { } } }
From source file:com.springsource.hq.plugin.tcserver.cli.client.configuration.FileResponseHandler.java
public T handleResponse(int responseCode, HttpMethodBase method) throws IOException { switch (responseCode) { case 200://from w ww . j ava 2s . c o m FileOutputStream fileOutputStream = null; InputStream in = method.getResponseBodyAsStream(); try { fileOutputStream = new FileOutputStream(targetFile.getAbsolutePath()); final byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { fileOutputStream.write(buf, 0, len); } return getSuccessResponse(); } catch (Exception e) { return getErrorResponse(createServiceError("UnexpectedError", "Unable to deserialize result")); } finally { if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e) { } } } case 401: // Unauthorized return getErrorResponse( createServiceError("LoginFailure", "The given username and password could not be validated")); default: String reasonText; if (method.getStatusText() != null) { reasonText = method.getStatusText(); } else { reasonText = "An unexpected error occurred"; } return getErrorResponse(createServiceError("UnexpectedError", reasonText)); } }
From source file:com.honnix.cheater.validator.ContentValidator.java
private boolean isValidContent(HttpMethodBase method, String keyString) { boolean result = false; String charSet = method.getResponseCharSet(); try {/*from ww w . j a va 2 s.com*/ BufferedReader br = new BufferedReader( new InputStreamReader(method.getResponseBodyAsStream(), charSet)); String line = null; while (!result && (line = br.readLine()) != null) { if (line.indexOf(keyString) != -1) { result = true; } } } catch (IOException e) { result = false; } return result; }
From source file:com.github.rnewson.couchdb.lucene.Database.java
private synchronized String execute(final HttpMethodBase method) throws HttpException, IOException { try {//from w w w . j a va2 s.c om final int sc = CLIENT.executeMethod(method); if (sc == 401) { throw new HttpException("Unauthorized."); } final InputStream in = method.getResponseBodyAsStream(); try { final StringWriter writer = new StringWriter(2048); IOUtils.copy(in, writer, method.getResponseCharSet()); return writer.toString(); } finally { in.close(); } } finally { method.releaseConnection(); } }