List of usage examples for org.apache.commons.httpclient HttpMethod setRequestHeader
public abstract void setRequestHeader(String paramString1, String paramString2);
From source file:com.cyberway.issue.crawler.fetcher.FetchHTTP.java
/** * Configure the HttpMethod setting options and headers. * * @param curi CrawlURI from which we pull configuration. * @param method The Method to configure. * @return HostConfiguration copy customized for this CrawlURI *//* w w w . j av a 2 s . c o m*/ protected HostConfiguration configureMethod(CrawlURI curi, HttpMethod method) { // Don't auto-follow redirects method.setFollowRedirects(false); // // set soTimeout // method.getParams().setSoTimeout( // ((Integer) getUncheckedAttribute(curi, ATTR_SOTIMEOUT_MS)) // .intValue()); // Set cookie policy. method.getParams() .setCookiePolicy((((Boolean) getUncheckedAttribute(curi, ATTR_IGNORE_COOKIES)).booleanValue()) ? CookiePolicy.IGNORE_COOKIES : CookiePolicy.BROWSER_COMPATIBILITY); // Use only HTTP/1.0 (to avoid receiving chunked responses) method.getParams().setVersion(HttpVersion.HTTP_1_0); CrawlOrder order = getSettingsHandler().getOrder(); String userAgent = curi.getUserAgent(); if (userAgent == null) { userAgent = order.getUserAgent(curi); } method.setRequestHeader("User-Agent", userAgent); method.setRequestHeader("From", order.getFrom(curi)); // Set retry handler. method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new HeritrixHttpMethodRetryHandler()); final long maxLength = getMaxLength(curi); if (maxLength > 0 && ((Boolean) getUncheckedAttribute(curi, ATTR_SEND_RANGE)).booleanValue()) { method.addRequestHeader(RANGE, RANGE_PREFIX.concat(Long.toString(maxLength - 1))); } if (((Boolean) getUncheckedAttribute(curi, ATTR_SEND_CONNECTION_CLOSE)).booleanValue()) { method.addRequestHeader(HEADER_SEND_CONNECTION_CLOSE); } if (((Boolean) getUncheckedAttribute(curi, ATTR_SEND_REFERER)).booleanValue()) { // RFC2616 says no referer header if referer is https and the url // is not String via = curi.flattenVia(); if (via != null && via.length() > 0 && !(via.startsWith(HTTPS_SCHEME) && curi.getUURI().getScheme().equals(HTTP_SCHEME))) { method.setRequestHeader(REFERER, via); } } if (!curi.isPrerequisite()) { setConditionalGetHeader(curi, method, ATTR_SEND_IF_MODIFIED_SINCE, CoreAttributeConstants.A_LAST_MODIFIED_HEADER, "If-Modified-Since"); setConditionalGetHeader(curi, method, ATTR_SEND_IF_NONE_MATCH, CoreAttributeConstants.A_ETAG_HEADER, "If-None-Match"); } // TODO: What happens if below method adds a header already // added above: e.g. Connection, Range, or Referer? setAcceptHeaders(curi, method); HostConfiguration config = new HostConfiguration(http.getHostConfiguration()); configureProxy(curi, config); configureBindAddress(curi, config); return config; }
From source file:itslearning.platform.restapi.sdk.learningtoolapp.LearningObjectServicetRestClient.java
/** * Initializes the http client with correct httpmethod. Adds Authorization header to request. * @param client//from w w w . ja va 2 s. c o m * @param uri * @param methodType * @return */ protected HttpMethod getInitializedHttpMethod(HttpClient client, String uri, HttpMethodType methodType) { Calendar now = GregorianCalendar.getInstance(); _apiSession.setLastRequestDateTimeUtc(new Date(now.getTimeInMillis())); _apiSession.setHash(CryptographyHelper.computeHash(_apiSession, _sharedSecret)); String authHeader = AuthorizationHelper.toAuthorizationHeader(_apiSession); HttpMethod method; switch (methodType) { case GET: method = new GetMethod(uri); break; case PUT: method = new PutMethod(uri); method.setRequestHeader("Content-Type", "text/xml"); break; case POST: method = new PostMethod(uri); method.setRequestHeader("Content-Type", "text/xml"); break; case DELETE: method = new DeleteMethod(uri); break; default: method = new GetMethod(uri); } HttpParams params = DefaultHttpParams.getDefaultParams(); method.setRequestHeader("Authorization", authHeader); client.setParams((HttpClientParams) params); return method; }
From source file:com.twinsoft.convertigo.beans.connectors.HttpConnector.java
protected int doExecuteMethod(final HttpMethod method, Context context) throws ConnectionException, URIException, MalformedURLException { int statuscode = -1; // Tells the method to automatically handle authentication. method.setDoAuthentication(true);//from w w w.j a v a2 s.c o m // Tells the method to automatically handle redirection. method.setFollowRedirects(false); HttpPool httpPool = ((AbstractHttpTransaction) context.transaction).getHttpPool(); HttpClient httpClient = context.getHttpClient3(httpPool); try { // Display the cookies if (handleCookie) { Cookie[] cookies = httpState.getCookies(); if (Engine.logBeans.isTraceEnabled()) Engine.logBeans.trace( "(HttpConnector) HttpClient request cookies:" + Arrays.asList(cookies).toString()); } forwardHeader(new HeaderForwarder() { public void add(String name, String value, String forwardPolicy) { if (HttpConnector.HTTP_HEADER_FORWARD_POLICY_IGNORE.equals(forwardPolicy)) { Header exHeader = method.getRequestHeader(name); if (exHeader != null) { // Nothing to do Engine.logEngine.debug("(WebViewer) Forwarding header '" + name + "' has been ignored due to forward policy"); } else { method.setRequestHeader(name, value); Engine.logEngine.debug("(WebViewer) Header forwarded and added: " + name + "=" + value); } } else if (HttpConnector.HTTP_HEADER_FORWARD_POLICY_REPLACE.equals(forwardPolicy)) { method.setRequestHeader(name, value); Engine.logEngine.debug("(WebViewer) Header forwarded and replaced: " + name + "=" + value); } else if (HttpConnector.HTTP_HEADER_FORWARD_POLICY_MERGE.equals(forwardPolicy)) { Header exHeader = method.getRequestHeader(name); if (exHeader != null) value = exHeader.getValue() + ", " + value; method.setRequestHeader(name, value); Engine.logEngine.debug("(WebViewer) Header forwarded and merged: " + name + "=" + value); } } }); // handle oAuthSignatures if any if (oAuthKey != null && oAuthSecret != null && oAuthToken != null && oAuthTokenSecret != null) { oAuthConsumer = new HttpOAuthConsumer(oAuthKey, oAuthSecret, hostConfiguration); oAuthConsumer.setTokenWithSecret(oAuthToken, oAuthTokenSecret); oAuthConsumer.sign(method); oAuthConsumer = null; } HttpUtils.logCurrentHttpConnection(httpClient, hostConfiguration, httpPool); hostConfiguration.getParams().setIntParameter(HttpConnectionParams.SO_TIMEOUT, (int) context.requestedObject.getResponseTimeout() * 1000); hostConfiguration.getParams().setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, (int) context.requestedObject.getResponseTimeout() * 1000); Engine.logBeans.debug("(HttpConnector) HttpClient: executing method..."); statuscode = httpClient.executeMethod(hostConfiguration, method, httpState); Engine.logBeans.debug("(HttpConnector) HttpClient: end of method successfull"); // Display the cookies if (handleCookie) { Cookie[] cookies = httpState.getCookies(); if (Engine.logBeans.isTraceEnabled()) Engine.logBeans.trace( "(HttpConnector) HttpClient response cookies:" + Arrays.asList(cookies).toString()); } } catch (SocketTimeoutException e) { throw new ConnectionException( "Timeout reached (" + context.requestedObject.getResponseTimeout() + " sec)"); } catch (IOException e) { if (!context.requestedObject.runningThread.bContinue) return statuscode; try { HttpUtils.logCurrentHttpConnection(httpClient, hostConfiguration, httpPool); Engine.logBeans.warn("(HttpConnector) HttpClient: connection error to " + sUrl + ": " + e.getMessage() + "; retrying method"); statuscode = httpClient.executeMethod(hostConfiguration, method, httpState); Engine.logBeans.debug("(HttpConnector) HttpClient: end of method successfull"); } catch (IOException ee) { throw new ConnectionException("Connection error to " + sUrl, ee); } } catch (OAuthException eee) { throw new ConnectionException("OAuth Connection error to " + sUrl, eee); } return statuscode; }
From source file:com.twinsoft.convertigo.beans.connectors.HttpConnector.java
public byte[] getData(Context context) throws IOException, EngineException { HttpMethod method = null; try {/* www .j a v a2s . c o m*/ // Fire event for plugins long t0 = System.currentTimeMillis(); Engine.theApp.pluginsManager.fireHttpConnectorGetDataStart(context); // Retrieving httpState getHttpState(context); Engine.logBeans.trace("(HttpConnector) Retrieving data as a bytes array..."); Engine.logBeans.debug("(HttpConnector) Connecting to: " + sUrl); // Setting the referer referer = sUrl; URL url = null; url = new URL(sUrl); // Proxy configuration Engine.theApp.proxyManager.setProxy(hostConfiguration, httpState, url); Engine.logBeans.debug("(HttpConnector) Https: " + https); String host = ""; int port = -1; if (sUrl.toLowerCase().startsWith("https:")) { if (!https) { Engine.logBeans.debug("(HttpConnector) Setting up SSL properties"); certificateManager.collectStoreInformation(context); } url = new URL(sUrl); host = url.getHost(); port = url.getPort(); if (port == -1) port = 443; Engine.logBeans.debug("(HttpConnector) Host: " + host + ":" + port); Engine.logBeans .debug("(HttpConnector) CertificateManager has changed: " + certificateManager.hasChanged); if (certificateManager.hasChanged || (!host.equalsIgnoreCase(hostConfiguration.getHost())) || (hostConfiguration.getPort() != port)) { Engine.logBeans.debug("(HttpConnector) Using MySSLSocketFactory for creating the SSL socket"); Protocol myhttps = new Protocol("https", MySSLSocketFactory.getSSLSocketFactory(certificateManager.keyStore, certificateManager.keyStorePassword, certificateManager.trustStore, certificateManager.trustStorePassword, this.trustAllServerCertificates), port); hostConfiguration.setHost(host, port, myhttps); } sUrl = url.getFile(); Engine.logBeans.debug("(HttpConnector) Updated URL for SSL purposes: " + sUrl); } else { url = new URL(sUrl); host = url.getHost(); port = url.getPort(); Engine.logBeans.debug("(HttpConnector) Host: " + host + ":" + port); hostConfiguration.setHost(host, port); } AbstractHttpTransaction httpTransaction = (AbstractHttpTransaction) context.transaction; // Retrieve HTTP method HttpMethodType httpVerb = httpTransaction.getHttpVerb(); String sHttpVerb = httpVerb.name(); final String sCustomHttpVerb = httpTransaction.getCustomHttpVerb(); if (sCustomHttpVerb.length() > 0) { Engine.logBeans.debug( "(HttpConnector) HTTP verb: " + sHttpVerb + " overridden to '" + sCustomHttpVerb + "'"); switch (httpVerb) { case GET: method = new GetMethod(sUrl) { @Override public String getName() { return sCustomHttpVerb; } }; break; case POST: method = new PostMethod(sUrl) { @Override public String getName() { return sCustomHttpVerb; } }; break; case PUT: method = new PutMethod(sUrl) { @Override public String getName() { return sCustomHttpVerb; } }; break; case DELETE: method = new DeleteMethod(sUrl) { @Override public String getName() { return sCustomHttpVerb; } }; break; case HEAD: method = new HeadMethod(sUrl) { @Override public String getName() { return sCustomHttpVerb; } }; break; case OPTIONS: method = new OptionsMethod(sUrl) { @Override public String getName() { return sCustomHttpVerb; } }; break; case TRACE: method = new TraceMethod(sUrl) { @Override public String getName() { return sCustomHttpVerb; } }; break; } } else { Engine.logBeans.debug("(HttpConnector) HTTP verb: " + sHttpVerb); switch (httpVerb) { case GET: method = new GetMethod(sUrl); break; case POST: method = new PostMethod(sUrl); break; case PUT: method = new PutMethod(sUrl); break; case DELETE: method = new DeleteMethod(sUrl); break; case HEAD: method = new HeadMethod(sUrl); break; case OPTIONS: method = new OptionsMethod(sUrl); break; case TRACE: method = new TraceMethod(sUrl); break; } } // Setting HTTP parameters boolean hasUserAgent = false; for (List<String> httpParameter : httpParameters) { String key = httpParameter.get(0); String value = httpParameter.get(1); if (key.equalsIgnoreCase("host") && !value.equals(host)) { value = host; } if (!key.startsWith(DYNAMIC_HEADER_PREFIX)) { method.setRequestHeader(key, value); } if (HeaderName.UserAgent.is(key)) { hasUserAgent = true; } } // set user-agent header if not found if (!hasUserAgent) { HeaderName.UserAgent.setRequestHeader(method, getUserAgent(context)); } // Setting POST or PUT parameters if any Engine.logBeans.debug("(HttpConnector) Setting " + httpVerb + " data"); if (method instanceof EntityEnclosingMethod) { EntityEnclosingMethod entityEnclosingMethod = (EntityEnclosingMethod) method; AbstractHttpTransaction transaction = (AbstractHttpTransaction) context.requestedObject; if (doMultipartFormData) { RequestableHttpVariable body = (RequestableHttpVariable) httpTransaction .getVariable(Parameter.HttpBody.getName()); if (body != null && body.getDoFileUploadMode() == DoFileUploadMode.multipartFormData) { String stringValue = httpTransaction.getParameterStringValue(Parameter.HttpBody.getName()); String filepath = Engine.theApp.filePropertyManager.getFilepathFromProperty(stringValue, getProject().getName()); File file = new File(filepath); if (file.exists()) { HeaderName.ContentType.setRequestHeader(method, contentType); entityEnclosingMethod.setRequestEntity(new FileRequestEntity(file, contentType)); } else { throw new FileNotFoundException(file.getAbsolutePath()); } } else { List<Part> parts = new LinkedList<Part>(); for (RequestableVariable variable : transaction.getVariablesList()) { if (variable instanceof RequestableHttpVariable) { RequestableHttpVariable httpVariable = (RequestableHttpVariable) variable; if ("POST".equals(httpVariable.getHttpMethod())) { Object httpObjectVariableValue = transaction .getVariableValue(httpVariable.getName()); if (httpVariable.isMultiValued()) { if (httpObjectVariableValue instanceof Collection<?>) { for (Object httpVariableValue : (Collection<?>) httpObjectVariableValue) { addFormDataPart(parts, httpVariable, httpVariableValue); } } } else { addFormDataPart(parts, httpVariable, httpObjectVariableValue); } } } } MultipartRequestEntity mre = new MultipartRequestEntity( parts.toArray(new Part[parts.size()]), entityEnclosingMethod.getParams()); HeaderName.ContentType.setRequestHeader(method, mre.getContentType()); entityEnclosingMethod.setRequestEntity(mre); } } else if (MimeType.TextXml.is(contentType)) { final MimeMultipart[] mp = { null }; for (RequestableVariable variable : transaction.getVariablesList()) { if (variable instanceof RequestableHttpVariable) { RequestableHttpVariable httpVariable = (RequestableHttpVariable) variable; if (httpVariable.getDoFileUploadMode() == DoFileUploadMode.MTOM) { Engine.logBeans.trace( "(HttpConnector) Variable " + httpVariable.getName() + " detected as MTOM"); MimeMultipart mimeMultipart = mp[0]; try { if (mimeMultipart == null) { Engine.logBeans.debug("(HttpConnector) Preparing the MTOM request"); mimeMultipart = new MimeMultipart("related; type=\"application/xop+xml\""); MimeBodyPart bp = new MimeBodyPart(); bp.setText(postQuery, "UTF-8"); bp.setHeader(HeaderName.ContentType.value(), contentType); mimeMultipart.addBodyPart(bp); } Object httpObjectVariableValue = transaction .getVariableValue(httpVariable.getName()); if (httpVariable.isMultiValued()) { if (httpObjectVariableValue instanceof Collection<?>) { for (Object httpVariableValue : (Collection<?>) httpObjectVariableValue) { addMtomPart(mimeMultipart, httpVariable, httpVariableValue); } } } else { addMtomPart(mimeMultipart, httpVariable, httpObjectVariableValue); } mp[0] = mimeMultipart; } catch (Exception e) { Engine.logBeans.warn( "(HttpConnector) Failed to add MTOM part for " + httpVariable.getName(), e); } } } } if (mp[0] == null) { entityEnclosingMethod.setRequestEntity( new StringRequestEntity(postQuery, MimeType.TextXml.value(), "UTF-8")); } else { Engine.logBeans.debug("(HttpConnector) Commit the MTOM request with the ContentType: " + mp[0].getContentType()); HeaderName.ContentType.setRequestHeader(method, mp[0].getContentType()); entityEnclosingMethod.setRequestEntity(new RequestEntity() { @Override public void writeRequest(OutputStream outputStream) throws IOException { try { mp[0].writeTo(outputStream); } catch (MessagingException e) { new IOException(e); } } @Override public boolean isRepeatable() { return true; } @Override public String getContentType() { return mp[0].getContentType(); } @Override public long getContentLength() { return -1; } }); } } else { String charset = httpTransaction.getComputedUrlEncodingCharset(); HeaderName.ContentType.setRequestHeader(method, contentType); entityEnclosingMethod .setRequestEntity(new StringRequestEntity(postQuery, contentType, charset)); } } // Getting the result Engine.logBeans.debug("(HttpConnector) HttpClient: getting response body"); byte[] result = executeMethod(method, context); Engine.logBeans.debug("(HttpConnector) Total read bytes: " + ((result != null) ? result.length : 0)); // Fire event for plugins long t1 = System.currentTimeMillis(); Engine.theApp.pluginsManager.fireHttpConnectorGetDataEnd(context, t0, t1); fireDataChanged(new ConnectorEvent(this, result)); return result; } finally { if (method != null) method.releaseConnection(); } }
From source file:org.agnitas.dao.impl.VersionControlDaoImpl.java
private String fetchServerVersion(String currentVersion, String referrer) { HttpClient client = new HttpClient(); client.setConnectionTimeout(CONNECTION_TIMEOUT); HttpMethod method = new GetMethod(URL); method.setRequestHeader("referer", referrer); NameValuePair[] queryParams = new NameValuePair[1]; queryParams[0] = new NameValuePair(VERSION_KEY, currentVersion); method.setQueryString(queryParams);//from w w w . j av a 2 s. co m method.setFollowRedirects(true); String responseBody = null; try { client.executeMethod(method); responseBody = method.getResponseBodyAsString(); } catch (Exception he) { logger.error("HTTP error connecting to '" + URL + "'", he); } //clean up the connection resources method.releaseConnection(); return responseBody; }
From source file:org.alfresco.custom.rest.httpconnector.RemoteClientImpl.java
private void preparePostPutRequest(ContentType contentType, InputStream in, org.apache.commons.httpclient.HttpMethod method) { method.setRequestHeader("Content-Type", contentType.value()); // apply content-length here if known (i.e. from proxied req) // if this is not set, then the content will be buffered in memory int contentLength = InputStreamRequestEntity.CONTENT_LENGTH_AUTO; ((EntityEnclosingMethod) method).setRequestEntity(new InputStreamRequestEntity(in, contentLength)); }
From source file:org.alfresco.encryption.DefaultEncryptionUtils.java
protected void setRequestMac(HttpMethod method, byte[] mac) { if (mac == null) { throw new AlfrescoRuntimeException("Mac cannot be null"); }// w w w . j a va 2 s. c o m method.setRequestHeader(HEADER_MAC, Base64.encodeBytes(mac)); }
From source file:org.alfresco.encryption.DefaultEncryptionUtils.java
/** * Set the timestamp on the HTTP request * @param method HttpMethod/*from ww w . j a va 2 s . c o m*/ * @param timestamp (ms, in UNIX time) */ protected void setRequestTimestamp(HttpMethod method, long timestamp) { method.setRequestHeader(HEADER_TIMESTAMP, String.valueOf(timestamp)); }
From source file:org.alfresco.encryption.DefaultEncryptionUtils.java
/** * {@inheritDoc}/*from w ww .j a va 2 s. com*/ */ @Override public void setRequestAlgorithmParameters(HttpMethod method, AlgorithmParameters params) throws IOException { if (params != null) { method.setRequestHeader(HEADER_ALGORITHM_PARAMETERS, Base64.encodeBytes(params.getEncoded())); } }
From source file:org.alfresco.httpclient.AbstractHttpClient.java
protected HttpMethod createMethod(Request req) throws IOException { StringBuilder url = new StringBuilder(128); url.append(baseUrl);//from w ww . jav a 2 s . co m url.append("/service/"); url.append(req.getFullUri()); // construct method HttpMethod httpMethod = null; String method = req.getMethod(); if (method.equalsIgnoreCase("GET")) { GetMethod get = new GetMethod(url.toString()); httpMethod = get; httpMethod.setFollowRedirects(true); } else if (method.equalsIgnoreCase("POST")) { PostMethod post = new PostMethod(url.toString()); httpMethod = post; ByteArrayRequestEntity requestEntity = new ByteArrayRequestEntity(req.getBody(), req.getType()); if (req.getBody().length > DEFAULT_SAVEPOST_BUFFER) { post.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, true); } post.setRequestEntity(requestEntity); // Note: not able to automatically follow redirects for POST, this is handled by sendRemoteRequest } else if (method.equalsIgnoreCase("HEAD")) { HeadMethod head = new HeadMethod(url.toString()); httpMethod = head; httpMethod.setFollowRedirects(true); } else { throw new AlfrescoRuntimeException("Http Method " + method + " not supported"); } if (req.getHeaders() != null) { for (Map.Entry<String, String> header : req.getHeaders().entrySet()) { httpMethod.setRequestHeader(header.getKey(), header.getValue()); } } return httpMethod; }