List of usage examples for org.apache.commons.httpclient HttpMethod getResponseBodyAsStream
public abstract InputStream getResponseBodyAsStream() throws IOException;
From source file:com.zimbra.cs.servlet.ZimbraServlet.java
public static void proxyServletRequest(HttpServletRequest req, HttpServletResponse resp, HttpMethod method, HttpState state) throws IOException, ServiceException { // create an HTTP client with the same cookies javax.servlet.http.Cookie cookies[] = req.getCookies(); String hostname = method.getURI().getHost(); boolean hasZMAuth = hasZimbraAuthCookie(state); if (cookies != null) { for (int i = 0; i < cookies.length; i++) { if (cookies[i].getName().equals(ZimbraCookie.COOKIE_ZM_AUTH_TOKEN) && hasZMAuth) continue; state.addCookie(/*from www. ja v a 2s .c om*/ new Cookie(hostname, cookies[i].getName(), cookies[i].getValue(), "/", null, false)); } } HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient(); if (state != null) client.setState(state); int hopcount = 0; for (Enumeration<?> enm = req.getHeaderNames(); enm.hasMoreElements();) { String hname = (String) enm.nextElement(), hlc = hname.toLowerCase(); if (hlc.equals("x-zimbra-hopcount")) try { hopcount = Math.max(Integer.parseInt(req.getHeader(hname)), 0); } catch (NumberFormatException e) { } else if (hlc.startsWith("x-") || hlc.startsWith("content-") || hlc.equals("authorization")) method.addRequestHeader(hname, req.getHeader(hname)); } if (hopcount >= MAX_PROXY_HOPCOUNT) throw ServiceException.TOO_MANY_HOPS(HttpUtil.getFullRequestURL(req)); method.addRequestHeader("X-Zimbra-Hopcount", Integer.toString(hopcount + 1)); if (method.getRequestHeader("X-Zimbra-Orig-Url") == null) method.addRequestHeader("X-Zimbra-Orig-Url", req.getRequestURL().toString()); String ua = req.getHeader("User-Agent"); if (ua != null) method.setRequestHeader("User-Agent", ua); // dispatch the request and copy over the results int statusCode = -1; for (int retryCount = 3; statusCode == -1 && retryCount > 0; retryCount--) { statusCode = HttpClientUtil.executeMethod(client, method); } if (statusCode == -1) { resp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "retry limit reached"); return; } else if (statusCode >= 300) { resp.sendError(statusCode, method.getStatusText()); return; } Header[] headers = method.getResponseHeaders(); for (int i = 0; i < headers.length; i++) { String hname = headers[i].getName(), hlc = hname.toLowerCase(); if (hlc.startsWith("x-") || hlc.startsWith("content-") || hlc.startsWith("www-")) resp.addHeader(hname, headers[i].getValue()); } InputStream responseStream = method.getResponseBodyAsStream(); if (responseStream == null || resp.getOutputStream() == null) return; ByteUtil.copy(method.getResponseBodyAsStream(), false, resp.getOutputStream(), false); }
From source file:it.drwolf.ridire.session.CrawlerManager.java
@Restrict("#{identity.loggedIn}") public List<String> getJobSeeds(String profileName, String jobName, User currentUser) throws HeritrixException { List<String> seeds = new ArrayList<String>(); HttpMethod method = null; String cxml = "crawler-beans.cxml"; try {/*from ww w . jav a2 s . c o m*/ this.updateJobsList(currentUser); String filename = null; Job j = this.getPersistedJob(jobName); if (j != null) { if (j.getChildJobName() != null) { filename = j.getChildJobName(); } else { filename = j.getName(); } } else { filename = profileName; cxml = "profile-crawler-beans.cxml"; } if (j != null) { String[] s = j.getSeeds().split("\n"); for (int i = 0; i < s.length; i++) { seeds.add(s[i].trim()); } } else { method = new GetMethod( this.engineUri + "job/" + URLEncoder.encode(filename, "UTF-8") + "/jobdir/" + cxml); int status = this.httpClient.executeMethod(method); SAXReader saxReader = new SAXReader(); Document d = saxReader.read(method.getResponseBodyAsStream()); method.releaseConnection(); Element seedsElement = (Element) d.selectSingleNode( "//*[name()='bean' and @id='longerOverrides']/*[name()='property']/*[name()='props']/*[name()='prop']"); if (seedsElement != null) { String seedsText = seedsElement.getText(); StringTokenizer stringTokenizer = new StringTokenizer(seedsText, "\n"); while (stringTokenizer.hasMoreTokens()) { String t = stringTokenizer.nextToken().trim(); if (!t.startsWith("#")) { seeds.add(t); } } } } } catch (IOException e) { e.printStackTrace(); throw new HeritrixException(); } catch (DocumentException e) { e.printStackTrace(); throw new HeritrixException(); } finally { if (method != null) { method.releaseConnection(); } } return seeds; }
From source file:com.cloudbees.api.BeesClient.java
/** * Executes an HTTP method.// w w w .jav a 2s .c o m */ private HttpReply executeRequest(HttpMethod httpMethod, Map<String, String> headers) throws IOException { BeesClientConfiguration conf = getBeesClientConfiguration(); HttpClient httpClient = HttpClientHelper.createClient(conf); if (encodedAccountAuthorization != null) httpMethod.setRequestHeader("Authorization", encodedAccountAuthorization); if (headers != null) { for (Map.Entry<String, String> entry : headers.entrySet()) { httpMethod.setRequestHeader(entry.getKey(), entry.getValue()); } } int status = 500; String rsp = "Error"; try { status = httpClient.executeMethod(httpMethod); rsp = IOUtils.toString(httpMethod.getResponseBodyAsStream()); } catch (IOException e) { throw (IOException) new IOException("Failed to " + httpMethod.getName() + " : " + httpMethod.getURI() + " : code=" + status + " response=" + e.getMessage()).initCause(e); } finally { httpMethod.releaseConnection(); } trace(status + ": " + rsp); return new HttpReply(httpMethod, status, rsp); }
From source file:com.sun.faban.driver.transport.hc3.ApacheHC3Transport.java
/** * Fetch the response. If it is gzipped, unzip it. Checking encoding to * ensure data is read correctly//from w w w . j a v a2s. c o m * @param method * @return text response * @throws IOException */ private StringBuilder fetchResponse(HttpMethod method) throws IOException { Header contentTypeHdr = method.getResponseHeader("content-type"); String contentType = null; if (contentTypeHdr != null) contentType = contentTypeHdr.getValue(); String hdr = "charset="; int hdrLen = hdr.length(); String encoding = "ISO-8859-1"; if (contentType != null) { StringTokenizer t = new StringTokenizer(contentType, ";"); contentType = t.nextToken().trim(); while (t.hasMoreTokens()) { String param = t.nextToken().trim(); if (param.startsWith(hdr)) { encoding = param.substring(hdrLen); break; } } } Header contentEncodingHdr = method.getResponseHeader("content-encoding"); String contentEncoding = null; boolean isGzip = false; if (contentEncodingHdr != null) { contentEncoding = contentEncodingHdr.getValue(); if ("gzip".matches(contentEncoding.toLowerCase())) isGzip = true; else throw new IOException("cannot handle content-encoding " + contentEncoding); } if (contentType != null && (contentType.startsWith("text/") || texttypes.contains(contentType))) { InputStream is = method.getResponseBodyAsStream(); if (is != null) { Reader reader; if (isGzip) { reader = new InputStreamReader(new GZIPInputStream(is), encoding); } else { reader = new InputStreamReader(is, encoding); } // We have to close the input stream in order to return it to // the cache, so we get it for all content, even if we don't // use it. It's (I believe) a bug that the content handlers // used by getContent() don't close the input stream, but the // JDK team has marked those bugs as "will not fix." fetchResponseData(reader); reader.close(); } else { reInitBuffer(2048); // Ensure we have an empty buffer. } return charBuffer; } readResponse(method); return null; }
From source file:ch.ksfx.web.services.spidering.http.WebEngine.java
public void loadResource(Resource resource) { HttpMethod httpMethod; try {//from w w w . j a va2 s.co m if (/*resource.getHttpMethod().equals(GET)*/true) { String url = resource.getUrl(); if (url != null) { url = url.replaceAll("&", "&"); url = url.replaceAll(""", "\""); } httpMethod = this.httpClientHelper.executeGetMethod(url); } else { //TODO implement POST functionality /* NameValuePair[] nameValuePairs = new NameValuePair[resource.getPostData().size()]; for(int i = 0; i < resource.getPostData().size(); i++) { nameValuePairs[i] = new NameValuePair(resource.getPostData().get(i).getName(), resource.getPostData().get(i).getValue()); } String url = resource.getURL().toString(); if (url != null) { url = url.replaceAll("&","&"); url = url.replaceAll(""","\""); } httpMethod = this.httpClientHelper.executePostMethod(url, nameValuePairs); */ } } catch (Exception e) { resource.setLoadSucceed(false); resource.setHttpStatusCode(222); logger.log(Level.SEVERE, "Unable to load resource", e); return; } if (httpMethod == null) { resource.setLoadSucceed(false); return; } if (httpMethod.getStatusCode() != HttpStatus.SC_OK) { try { resource.setUrl(httpMethod.getURI().toString()); for (Header header : httpMethod.getResponseHeaders()) { resource.addResponseHeader(new ResponseHeader(header.getName(), header.getValue())); } resource.setHttpStatusCode(httpMethod.getStatusCode()); } catch (Exception e) { logger.warning(e.getMessage()); e.printStackTrace(); } return; } try { if (httpMethod.getResponseHeader("Content-Encoding") != null && httpMethod.getResponseHeader("Content-Encoding").getValue().contains("gzip")) { BufferedInputStream in = new BufferedInputStream( new GZIPInputStream(httpMethod.getResponseBodyAsStream())); ByteArrayOutputStream out = new ByteArrayOutputStream(1024); byte[] buffer = new byte[1024]; int length; while ((length = in.read(buffer)) >= 0) { out.write(buffer, 0, length); } resource.setRawContent(out.toByteArray()); resource.setSize(new Long(out.toByteArray().length)); } else { BufferedInputStream in = new BufferedInputStream(httpMethod.getResponseBodyAsStream()); ByteArrayOutputStream out = new ByteArrayOutputStream(1024); byte[] buffer = new byte[1024]; int length; while ((length = in.read(buffer)) >= 0) { out.write(buffer, 0, length); } resource.setRawContent(out.toByteArray()); resource.setSize(new Long(out.toByteArray().length)); } resource.setHttpStatusCode(httpMethod.getStatusCode()); resource.setLoadSucceed(true); resource.setMimeType(recognizeMimeType(resource, httpMethod)); if (resource.getMimeType().startsWith("text") || resource.getMimeType().equals("application/json")) { resource.setContent(EncodingHelper.encode(resource.getRawContent(), resource.getEncoding(), ((HttpMethodBase) httpMethod).getResponseCharSet())); } else { resource.setIsBinary(true); } } catch (IOException e) { e.printStackTrace(); logger.log(Level.SEVERE, "Unable to load resource", e); } finally { httpMethod.releaseConnection(); } }
From source file:itslearning.platform.restapi.sdk.learningtoolapp.LearningObjectServicetRestClient.java
public LearningObjectInstance getLearningObjectInstance(int instanceId, int learningObjectId) throws Exception { String uri = String.format(_baseUri + "/LearningObjectService.svc/learningObjects/%s/instances/%s", learningObjectId, instanceId); HttpMethod method = getInitializedHttpMethod(_httpClient, uri, HttpMethodType.GET); LearningObjectInstance loi = null;//from w ww .ja va 2 s.co m try { int statusCode = _httpClient.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { throw new HTTPException(statusCode); } else { loi = deserializeXMLToLearningObjectInstance(method.getResponseBodyAsStream()); } } catch (Exception ex) { ExceptionHandler.handle(ex); } finally { method.releaseConnection(); } return loi; }
From source file:itslearning.platform.restapi.sdk.learningtoolapp.LearningObjectServicetRestClient.java
public int getLearningObjectInstanceUserReportsCount(int instanceId, int learningObjectId) throws Exception { String uri = String.format( _baseUri + "/LearningObjectService.svc/learningObjects/%s/instances/%s/Reports/count", learningObjectId, instanceId); HttpMethod method = getInitializedHttpMethod(_httpClient, uri, HttpMethodType.GET); int reportsCount = 0; try {/*from w ww . j av a 2 s . c o m*/ int statusCode = _httpClient.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { throw new HTTPException(statusCode); } else { SAXReader reader = new SAXReader(); Document doc = reader.read(method.getResponseBodyAsStream()); reportsCount = Integer.parseInt(doc.getRootElement().getText()); } } catch (Exception ex) { ExceptionHandler.handle(ex); } finally { method.releaseConnection(); } return reportsCount; }
From source file:itslearning.platform.restapi.sdk.learningtoolapp.LearningObjectServicetRestClient.java
public int getLearningObjectInstanceUsersCount(int instanceId, int learningObjectId, int[] userIds, boolean includeTeachers) throws Exception { String uri = String.format( _baseUri + "/LearningObjectService.svc/learningObjects/%s/instances/%s/Users/count", learningObjectId, instanceId); uri = appendLearningObjectInstanceUsersExtraParameters(uri, userIds, includeTeachers); HttpMethod method = getInitializedHttpMethod(_httpClient, uri, HttpMethodType.GET); int usersCount = 0; try {/*from ww w. j a va 2 s . c o m*/ int statusCode = _httpClient.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { throw new HTTPException(statusCode); } else { SAXReader reader = new SAXReader(); Document doc = reader.read(method.getResponseBodyAsStream()); usersCount = Integer.parseInt(doc.getRootElement().getText()); } } catch (Exception ex) { ExceptionHandler.handle(ex); } finally { method.releaseConnection(); } return usersCount; }
From source file:itslearning.platform.restapi.sdk.learningtoolapp.LearningObjectServicetRestClient.java
public List<Assessment> getPossibleAssessments(int instanceId, int learningObjectId) throws Exception { String uri = String.format( _baseUri + "/LearningObjectService.svc/learningObjects/%s/instances/%s/PossibleAssessments", learningObjectId, instanceId); HttpMethod method = getInitializedHttpMethod(_httpClient, uri, HttpMethodType.GET); List<Assessment> assessments = new ArrayList<Assessment>(); try {//www . j av a 2 s .com int statusCode = _httpClient.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { throw new HTTPException(statusCode); } else { assessments = deserializeXMLToListOfAssessments(method.getResponseBodyAsStream()); } } catch (Exception ex) { ExceptionHandler.handle(ex); } finally { method.releaseConnection(); } return assessments; }
From source file:itslearning.platform.restapi.sdk.learningtoolapp.LearningObjectServicetRestClient.java
public List<AssessmentItem> getAssessmentItems(int instanceId, int learningObjectId) throws Exception { String uri = String.format( _baseUri + "/LearningObjectService.svc/learningObjects/%s/instances/%s/AssessmentItems", learningObjectId, instanceId); HttpMethod method = getInitializedHttpMethod(_httpClient, uri, HttpMethodType.GET); List<AssessmentItem> assessmentItems = new ArrayList<AssessmentItem>(); try {//from w ww . j a v a 2s . c o m int statusCode = _httpClient.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { throw new HTTPException(statusCode); } else { assessmentItems = deserializeXMLToListOfAssessmentItems(method.getResponseBodyAsStream()); } } catch (Exception ex) { ExceptionHandler.handle(ex); } finally { method.releaseConnection(); } return assessmentItems; }