List of usage examples for org.apache.commons.httpclient HttpMethod setQueryString
public abstract void setQueryString(NameValuePair[] paramArrayOfNameValuePair);
From source file:com.netflix.postreview.ExtendedCrucibleSession.java
@Override protected void adjustHttpHeader(HttpMethod method) { //method.addRequestHeader(new Header("Authorization", "Basic " + StringUtil.encode(getUsername() + ":" + getPassword()))); if (getAuthToken() != null) { String qs = method.getQueryString(); if (qs == null) qs = ""; if (qs.length() > 0) qs = qs + "&"; method.setQueryString(qs + "FEAUTH=" + getAuthToken()); }/* w ww . j a v a 2s . co m*/ }
From source file:de.topicmapslab.tmcledit.model.psiprovider.internal.Subj3ctPSIProvider.java
public Set<PSIProviderResult> getSubjectIdentifier() { if (getName().length() == 0) return Collections.emptySet(); HttpMethod method = null; try {//from ww w. j a va2 s. c o m String url = "http://api.subj3ct.com/subjects/search"; HttpClient client = new HttpClient(); method = new GetMethod(url); ArrayList<NameValuePair> params = new ArrayList<NameValuePair>(2); params.add(new NameValuePair("format", "xml")); params.add(new NameValuePair("query", getName())); method.setQueryString(params.toArray(new NameValuePair[params.size()])); client.getParams().setSoTimeout(5000); client.executeMethod(method); String result = method.getResponseBodyAsString(); SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); Subj3ctXmlHandler handler = new Subj3ctXmlHandler(); parser.parse(new InputSource(new StringReader(result)), handler); List<Subje3ctResult> resultList = handler.getResultList(); if (resultList.size() == 0) { return Collections.emptySet(); } Set<PSIProviderResult> resultSet = new HashSet<PSIProviderResult>(resultList.size()); for (Subje3ctResult r : resultList) { String description = ""; if (r.name != null) description = "Name: " + r.name + "\n"; if (r.description != null) description += "Description: " + r.description + "\n"; description += "\n\nThis service is provided by http://www.subj3ct.com"; resultSet.add(new PSIProviderResult(r.identifier, description)); } return Collections.unmodifiableSet(resultSet); } catch (UnknownHostException e) { // no http connection -> no results TmcleditEditPlugin.logInfo(e); return Collections.emptySet(); } catch (SocketTimeoutException e) { // timeout -> no results TmcleditEditPlugin.logInfo(e); return Collections.emptySet(); } catch (Exception e) { throw new RuntimeException(e); } finally { if (method != null) method.releaseConnection(); } }
From source file:com.boyuanitsm.pay.alipay.util.httpClient.HttpProtocolHandler.java
/** * Http//from w w w .ja v a 2 s . c om * * @param request ? * @param strParaFileName ??? * @param strFilePath * @return * @throws HttpException, IOException */ public HttpResponse execute(HttpRequest request, String strParaFileName, String strFilePath) throws HttpException, IOException { HttpClient httpclient = new HttpClient(connectionManager); // int connectionTimeout = defaultConnectionTimeout; if (request.getConnectionTimeout() > 0) { connectionTimeout = request.getConnectionTimeout(); } httpclient.getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout); // int soTimeout = defaultSoTimeout; if (request.getTimeout() > 0) { soTimeout = request.getTimeout(); } httpclient.getHttpConnectionManager().getParams().setSoTimeout(soTimeout); // ConnectionManagerconnection httpclient.getParams().setConnectionManagerTimeout(defaultHttpConnectionManagerTimeout); String charset = request.getCharset(); charset = charset == null ? DEFAULT_CHARSET : charset; HttpMethod method = null; //get?? if (request.getMethod().equals(HttpRequest.METHOD_GET)) { method = new GetMethod(request.getUrl()); method.getParams().setCredentialCharset(charset); // parseNotifyConfig??GETrequestQueryString method.setQueryString(request.getQueryString()); } else if (strParaFileName.equals("") && strFilePath.equals("")) { //post?? method = new PostMethod(request.getUrl()); ((PostMethod) method).addParameters(request.getParameters()); method.addRequestHeader("Content-Type", "application/x-www-form-urlencoded; text/html; charset=" + charset); } else { //post? method = new PostMethod(request.getUrl()); List<Part> parts = new ArrayList<Part>(); for (int i = 0; i < request.getParameters().length; i++) { parts.add(new StringPart(request.getParameters()[i].getName(), request.getParameters()[i].getValue(), charset)); } //?strParaFileName??? parts.add(new FilePart(strParaFileName, new FilePartSource(new File(strFilePath)))); // ((PostMethod) method).setRequestEntity( new MultipartRequestEntity(parts.toArray(new Part[0]), new HttpMethodParams())); } // Http HeaderUser-Agent method.addRequestHeader("User-Agent", "Mozilla/4.0"); HttpResponse response = new HttpResponse(); try { httpclient.executeMethod(method); if (request.getResultType().equals(HttpResultType.STRING)) { response.setStringResult(method.getResponseBodyAsString()); } else if (request.getResultType().equals(HttpResultType.BYTES)) { response.setByteResult(method.getResponseBody()); } response.setResponseHeaders(method.getResponseHeaders()); } catch (UnknownHostException ex) { return null; } catch (IOException ex) { return null; } catch (Exception ex) { return null; } finally { method.releaseConnection(); } return response; }
From source file:com.mirth.connect.connectors.http.HttpMessageDispatcher.java
private HttpMethod buildHttpRequest(String address, MessageObject mo) throws Exception { String method = connector.getDispatcherMethod(); String content = replacer.replaceValues(connector.getDispatcherContent(), mo); String contentType = connector.getDispatcherContentType(); String charset = connector.getDispatcherCharset(); boolean isMultipart = connector.isDispatcherMultipart(); Map<String, String> headers = replacer.replaceValuesInMap(connector.getDispatcherHeaders(), mo); Map<String, String> parameters = replacer.replaceValuesInMap(connector.getDispatcherParameters(), mo); HttpMethod httpMethod = null; // populate the query parameters NameValuePair[] queryParameters = new NameValuePair[parameters.size()]; int index = 0; for (Entry<String, String> parameterEntry : parameters.entrySet()) { queryParameters[index] = new NameValuePair(parameterEntry.getKey(), parameterEntry.getValue()); index++;/*from ww w . j a v a 2s . com*/ logger.debug("setting query parameter: [" + parameterEntry.getKey() + ", " + parameterEntry.getValue() + "]"); } // create the method if ("GET".equalsIgnoreCase(method)) { httpMethod = new GetMethod(address); httpMethod.setQueryString(queryParameters); } else if ("POST".equalsIgnoreCase(method)) { PostMethod postMethod = new PostMethod(address); if (isMultipart) { logger.debug("setting multipart file content"); File tempFile = File.createTempFile(UUID.randomUUID().toString(), ".tmp"); FileUtils.writeStringToFile(tempFile, content, charset); Part[] parts = new Part[] { new FilePart(tempFile.getName(), tempFile, contentType, charset) }; postMethod.setQueryString(queryParameters); postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams())); } else if (StringUtils.equals(contentType, "application/x-www-form-urlencoded")) { postMethod.setRequestBody(queryParameters); } else { postMethod.setQueryString(queryParameters); postMethod.setRequestEntity(new StringRequestEntity(content, contentType, charset)); } httpMethod = postMethod; } else if ("PUT".equalsIgnoreCase(method)) { PutMethod putMethod = new PutMethod(address); putMethod.setRequestEntity(new StringRequestEntity(content, contentType, charset)); putMethod.setQueryString(queryParameters); httpMethod = putMethod; } else if ("DELETE".equalsIgnoreCase(method)) { httpMethod = new DeleteMethod(address); httpMethod.setQueryString(queryParameters); } // set the headers for (Entry<String, String> headerEntry : headers.entrySet()) { httpMethod.setRequestHeader(new Header(headerEntry.getKey(), headerEntry.getValue())); logger.debug("setting method header: [" + headerEntry.getKey() + ", " + headerEntry.getValue() + "]"); } return httpMethod; }
From source file:fr.cls.atoll.motu.library.cas.HttpClientCAS.java
/** * Adds the cas ticket./* w w w . j a v a 2 s . c o m*/ * * @param method * the method * @throws IOException * Signals that an I/O exception has occurred. * @throws MotuCasException */ public static void addCASTicket(HttpMethod method) throws IOException, MotuCasException { if (LOG.isDebugEnabled()) { LOG.debug("addCASTicket(HttpMethod) - entering : debugHttpMethod BEFORE " + HttpClientCAS.debugHttpMethod(method)); } if (HttpClientCAS.addCASTicketFromTGT(method)) { return; } if (!AuthenticationHolder.isCASAuthentication()) { if (LOG.isDebugEnabled()) { LOG.debug("addCASTicket(HttpMethod) - exiting - NO CAS AUTHENTICATION : debugHttpMethod AFTER " + HttpClientCAS.debugHttpMethod(method)); } return; } String newURIAsString = AssertionUtils.addCASTicket(method.getURI().getEscapedURI()); if (!AssertionUtils.hasCASTicket(newURIAsString)) { newURIAsString = AssertionUtils.addCASTicket(method.getURI().getEscapedURI(), AuthenticationHolder.getUser()); if (!AssertionUtils.hasCASTicket(newURIAsString)) { String login = AuthenticationHolder.getUserLogin(); throw new MotuCasException(String.format( "Unable to access resource '%s'. This resource has been declared as CASified, but the Motu application/API can't retrieve any ticket from CAS via REST. \nFor information, current user login is:'%s'", method.getURI().getEscapedURI(), login)); } } URI newURI = new URI(newURIAsString, true); // method.setURI(newURI); method.setPath(newURI.getPath()); method.setQueryString(newURI.getQuery()); // System.out.println(newURI.getPathQuery()); if (LOG.isDebugEnabled()) { LOG.debug("addCASTicket(HttpMethod) - exiting : debugHttpMethod AFTER " + HttpClientCAS.debugHttpMethod(method)); } }
From source file:fr.cls.atoll.motu.library.cas.HttpClientCAS.java
/** * Adds the cas ticket from tgt.//from w ww . j a va2 s . c o m * * @param method the method * @return true, if successful * @throws MotuCasException the motu cas exception * @throws URIException the uRI exception * @throws IOException Signals that an I/O exception has occurred. */ public static boolean addCASTicketFromTGT(HttpMethod method) throws MotuCasException, URIException, IOException { if (LOG.isDebugEnabled()) { LOG.debug("addCASTicketFromTGT(HttpMethod) - entering : debugHttpMethod BEFORE " + HttpClientCAS.debugHttpMethod(method)); } Header headerTgt = method.getRequestHeader(HttpClientCAS.TGT_PARAM); Header headerCasRestUrl = method.getRequestHeader(HttpClientCAS.CAS_REST_URL_PARAM); if ((headerTgt == null) || (headerCasRestUrl == null)) { return false; } String ticketGrantingTicket = headerTgt.getValue(); String casRestUrl = headerCasRestUrl.getValue(); if ((RestUtil.isNullOrEmpty(ticketGrantingTicket)) || (RestUtil.isNullOrEmpty(casRestUrl))) { return false; } String ticket = RestUtil.loginToCASWithTGT(casRestUrl, ticketGrantingTicket, method.getURI().getEscapedURI()); String newURIAsString = AssertionUtils.addCASTicket(ticket, method.getURI().getEscapedURI()); if (!AssertionUtils.hasCASTicket(newURIAsString)) { throw new MotuCasException(String.format( "Unable to access resource '%s'. This resource has been declared as CASified, but the Motu application/API can't retrieve any ticket from CAS via REST. \nFor information, current TGT is:'%s', CAS REST url is:'%s'", method.getURI().getEscapedURI(), ticketGrantingTicket, casRestUrl)); } URI newURI = new URI(newURIAsString, true); // method.setURI(newURI); method.setPath(newURI.getPath()); method.setQueryString(newURI.getQuery()); // System.out.println(newURI.getPathQuery()); if (LOG.isDebugEnabled()) { LOG.debug("addCASTicketFromTGT(HttpMethod) - exiting : debugHttpMethod AFTER " + HttpClientCAS.debugHttpMethod(method)); } return true; }
From source file:com.mercatis.lighthouse3.commons.commons.HttpRequest.java
/** * This method performs an HTTP request against an URL. * * @param url the URL to call/*from ww w . j a v a 2 s. c om*/ * @param method the HTTP method to execute * @param body the body of a POST or PUT request, can be <code>null</code> * @param queryParams a Hash with the query parameter, can be <code>null</code> * @return the data returned by the web server * @throws HttpException in case a communication error occurred. */ @SuppressWarnings("deprecation") public String execute(String url, HttpRequest.HttpMethod method, String body, Map<String, String> queryParams) { NameValuePair[] query = null; if (queryParams != null) { query = new NameValuePair[queryParams.size()]; int counter = 0; for (Entry<String, String> queryParam : queryParams.entrySet()) { query[counter] = new NameValuePair(queryParam.getKey(), queryParam.getValue()); counter++; } } org.apache.commons.httpclient.HttpMethod request = null; if (method == HttpMethod.GET) { request = new GetMethod(url); } else if (method == HttpMethod.POST) { PostMethod postRequest = new PostMethod(url); if (body != null) { postRequest.setRequestBody(body); } request = postRequest; } else if (method == HttpMethod.PUT) { PutMethod putRequest = new PutMethod(url); if (body != null) { putRequest.setRequestBody(body); } request = putRequest; } else if (method == HttpMethod.DELETE) { request = new DeleteMethod(url); } request.setRequestHeader("Content-type", "application/xml;charset=utf-8"); if (query != null) { request.setQueryString(query); } int resultCode = 0; StringBuilder resultBodyBuilder = new StringBuilder(); try { resultCode = this.httpClient.executeMethod(request); BufferedReader reader = new BufferedReader( new InputStreamReader(request.getResponseBodyAsStream(), Charset.forName("UTF-8"))); String line = null; while ((line = reader.readLine()) != null) { resultBodyBuilder.append(line); } if (resultCode != 200) { throw new HttpException(resultBodyBuilder.toString(), null); } } catch (HttpException httpException) { throw new HttpException("HTTP request failed", httpException); } catch (IOException ioException) { throw new HttpException("HTTP request failed", ioException); } catch (NullPointerException npe) { throw new HttpException("HTTP request failed", npe); } finally { request.releaseConnection(); } return resultBodyBuilder.toString(); }
From source file:ar.com.zauber.commons.web.proxy.HttpClientRequestProxy.java
/** * @param request//from w w w. j a va 2s .c om * @return */ private HttpMethod buildRequest(final HttpServletRequest request, final URLResult urlResult) { final String method = request.getMethod().toUpperCase(); final HttpMethod ret; final String uri = urlResult.getURL().toExternalForm(); if ("GET".equals(method)) { ret = new GetMethod(uri); proxyHeaders(request, ret); } else if ("POST".equals(method) || "PUT".equals(method)) { final EntityEnclosingMethod pm = "POST".equals(method) ? new PostMethod(uri) : new PutMethod(uri); proxyHeaders(request, pm); try { pm.setRequestEntity(new InputStreamRequestEntity(request.getInputStream())); } catch (IOException e) { throw new UnhandledException("No pudo abrirse el InputStream" + "del request.", e); } ret = pm; } else if ("DELETE".equals(method)) { /* rfc2616 The Content-Length field of a request or response is added or deleted according to the rules in section 4.4. A transparent proxy MUST preserve the entity-length (section 7.2.2) of the entity-body, although it MAY change the transfer-length (section 4.4). */ final DeleteMethod dm = new DeleteMethod(uri); proxyHeaders(request, dm); //No body => No Header dm.removeRequestHeader("Content-Length"); ret = dm; } else { throw new NotImplementedException("i accept patches :)"); } if (request.getQueryString() != null) { ret.setQueryString(request.getQueryString()); } return ret; }
From source file:edu.ucsb.eucalyptus.cloud.ws.WalrusManager.java
public static void callWalrusHeartBeat(String account, String instid, String API) throws Throwable { HttpClient client = null;// w ww .j a va 2 s . c om HttpMethod method = null; NameValuePair[] queryString = null; if (account == null || instid == null) { LOG.debug(API + ":callWalrusHeartBeat error: #account=" + account + "#instid=" + instid + "#"); return; } try { client = new HttpClient(); String URL = "http://127.0.0.1/sbx_svr/rest/EBS/walrusheartbeat"; queryString = new NameValuePair[] { new NameValuePair("account", account), new NameValuePair("instanceid", instid) }; method = new PostMethod(URL); method.addRequestHeader(new Header("Connection", "close")); method.setQueryString(queryString); int statusCode = client.executeMethod(method); if (statusCode == HttpStatus.SC_OK) { // Read the response body. StringBuffer stb = new StringBuffer(); InputStream ins = method.getResponseBodyAsStream(); InputStreamReader insReader = new InputStreamReader(ins); BufferedReader br = new BufferedReader(insReader); String buffText = br.readLine(); while (null != buffText) { stb.append(buffText); buffText = br.readLine(); } if (stb.length() == 0 || StringUtils.isEmpty(stb.toString())) { LOG.debug(API + ":callWalrusHeartBeat: Http Response Body is empty!"); } } else { LOG.debug(API + ":callWalrusHeartBeat: Http Response Error:" + statusCode + "#account=" + account + "#instid=" + instid); } } catch (Throwable t) { LOG.debug(API + ":callWalrusHeartBeat: Http Response Error: #account=" + account + "#instid=" + instid + "#" + t.toString()); throw t; } finally { try { if (method != null) method.releaseConnection(); } catch (Throwable t) { /*NOP*/} ; } }
From source file:com.twinsoft.convertigo.beans.connectors.SiteClipperConnector.java
private void doProcessRequest(Shuttle shuttle) throws IOException, ServletException, EngineException { shuttle.statisticsTaskID = context.statistics.start(EngineStatistics.GET_DOCUMENT); try {//w ww .ja va2 s . co m shuttle.sharedScope = context.getSharedScope(); String domain = shuttle.getRequest(QueryPart.host) + shuttle.getRequest(QueryPart.port); Engine.logSiteClipper.trace("(SiteClipperConnector) Prepare the request for the domain " + domain); if (!shouldRewrite(domain)) { Engine.logSiteClipper.info( "(SiteClipperConnector) The domain " + domain + " is not allowed with this connector"); shuttle.response.sendError(HttpServletResponse.SC_FORBIDDEN, "The domain " + domain + " is not allowed with this connector"); return; } String uri = shuttle.getRequest(QueryPart.uri); Engine.logSiteClipper.info("Preparing " + shuttle.request.getMethod() + " " + shuttle.getRequestUrl()); HttpMethod httpMethod = null; XulRecorder xulRecorder = context.getXulRecorder(); if (xulRecorder != null) { httpMethod = shuttle.httpMethod = xulRecorder.getRecord(shuttle.getRequestUrlAndQuery()); } if (httpMethod == null) { try { switch (shuttle.getRequestHttpMethodType()) { case GET: httpMethod = new GetMethod(uri); break; case POST: httpMethod = new PostMethod(uri); ((PostMethod) httpMethod) .setRequestEntity(new InputStreamRequestEntity(shuttle.request.getInputStream())); break; case PUT: httpMethod = new PutMethod(uri); ((PutMethod) httpMethod) .setRequestEntity(new InputStreamRequestEntity(shuttle.request.getInputStream())); break; case DELETE: httpMethod = new DeleteMethod(uri); break; case HEAD: httpMethod = new HeadMethod(uri); break; case OPTIONS: httpMethod = new OptionsMethod(uri); break; case TRACE: httpMethod = new TraceMethod(uri); break; default: throw new ServletException( "(SiteClipperConnector) unknown http method " + shuttle.request.getMethod()); } httpMethod.setFollowRedirects(false); } catch (Exception e) { throw new ServletException( "(SiteClipperConnector) unexpected exception will building the http method : " + e.getMessage()); } shuttle.httpMethod = httpMethod; SiteClipperScreenClass screenClass = getCurrentScreenClass(); Engine.logSiteClipper.info("Request screen class: " + screenClass.getName()); for (String name : Collections .list(GenericUtils.<Enumeration<String>>cast(shuttle.request.getHeaderNames()))) { if (requestHeadersToIgnore.contains(HeaderName.parse(name))) { Engine.logSiteClipper.trace("(SiteClipperConnector) Ignoring request header " + name); } else { String value = shuttle.request.getHeader(name); Engine.logSiteClipper .trace("(SiteClipperConnector) Copying request header " + name + "=" + value); shuttle.setRequestCustomHeader(name, value); } } Engine.logSiteClipper.debug("(SiteClipperConnector) applying request rules for the screenclass " + screenClass.getName()); for (IRequestRule rule : screenClass.getRequestRules()) { if (rule.isEnabled()) { Engine.logSiteClipper .trace("(SiteClipperConnector) applying request rule " + rule.getName()); rule.fireEvents(); boolean done = rule.applyOnRequest(shuttle); Engine.logSiteClipper.debug("(SiteClipperConnector) the request rule " + rule.getName() + " is " + (done ? "well" : "not") + " applied"); } else { Engine.logSiteClipper .trace("(SiteClipperConnector) skip the disabled request rule " + rule.getName()); } } for (Entry<String, String> header : shuttle.requestCustomHeaders.entrySet()) { Engine.logSiteClipper.trace("(SiteClipperConnector) Push request header " + header.getKey() + "=" + header.getValue()); httpMethod.addRequestHeader(header.getKey(), header.getValue()); } String queryString = shuttle.request.getQueryString(); if (queryString != null) { try { // Fake test in order to check query string validity new URI("http://localhost/index?" + queryString, true, httpMethod.getParams().getUriCharset()); } catch (URIException e) { // Bugfix #2103 StringBuffer newQuery = new StringBuffer(); for (String part : RegexpUtils.pattern_and.split(queryString)) { String[] pair = RegexpUtils.pattern_equals.split(part, 2); try { newQuery.append('&') .append(URLEncoder.encode(URLDecoder.decode(pair[0], "UTF-8"), "UTF-8")); if (pair.length > 1) { newQuery.append('=').append( URLEncoder.encode(URLDecoder.decode(pair[1], "UTF-8"), "UTF-8")); } } catch (UnsupportedEncodingException ee) { Engine.logSiteClipper .trace("(SiteClipperConnector) failed to encode query part : " + part); } } queryString = newQuery.length() > 0 ? newQuery.substring(1) : newQuery.toString(); Engine.logSiteClipper.trace("(SiteClipperConnector) re-encode query : " + queryString); } } Engine.logSiteClipper.debug("(SiteClipperConnector) Copying the query string : " + queryString); httpMethod.setQueryString(queryString); // if (context.httpState == null) { // Engine.logSiteClipper.debug("(SiteClipperConnector) Creating new HttpState for context id " + context.contextID); // context.httpState = new HttpState(); // } else { // Engine.logSiteClipper.debug("(SiteClipperConnector) Using HttpState of context id " + context.contextID); // } getHttpState(shuttle); HostConfiguration hostConfiguration = getHostConfiguration(shuttle); HttpMethodParams httpMethodParams = httpMethod.getParams(); httpMethodParams.setBooleanParameter("http.connection.stalecheck", true); httpMethodParams.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, true)); Engine.logSiteClipper.info("Requesting " + httpMethod.getName() + " " + hostConfiguration.getHostURL() + httpMethod.getURI().toString()); HttpClient httpClient = context.getHttpClient3(shuttle.getHttpPool()); HttpUtils.logCurrentHttpConnection(httpClient, hostConfiguration, shuttle.getHttpPool()); httpClient.executeMethod(hostConfiguration, httpMethod, context.httpState); } else { Engine.logSiteClipper.info("Retrieve recorded response from Context"); } int status = httpMethod.getStatusCode(); shuttle.processState = ProcessState.response; Engine.logSiteClipper.info("Request terminated with status " + status); shuttle.response.setStatus(status); if (Engine.isStudioMode() && status == HttpServletResponse.SC_OK && shuttle.getResponseMimeType().startsWith("text/")) { fireDataChanged(new ConnectorEvent(this, shuttle.getResponseAsString())); } SiteClipperScreenClass screenClass = getCurrentScreenClass(); Engine.logSiteClipper.info("Response screen class: " + screenClass.getName()); if (Engine.isStudioMode()) { Engine.theApp.fireObjectDetected(new EngineEvent(screenClass)); } for (Header header : httpMethod.getResponseHeaders()) { String name = header.getName(); if (responseHeadersToIgnore.contains(HeaderName.parse(name))) { Engine.logSiteClipper.trace("(SiteClipperConnector) Ignoring response header " + name); } else { String value = header.getValue(); Engine.logSiteClipper .trace("(SiteClipperConnector) Copying response header " + name + "=" + value); shuttle.responseCustomHeaders.put(name, value); } } Engine.logSiteClipper.debug( "(SiteClipperConnector) applying response rules for the screenclass " + screenClass.getName()); for (IResponseRule rule : screenClass.getResponseRules()) { if (rule.isEnabled()) { Engine.logSiteClipper.trace("(SiteClipperConnector) applying response rule " + rule.getName()); rule.fireEvents(); boolean done = rule.applyOnResponse(shuttle); Engine.logSiteClipper.debug("(SiteClipperConnector) the response rule " + rule.getName() + " is " + (done ? "well" : "not") + " applied"); } else { Engine.logSiteClipper .trace("(SiteClipperConnector) skip the disabled response rule " + rule.getName()); } } for (Entry<String, String> header : shuttle.responseCustomHeaders.entrySet()) { Engine.logSiteClipper.trace( "(SiteClipperConnector) Push request header " + header.getKey() + "=" + header.getValue()); shuttle.response.addHeader(header.getKey(), header.getValue()); } if (shuttle.postInstructions != null) { JSONArray instructions = new JSONArray(); for (IClientInstruction instruction : shuttle.postInstructions) { try { instructions.put(instruction.getInstruction()); } catch (JSONException e) { Engine.logSiteClipper.error( "(SiteClipperConnector) Failed to add a post instruction due to a JSONException", e); } } String codeToInject = "<script>C8O_postInstructions = " + instructions.toString() + "</script>\n" + "<script src=\"" + shuttle.getRequest(QueryPart.full_convertigo_path) + "/scripts/jquery.min.js\"></script>\n" + "<script src=\"" + shuttle.getRequest(QueryPart.full_convertigo_path) + "/scripts/siteclipper.js\"></script>\n"; String content = shuttle.getResponseAsString(); Matcher matcher = HtmlLocation.head_top.matcher(content); String newContent = RegexpUtils.inject(matcher, codeToInject); if (newContent == null) { matcher = HtmlLocation.body_top.matcher(content); newContent = RegexpUtils.inject(matcher, codeToInject); } if (newContent != null) { shuttle.setResponseAsString(newContent); } else { Engine.logSiteClipper.info( "(SiteClipperConnector) Failed to find a head or body tag in the response content"); Engine.logSiteClipper.trace("(SiteClipperConnector) Response content : \"" + content + "\""); } } long nbBytes = 0L; if (shuttle.responseAsString != null && shuttle.responseAsString.hashCode() != shuttle.responseAsStringOriginal.hashCode()) { OutputStream os = shuttle.response.getOutputStream(); switch (shuttle.getResponseContentEncoding()) { case gzip: os = new GZIPOutputStream(os); break; case deflate: os = new DeflaterOutputStream(os, new Deflater(Deflater.DEFAULT_COMPRESSION | Deflater.DEFAULT_STRATEGY, true)); break; default: break; } nbBytes = shuttle.responseAsByte.length; IOUtils.write(shuttle.responseAsString, os, shuttle.getResponseCharset()); os.close(); } else { InputStream is = (shuttle.responseAsByte == null) ? httpMethod.getResponseBodyAsStream() : new ByteArrayInputStream(shuttle.responseAsByte); if (is != null) { nbBytes = 0; OutputStream os = shuttle.response.getOutputStream(); int read = is.read(); while (read >= 0) { os.write(read); os.flush(); read = is.read(); nbBytes++; } is.close(); // nbBytes = IOUtils.copyLarge(is, shuttle.response.getOutputStream()); Engine.logSiteClipper .trace("(SiteClipperConnector) Response body copyied (" + nbBytes + " bytes)"); } } shuttle.response.getOutputStream().close(); shuttle.score = getScore(nbBytes); Engine.logSiteClipper .debug("(SiteClipperConnector) Request terminated with a score of " + shuttle.score); } finally { long duration = context.statistics.stop(shuttle.statisticsTaskID); if (context.requestedObject != null) { try { Engine.theApp.billingManager.insertBilling(context, Long.valueOf(duration), Long.valueOf(shuttle.score)); } catch (Exception e) { Engine.logContext.warn("Unable to insert billing ticket (the billing is thus ignored): [" + e.getClass().getName() + "] " + e.getMessage()); } } } }