List of usage examples for org.apache.commons.httpclient HttpMethod getResponseBodyAsStream
public abstract InputStream getResponseBodyAsStream() throws IOException;
From source file:org.eclipsetrader.yahoojapanfx.internal.core.connector.SnapshotConnector.java
protected void fetchLatestSnapshot(HttpClient client, String[] symbols) { try {//from w ww.j av a 2 s.c o m HttpMethod method = Util.getSnapshotFeedMethod(streamingServer); BufferedReader in = null; try { client.executeMethod(method); if ((method.getResponseHeader("Content-Encoding") != null) && (method.getResponseHeader("Content-Encoding").getValue().equals("gzip"))) { in = new BufferedReader( new InputStreamReader(new GZIPInputStream(method.getResponseBodyAsStream()))); } else { in = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream())); } StringBuilder sb = new StringBuilder(1000); String inputLine; while ((inputLine = in.readLine()) != null) { sb.append(inputLine); } Calendar c = Calendar.getInstance(); String year = String.valueOf(c.get(Calendar.YEAR)); JSONObject obj = new JSONObject(sb.toString()); JSONObject arate = obj.getJSONObject("rate"); String date = obj.getString("date"); // String company = obj.getString("company"); for (int i = 0; i < symbols.length; i++) { String symbol = (symbols[i].length() > 6 ? symbols[i].substring(0, 6) : symbols[i]); if (arate.has(symbol)) { JSONObject o = arate.getJSONObject(symbol); processSnapshotData(symbols[i], o, year + "/" + date); } } } catch (Exception e) { Status status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, 0, "Error reading snapshot data", e); //$NON-NLS-1$ Activator.log(status); } finally { try { if (in != null) { in.close(); } if (method != null) { method.releaseConnection(); } } catch (Exception e) { Status status = new Status(IStatus.WARNING, Activator.PLUGIN_ID, 0, "Connection wasn't closed cleanly", e); Activator.log(status); } } wakeupNotifyThread(); } catch (Exception e) { Activator.log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, 0, "Error reading snapshot data", e)); //$NON-NLS-1$ } }
From source file:org.exist.xquery.modules.httpclient.BaseHTTPClientFunction.java
/** * Takes the HTTP Response Body from the HTTP Method and attempts to insert it into the response tree we are building. * * <p>Conversion Preference - 1) Try and parse as XML, if successful returns a Node 2) Try and parse as HTML returning as XML compatible HTML, if * successful returns a Node 3) Return as base64Binary encoded data</p> * * @param context The context of the calling XQuery * @param method The HTTP Request Method * @param builder The MemTreeBuilder that is being used * * @throws IOException /*w w w. j a v a2 s. com*/ * @throws XPathException */ private void insertResponseBody(final XQueryContext context, final HttpMethod method, final MemTreeBuilder builder, final Map<String, Boolean> parserFeatures, final Map<String, String> parserProperties) throws IOException, XPathException { NodeImpl responseNode = null; final InputStream bodyAsStream = method.getResponseBodyAsStream(); // check if there is a response body if (bodyAsStream != null) { CachingFilterInputStream cfis = null; FilterInputStreamCache cache = null; try { //we have to cache the input stream, so we can reread it, as we may use it twice (once for xml attempt and once for string attempt) cache = FilterInputStreamCacheFactory .getCacheInstance(new FilterInputStreamCacheFactory.FilterInputStreamCacheConfiguration() { @Override public String getCacheClass() { return (String) context.getBroker().getConfiguration() .getProperty(Configuration.BINARY_CACHE_CLASS_PROPERTY); } }); cfis = new CachingFilterInputStream(cache, bodyAsStream); //mark the start of the stream cfis.mark(Integer.MAX_VALUE); // determine the type of the response document final Header responseContentType = method.getResponseHeader("Content-Type"); final MimeType responseMimeType = getResponseMimeType(responseContentType); if (responseContentType != null) { builder.addAttribute(new QName("mimetype", null, null), responseContentType.getValue()); } //try and parse the response as XML try { //we have to use CloseShieldInputStream otherwise the parser closes the stream and we cant later reread final InputStream shieldedInputStream = new CloseShieldInputStream(cfis); responseNode = (NodeImpl) ModuleUtils.streamToXML(context, shieldedInputStream); builder.addAttribute(new QName("type", null, null), "xml"); responseNode.copyTo(null, new DocumentBuilderReceiver(builder)); } catch (final SAXException se) { // could not parse to xml // not an error in itself, it will be treated either as HTML, // text or binary here below final String msg = "Request for URI '" + method.getURI().toString() + "' Could not parse http response content as XML (will try html, text or fallback to binary): " + se.getMessage(); if (logger.isDebugEnabled()) { logger.debug(msg, se); } else { logger.info(msg); } } catch (final IOException ioe) { final String msg = "Request for URI '" + method.getURI().toString() + "' Could not read http response content: " + ioe.getMessage(); logger.error(msg, ioe); throw new XPathException(msg, ioe); } if (responseNode == null) { //response is NOT parseable as XML //is it a html document? if (responseMimeType.getName().equals(MimeType.HTML_TYPE.getName())) { //html document try { //reset the stream to the start, as we need to reuse since attempting to parse to XML cfis.reset(); //parse html to xml(html) //we have to use CloseShieldInputStream otherwise the parser closes the stream and we cant later reread final InputStream shieldedInputStream = new CloseShieldInputStream(cfis); responseNode = (NodeImpl) ModuleUtils .htmlToXHtml(context, method.getURI().toString(), new InputSource(shieldedInputStream), parserFeatures, parserProperties) .getDocumentElement(); builder.addAttribute(new QName("type", null, null), "xhtml"); responseNode.copyTo(null, new DocumentBuilderReceiver(builder)); } catch (final URIException ue) { throw new XPathException(this, ue.getMessage(), ue); } catch (final SAXException se) { //could not parse to xml(html) logger.debug( "Could not parse http response content from HTML to XML: " + se.getMessage(), se); } } } if (responseNode == null) { //reset the stream to the start, as we need to reuse since attempting to parse to HTML->XML cfis.reset(); if (responseMimeType.getName().startsWith("text/")) { // Assume it's a text body and URL encode it builder.addAttribute(new QName("type", null, null), "text"); builder.addAttribute(new QName("encoding", null, null), "URLEncoded"); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final byte buf[] = new byte[4096]; int read = -1; while ((read = cfis.read(buf)) > -1) { baos.write(buf); } builder.characters(URLEncoder.encode(EncodingUtil.getString(baos.toByteArray(), ((HttpMethodBase) method).getResponseCharSet()), "UTF-8")); baos.close(); } else { // Assume it's a binary body and Base64 encode it builder.addAttribute(new QName("type", null, null), "binary"); builder.addAttribute(new QName("encoding", null, null), "Base64Encoded"); BinaryValue binary = null; try { binary = BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), cfis); builder.characters(binary.getStringValue()); } finally { // free resources if (binary != null) { binary.destroy(context, null); } } } } } finally { if (cache != null) { try { cache.invalidate(); } catch (final IOException ioe) { LOG.error(ioe.getMessage(), ioe); } } if (cfis != null) { try { cfis.close(); } catch (final IOException ioe) { LOG.error(ioe.getMessage(), ioe); } } } } }
From source file:org.gbif.portal.web.download.OpenModellerFileWriter.java
/** * 1) Creates the model - polling for progress * 2) Projects the moodel - polling fro progres * 3) Downloads the generated image to a temp file * //from w w w. j a v a 2 s .c o m * @see org.gbif.portal.web.download.FileWriter#writeFile() */ @Override public void writeFile() throws Exception { OmWSClientProxy omClient = new OmWSClientProxy(); omClient.setOmService(openModellerEndpoint); String soapRequest = omClient.omwsSOAPRequestUtil.getCreateModelRequest(omModel); if (logger.isDebugEnabled()) { logger.debug("Open Modeller request: " + soapRequest); } String displayRequest = omClient.omwsSOAPRequestUtil.cleanup(soapRequest, false); String omwsResponse = omClient.executeOMwsRequest(soapRequest); if (logger.isDebugEnabled()) { logger.debug("Open Modeller response: " + omwsResponse); } omClient.omwsSOAPRequestUtil.parseXMLResponseSAX(omwsResponse); OmWSSAXParser omwsSAXParser = omClient.omwsSOAPRequestUtil.getOMwsSAXParser(); String ticket = omwsSAXParser.getJobResponse(); boolean success = pollforTicket(omClient, ticket); if (!success) { logger.error("Model Generation Timeout at Create Model Stage !!!!!"); signalFileWriteFailure(); return; } //retrieve model logger.debug("Show project model request..."); omwsResponse = omClient.getModel(ticket); omClient.omwsSOAPRequestUtil.parseXMLResponseSAX(omwsResponse); omwsSAXParser = omClient.omwsSOAPRequestUtil.getOMwsSAXParser(); String modelParams = omwsSAXParser.getModelParams(); omModel.setModelParams(modelParams); soapRequest = omClient.omwsSOAPRequestUtil.getProjectModelRequest(omModel); omClient.omwsSOAPRequestUtil.cleanup(soapRequest, false); omwsResponse = omClient.executeOMwsRequest(soapRequest); omClient.omwsSOAPRequestUtil.parseXMLResponseSAX(omwsResponse); omwsSAXParser = omClient.omwsSOAPRequestUtil.getOMwsSAXParser(); ticket = omwsSAXParser.getJobResponse(); if (logger.isDebugEnabled()) { logger.debug("Ticket: " + ticket); } //poll for ticket success = pollforTicket(omClient, ticket); if (!success) { logger.error("Model Generation Timeout at Project Model Stage !!!!!"); signalFileWriteFailure(); return; } String urlStr = omClient.getLayerAsUrl(ticket); if (logger.isDebugEnabled()) { logger.debug("Generated IMG =" + urlStr); } urlStr = urlStr.substring(0, urlStr.length() - 4) + imgFileExtension; if (logger.isDebugEnabled()) { logger.debug("PNG url =" + urlStr); } HttpClient client = new HttpClient(); HttpMethod method = new GetMethod(urlStr); int state = client.executeMethod(method); logger.debug(state); InputStream in = method.getResponseBodyAsStream(); byte[] buffer = new byte[1000]; int bytesRead = in.read(buffer); while (bytesRead > 0) { outputStream.write(buffer, 0, bytesRead); bytesRead = in.read(buffer); } method.releaseConnection(); logger.debug("Image written to file"); signalFileWriteComplete(); }
From source file:org.geoserver.gss.HTTPGSSClient.java
/** * Executes the http method, checks the response status, parses the response and returns it. * Will throw an exception in case of communication errors, error codes, or service exceptions * /*from w ww .j a v a 2 s . c om*/ * @throws IOException */ Object executeMethod(HttpMethod method) throws IOException { Object response; try { if (LOGGER.isLoggable(Level.FINE)) { if (method instanceof GetMethod) { GetMethod gm = (GetMethod) method; LOGGER.fine("Sending GET request:\n " + method.getURI()); } else if (method instanceof PostMethod) { PostMethod pm = (PostMethod) method; XMLEntity entity = (XMLEntity) pm.getRequestEntity(); String request = entity.toString(); LOGGER.fine("Sending POST request:\n " + method.getURI() + "\n" + request); // ugly, but Encoder cannot be reused, so we have to set a new entity // that uses the already generated string pm.setRequestEntity(new StringRequestEntity(request, "text/xml", "UTF-8")); } else { LOGGER.fine("Sending unknown method type : " + method); } } // plain execution int statusCode = client.executeMethod(method); // check the HTTP status if (statusCode != HttpStatus.SC_OK) { throw new IOException("HTTP client returned with code " + statusCode); } // parse the response Parser parser = new Parser(configuration); parser.setStrict(true); parser.setFailOnValidationError(true); InputStream is; if (LOGGER.isLoggable(Level.FINE)) { String responseString = method.getResponseBodyAsString(); LOGGER.log(Level.FINE, "Response from Unit:\n" + responseString); is = new ByteArrayInputStream(responseString.getBytes()); } else { is = method.getResponseBodyAsStream(); } response = parser.parse(is); } catch (Exception e) { throw (IOException) new IOException("Error occurred while executing " + "a call to the Unit") .initCause(e); } finally { method.releaseConnection(); } // convert a service exception into an IOException if necessary if (response instanceof ExceptionReportType) { ExceptionReportType report = (ExceptionReportType) response; StringBuilder sb = new StringBuilder("The Unit service reported a failure: "); for (Iterator it = report.getException().iterator(); it.hasNext();) { ExceptionType et = (ExceptionType) it.next(); for (Iterator eit = et.getExceptionText().iterator(); eit.hasNext();) { String text = (String) eit.next(); sb.append(text); if (eit.hasNext()) sb.append(". "); } } throw new IOException(sb.toString()); } return response; }
From source file:org.geoserver.wps.Execute.java
/** * Executes// www . j a va 2s . com * * @param ref * @return */ Object executeRemoteRequest(InputReferenceType ref, ComplexPPIO ppio, String inputId) throws Exception { URL destination = new URL(ref.getHref()); HttpMethod method = null; GetMethod refMethod = null; InputStream input = null; InputStream refInput = null; // execute the request try { if ("http".equalsIgnoreCase(destination.getProtocol())) { // setup the client HttpClient client = new HttpClient(); // setting timeouts (30 seconds, TODO: make this configurable) HttpConnectionManagerParams params = new HttpConnectionManagerParams(); params.setSoTimeout(connectionTimeout); params.setConnectionTimeout(connectionTimeout); // TODO: make the http client a well behaved http client, no more than x connections // per server (x admin configurable maybe), persistent connections and so on HttpConnectionManager manager = new SimpleHttpConnectionManager(); manager.setParams(params); client.setHttpConnectionManager(manager); // prepare either a GET or a POST request if (ref.getMethod() == null || ref.getMethod() == MethodType.GET_LITERAL) { GetMethod get = new GetMethod(ref.getHref()); get.setFollowRedirects(true); method = get; } else { String encoding = ref.getEncoding(); if (encoding == null) { encoding = "UTF-8"; } PostMethod post = new PostMethod(ref.getHref()); Object body = ref.getBody(); if (body == null) { if (ref.getBodyReference() != null) { URL refDestination = new URL(ref.getBodyReference().getHref()); if ("http".equalsIgnoreCase(refDestination.getProtocol())) { // open with commons http client refMethod = new GetMethod(ref.getBodyReference().getHref()); refMethod.setFollowRedirects(true); client.executeMethod(refMethod); refInput = refMethod.getResponseBodyAsStream(); } else { // open with the built-in url management URLConnection conn = refDestination.openConnection(); conn.setConnectTimeout(connectionTimeout); conn.setReadTimeout(connectionTimeout); refInput = conn.getInputStream(); } post.setRequestEntity(new InputStreamRequestEntity(refInput, ppio.getMimeType())); } else { throw new WPSException("A POST request should contain a non empty body"); } } else if (body instanceof String) { post.setRequestEntity(new StringRequestEntity((String) body, ppio.getMimeType(), encoding)); } else { throw new WPSException("The request body should be contained in a CDATA section, " + "otherwise it will get parsed as XML instead of being preserved as is"); } method = post; } // add eventual extra headers if (ref.getHeader() != null) { for (Iterator it = ref.getHeader().iterator(); it.hasNext();) { HeaderType header = (HeaderType) it.next(); method.setRequestHeader(header.getKey(), header.getValue()); } } int code = client.executeMethod(method); if (code == 200) { input = method.getResponseBodyAsStream(); } else { throw new WPSException("Error getting remote resources from " + ref.getHref() + ", http error " + code + ": " + method.getStatusText()); } } else { // use the normal url connection methods then... URLConnection conn = destination.openConnection(); conn.setConnectTimeout(connectionTimeout); conn.setReadTimeout(connectionTimeout); input = conn.getInputStream(); } // actually parse teh data if (input != null) { return ppio.decode(input); } else { throw new WPSException("Could not find a mean to read input " + inputId); } } finally { // make sure to close the connection and streams no matter what if (input != null) { input.close(); } if (method != null) { method.releaseConnection(); } if (refMethod != null) { refMethod.releaseConnection(); } } }
From source file:org.geoserver.wps.executor.RemoteRequestInputProvider.java
@Override protected Object getValueInternal(ProgressListener listener) throws Exception { InputReferenceType ref = input.getReference(); URL destination = new URL(ref.getHref()); HttpMethod method = null; GetMethod refMethod = null;// www. ja v a 2s .com InputStream input = null; InputStream refInput = null; // execute the request listener.started(); try { if ("file".equalsIgnoreCase(destination.getProtocol())) { File file = DataUtilities.urlToFile(destination); if (maxSize > 0 && maxSize < file.length()) { throw new WPSException("Input " + getInputId() + " size " + file.length() + " exceeds maximum allowed size of " + maxSize, "NoApplicableCode", getInputId()); } input = new FileInputStream(file); } else if ("http".equalsIgnoreCase(destination.getProtocol())) { // setup the client HttpClient client = new HttpClient(); // setting timeouts (30 seconds, TODO: make this configurable) HttpConnectionManagerParams params = new HttpConnectionManagerParams(); params.setSoTimeout(timeout); params.setConnectionTimeout(timeout); // TODO: make the http client a well behaved http client, no more than x connections // per server (x admin configurable maybe), persistent connections and so on HttpConnectionManager manager = new SimpleHttpConnectionManager(); manager.setParams(params); client.setHttpConnectionManager(manager); // prepare either a GET or a POST request if (ref.getMethod() == null || ref.getMethod() == MethodType.GET_LITERAL) { GetMethod get = new GetMethod(ref.getHref()); get.setFollowRedirects(true); method = get; } else { String encoding = ref.getEncoding(); if (encoding == null) { encoding = "UTF-8"; } PostMethod post = new PostMethod(ref.getHref()); Object body = ref.getBody(); if (body == null) { if (ref.getBodyReference() != null) { URL refDestination = new URL(ref.getBodyReference().getHref()); if ("http".equalsIgnoreCase(refDestination.getProtocol())) { // open with commons http client refMethod = new GetMethod(ref.getBodyReference().getHref()); refMethod.setFollowRedirects(true); client.executeMethod(refMethod); refInput = refMethod.getResponseBodyAsStream(); } else { // open with the built-in url management URLConnection conn = refDestination.openConnection(); conn.setConnectTimeout(timeout); conn.setReadTimeout(timeout); refInput = conn.getInputStream(); } post.setRequestEntity( new InputStreamRequestEntity(refInput, complexPPIO.getMimeType())); } else { throw new WPSException("A POST request should contain a non empty body"); } } else if (body instanceof String) { post.setRequestEntity( new StringRequestEntity((String) body, complexPPIO.getMimeType(), encoding)); } else { throw new WPSException("The request body should be contained in a CDATA section, " + "otherwise it will get parsed as XML instead of being preserved as is"); } method = post; } // add eventual extra headers if (ref.getHeader() != null) { for (Iterator it = ref.getHeader().iterator(); it.hasNext();) { HeaderType header = (HeaderType) it.next(); method.setRequestHeader(header.getKey(), header.getValue()); } } int code = client.executeMethod(method); if (code == 200) { try { Header length = method.getResponseHeader("Content-Lenght"); if (maxSize > 0 && length != null && Long.parseLong(length.getValue()) > maxSize) { throw new WPSException( "Input " + getInputId() + " size " + length.getValue() + " exceeds maximum allowed size of " + maxSize + " according to HTTP Content-Lenght response header", "NoApplicableCode", getInputId()); } } catch (NumberFormatException e) { LOGGER.log(Level.FINE, "Failed to parse content lenght to check input limits respect, " + "moving on and checking data as it comes in", e); } input = method.getResponseBodyAsStream(); if (maxSize > 0) { input = new MaxSizeInputStream(input, getInputId(), maxSize); } } else { throw new WPSException("Error getting remote resources from " + ref.getHref() + ", http error " + code + ": " + method.getStatusText()); } } else { // use the normal url connection methods then... URLConnection conn = destination.openConnection(); conn.setConnectTimeout(timeout); conn.setReadTimeout(timeout); input = conn.getInputStream(); if (maxSize > 0) { input = new MaxSizeInputStream(input, getInputId(), maxSize); } } // actually parse the data if (input != null) { CancellingInputStream is = new CancellingInputStream(input, listener); return complexPPIO.decode(is); } else { throw new WPSException("Could not find a mean to read input " + inputId); } } finally { listener.progress(100); listener.complete(); // make sure to close the connection and streams no matter what if (refInput != null) { refInput.close(); } if (input != null) { input.close(); } if (method != null) { method.releaseConnection(); } if (refMethod != null) { refMethod.releaseConnection(); } } }
From source file:org.geoserver.wps.executor.SimpleInputProvider.java
/** * Executes/*from w w w. j a va 2s . com*/ * * @param ref * @return */ Object executeRemoteRequest(InputReferenceType ref, ComplexPPIO ppio, String inputId) throws Exception { URL destination = new URL(ref.getHref()); HttpMethod method = null; GetMethod refMethod = null; InputStream input = null; InputStream refInput = null; // execute the request try { if ("http".equalsIgnoreCase(destination.getProtocol())) { // setup the client HttpClient client = new HttpClient(); // setting timeouts (30 seconds, TODO: make this configurable) HttpConnectionManagerParams params = new HttpConnectionManagerParams(); params.setSoTimeout(executor.getConnectionTimeout()); params.setConnectionTimeout(executor.getConnectionTimeout()); // TODO: make the http client a well behaved http client, no more than x connections // per server (x admin configurable maybe), persistent connections and so on HttpConnectionManager manager = new SimpleHttpConnectionManager(); manager.setParams(params); client.setHttpConnectionManager(manager); // prepare either a GET or a POST request if (ref.getMethod() == null || ref.getMethod() == MethodType.GET_LITERAL) { GetMethod get = new GetMethod(ref.getHref()); get.setFollowRedirects(true); method = get; } else { String encoding = ref.getEncoding(); if (encoding == null) { encoding = "UTF-8"; } PostMethod post = new PostMethod(ref.getHref()); Object body = ref.getBody(); if (body == null) { if (ref.getBodyReference() != null) { URL refDestination = new URL(ref.getBodyReference().getHref()); if ("http".equalsIgnoreCase(refDestination.getProtocol())) { // open with commons http client refMethod = new GetMethod(ref.getBodyReference().getHref()); refMethod.setFollowRedirects(true); client.executeMethod(refMethod); refInput = refMethod.getResponseBodyAsStream(); } else { // open with the built-in url management URLConnection conn = refDestination.openConnection(); conn.setConnectTimeout(executor.getConnectionTimeout()); conn.setReadTimeout(executor.getConnectionTimeout()); refInput = conn.getInputStream(); } post.setRequestEntity(new InputStreamRequestEntity(refInput, ppio.getMimeType())); } else { throw new WPSException("A POST request should contain a non empty body"); } } else if (body instanceof String) { post.setRequestEntity(new StringRequestEntity((String) body, ppio.getMimeType(), encoding)); } else { throw new WPSException("The request body should be contained in a CDATA section, " + "otherwise it will get parsed as XML instead of being preserved as is"); } method = post; } // add eventual extra headers if (ref.getHeader() != null) { for (Iterator it = ref.getHeader().iterator(); it.hasNext();) { HeaderType header = (HeaderType) it.next(); method.setRequestHeader(header.getKey(), header.getValue()); } } int code = client.executeMethod(method); if (code == 200) { input = method.getResponseBodyAsStream(); } else { throw new WPSException("Error getting remote resources from " + ref.getHref() + ", http error " + code + ": " + method.getStatusText()); } } else { // use the normal url connection methods then... URLConnection conn = destination.openConnection(); conn.setConnectTimeout(executor.getConnectionTimeout()); conn.setReadTimeout(executor.getConnectionTimeout()); input = conn.getInputStream(); } // actually parse teh data if (input != null) { return ppio.decode(input); } else { throw new WPSException("Could not find a mean to read input " + inputId); } } finally { // make sure to close the connection and streams no matter what if (input != null) { input.close(); } if (method != null) { method.releaseConnection(); } if (refMethod != null) { refMethod.releaseConnection(); } } }
From source file:org.geotools.data.couchdb.client.CouchDBResponse.java
public CouchDBResponse(HttpMethod request, int result, IOException exception) throws IOException { this.request = request; this.result = result; this.exception = exception; boolean err = !isHttpOK(); InputStream response = request.getResponseBodyAsStream(); if (err) {//from w w w. jav a2 s. c om if (exception != null) { throw new IOException("HTTP error", exception); } if (response == null) { throw new IOException("HTTP error : " + result); } } json = JSONValue.parse(new InputStreamReader(request.getResponseBodyAsStream())); if (err) { isArray = false; } else { isArray = json instanceof JSONArray; } }
From source file:org.infoscoop.request.filter.ProxyFilterContainer.java
public final int invoke(HttpClient client, HttpMethod method, ProxyRequest request) throws Exception { int preStatus = prepareInvoke(client, method, request); switch (preStatus) { case 0://from w w w . j ava 2 s . c o m break; case EXECUTE_POST_STATUS: doFilterChain(request, request.getResponseBody()); default: return preStatus; } // copy headers sent target server List ignoreHeaderNames = request.getIgnoreHeaders(); List allowedHeaderNames = request.getAllowedHeaders(); boolean allowAllHeader = false; Proxy proxy = request.getProxy(); if (proxy != null) { allowAllHeader = proxy.isAllowAllHeader(); if (!allowAllHeader) allowedHeaderNames.addAll(proxy.getAllowedHeaders()); } AuthenticatorUtil.doAuthentication(client, method, request); StringBuffer headersSb = new StringBuffer(); for (String name : request.getRequestHeaders().keySet()) { String value = request.getRequestHeader(name); String lowname = name.toLowerCase(); if (!allowAllHeader && !allowedHeaderNames.contains(lowname)) continue; if (ignoreHeaderNames.contains(lowname)) continue; if ("cookie".equalsIgnoreCase(name)) { if (proxy.getSendingCookies() != null) { value = RequestUtil.removeCookieParam(value, proxy.getSendingCookies()); } } if ("if-modified-since".equalsIgnoreCase(name) && "Thu, 01 Jun 1970 00:00:00 GMT".equals(value)) continue; method.addRequestHeader(new Header(name, value)); headersSb.append(name + "=" + value + ", "); } int cacheStatus = getCache(client, method, request); if (cacheStatus != 0) return cacheStatus; if (log.isInfoEnabled()) log.info("RequestHeader: " + headersSb); // execute http method and process redirect method.setFollowRedirects(false); client.executeMethod(method); int statusCode = method.getStatusCode(); for (int i = 0; statusCode == HttpStatus.SC_MOVED_TEMPORARILY || statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_SEE_OTHER || statusCode == HttpStatus.SC_TEMPORARY_REDIRECT; i++) { // connection release method.releaseConnection(); if (i == 5) { log.error("The circular redirect is limited by five times."); return 500; } Header location = method.getResponseHeader("Location"); String redirectUrl = location.getValue(); // According to 2,068 1.1 rfc http spec, we cannot appoint the relative URL, // but microsoft.com gives back the relative URL. if (redirectUrl.startsWith("/")) { URI baseURI = method.getURI(); baseURI.setPath(redirectUrl); redirectUrl = baseURI.toString(); } //method.setURI(new URI(redirectUrl, false)); Header[] headers = method.getRequestHeaders(); method = new GetMethod(redirectUrl); for (int j = 0; j < headers.length; j++) { String headerName = headers[j].getName(); if (!headerName.equalsIgnoreCase("content-length") && !headerName.equalsIgnoreCase("authorization")) method.setRequestHeader(headers[j]); } AuthenticatorUtil.doAuthentication(client, method, request); method.setRequestHeader("authorization", request.getRequestHeader("Authorization")); method.setFollowRedirects(false); client.executeMethod(method); statusCode = method.getStatusCode(); request.setRedirectURL(redirectUrl); if (log.isInfoEnabled()) log.info("Redirect " + request.getTargetURL() + " to " + location + "."); } // copy response headers to proxyReqeust Header[] headers = method.getResponseHeaders(); for (int i = 0; i < headers.length; i++) { request.putResponseHeader(headers[i].getName(), headers[i].getValue()); } if (log.isInfoEnabled()) log.info("Original Status:" + statusCode); // check response code if (statusCode == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED) { log.error("Proxy Authentication Required. Confirm ajax proxy setting."); throw new Exception( "Http Status 407, Proxy Authentication Required. Please contuct System Administrator."); } if (statusCode == HttpStatus.SC_NOT_MODIFIED || statusCode == HttpStatus.SC_RESET_CONTENT) { return statusCode; } else if (statusCode < 200 || statusCode >= 300) { request.setResponseBody(method.getResponseBodyAsStream()); return statusCode; } // process response body InputStream responseStream = null; if (statusCode != HttpStatus.SC_NO_CONTENT) { if (request.allowUserPublicCache()) { byte[] responseBody = method.getResponseBody(); Map<String, List<String>> responseHeaders = request.getResponseHeaders(); if (request.getRedirectURL() != null) responseHeaders.put("X-IS-REDIRECTED-FROM", Arrays.asList(new String[] { request.getRedirectURL() })); if (method instanceof GetMethod) { putCache(request.getOriginalURL(), new ByteArrayInputStream(responseBody), responseHeaders); } responseStream = new ByteArrayInputStream(responseBody); } else { responseStream = method.getResponseBodyAsStream(); } } doFilterChain(request, responseStream); return statusCode != HttpStatus.SC_NO_CONTENT ? method.getStatusCode() : 200; }
From source file:org.jaggeryjs.hostobjects.ws.WSRequestHostObject.java
private static Scriptable setOptionsOpenWSDL(WSRequestHostObject wsRequest, Object[] args) throws ScriptException { Scriptable object = null;/* w w w. j ava2 s . c o m*/ String wsdlURL; QName serviceQName = null; String endpointName = null; switch (args.length) { case 0: throw Context.reportRuntimeError("INVALID_SYNTAX_EXCEPTION"); case 1: throw Context.reportRuntimeError("INVALID_SYNTAX_EXCEPTION"); case 2: if (args[0] instanceof String) { wsdlURL = (String) args[0]; } else { throw Context.reportRuntimeError("INVALID_SYNTAX_EXCEPTION"); } if (args[1] instanceof Boolean) { wsRequest.async = (Boolean) args[1]; } else { throw Context.reportRuntimeError("INVALID_SYNTAX_EXCEPTION"); } break; case 3: if (args[0] instanceof String) { wsdlURL = (String) args[0]; } else { throw Context.reportRuntimeError("INVALID_SYNTAX_EXCEPTION"); } if (args[1] instanceof Boolean) { wsRequest.async = (Boolean) args[1]; } else { throw Context.reportRuntimeError("INVALID_SYNTAX_EXCEPTION"); } if (args[2] instanceof Scriptable) { object = (Scriptable) args[2]; } else { throw Context.reportRuntimeError("INVALID_SYNTAX_EXCEPTION"); } break; case 5: if (args[0] instanceof String) { wsdlURL = (String) args[0]; } else { throw Context.reportRuntimeError("INVALID_SYNTAX_EXCEPTION"); } if (args[1] instanceof Boolean) { wsRequest.async = (Boolean) args[1]; } else { throw Context.reportRuntimeError("INVALID_SYNTAX_EXCEPTION"); } if (args[2] instanceof Scriptable) { object = (Scriptable) args[2]; } else { throw Context.reportRuntimeError("INVALID_SYNTAX_EXCEPTION"); } if (args[3] instanceof QName) { QName qName = (QName) args[3]; String uri = qName.getNamespaceURI(); String localName = qName.getLocalPart(); serviceQName = new QName(uri, localName); } else { throw Context.reportRuntimeError("INVALID_SYNTAX_EXCEPTION"); } if (args[4] instanceof String) { endpointName = (String) args[4]; } else { throw Context.reportRuntimeError("INVALID_SYNTAX_EXCEPTION"); } wsRequest.wsdlMode = true; break; default: throw Context.reportRuntimeError("INVALID_SYNTAX_EXCEPTION"); } HttpMethod method = new GetMethod(wsdlURL); try { int statusCode = HostObjectUtil.getURL(wsdlURL, null, null); if (statusCode != HttpStatus.SC_OK) { throw new ScriptException("An error occured while getting the resource at " + wsdlURL + ". Reason :" + method.getStatusLine()); } Document doc = XMLUtils.newDocument(method.getResponseBodyAsStream()); WSDLReader reader = WSDLFactory.newInstance().newWSDLReader(); reader.setFeature("javax.wsdl.importDocuments", true); Definition definition = reader.readWSDL(getBaseURI(wsdlURL), doc); wsRequest.targetNamespace = definition.getTargetNamespace(); Service service; Port returnPort; if (serviceQName == null) { Map services = definition.getServices(); service = null; for (Object o : services.values()) { service = (Service) o; if (service.getPorts().size() > 0) { //i.e we have found a service with ports break; } } if (service == null) { throw Context .reportRuntimeError("The WSDL given does not contain any services " + "that has ports"); } Map ports = service.getPorts(); Port port; returnPort = null; for (Iterator portsIterator = ports.values().iterator(); (portsIterator.hasNext() && returnPort == null);) { port = (Port) portsIterator.next(); List extensibilityElements = port.getExtensibilityElements(); for (Object extElement : extensibilityElements) { if (extElement instanceof SOAPAddress) { // SOAP 1.1 address found - keep this and loop until http address is found returnPort = port; String location = ((SOAPAddress) extElement).getLocationURI().trim(); if ((location != null) && location.startsWith("http:")) { // i.e we have found an http port so return from here break; } } } } if (returnPort == null) { for (Object o : ports.values()) { port = (Port) o; List extensibilityElements = port.getExtensibilityElements(); for (Object extElement : extensibilityElements) { if (extElement instanceof SOAP12Address) { // SOAP 1.2 address found - keep this and loop until http address is found returnPort = port; String location = ((SOAP12Address) extElement).getLocationURI().trim(); if ((location != null) && location.startsWith("http:")) { // i.e we have found an http port so return from here break; } } } } if (returnPort == null) { for (Object o : ports.values()) { port = (Port) o; List extensibilityElements = port.getExtensibilityElements(); for (Object extElement : extensibilityElements) { if (extElement instanceof HTTPAddress) { // HTTP address found - keep this and loop until http address is found returnPort = port; String location = ((HTTPAddress) extElement).getLocationURI().trim(); if ((location != null) && location.startsWith("http:")) { // i.e we have found an http port so return from here break; } } } } } } if (returnPort == null) { throw Context.reportRuntimeError( "The WSDL given does not contain any ports " + "that use the http transport"); } else { serviceQName = service.getQName(); endpointName = returnPort.getName(); } } wsRequest.sender = new ServiceClient(null, definition, serviceQName, endpointName); } catch (MalformedURLException e) { String message = "Malformed URL : " + wsdlURL; log.error(message, e); throw new ScriptException(message, e); } catch (Exception e) { String message = "Error occurred while reading the WSDL content from the URL : " + wsdlURL; log.error(message, e); throw new ScriptException(message, e); } finally { // Release the connection. method.releaseConnection(); } return object; }