List of usage examples for org.apache.commons.httpclient HttpMethod setRequestHeader
public abstract void setRequestHeader(String paramString1, String paramString2);
From source file:org.alfresco.jive.impl.JiveOpenClientImpl.java
/** * Sets default values for a bunch of standard request headers in the given <code>HttpMethod</code>. * Also sets the custom X-AlfrescoJive-UserId header to the encrypted user id. * /*from w ww . j ava 2 s .c om*/ * @param userId The userId to encrypt and add to the header <i>(must not be null, empty or blank)</i>. * @param method The method to add the headers to <i>(may be null, in which case this method won't do anything)</i>. */ private void setCommonHeaders(final String userId, final HttpMethod method) { if (userId == null || userId.trim().length() == 0) { throw new AuthenticationException(); } if (method != null) { final String encryptedUserId = encrypter.encrypt(userId); method.setDoAuthentication(true); method.setFollowRedirects(false); method.setRequestHeader("Accept", MIME_TYPE_JSON); method.setRequestHeader("Accept-Charset", CHARSET_UTF8); // method.setRequestHeader("Accept-Encoding", "gzip"); // Would dearly love to support compressed responses, but difficult to support in HttpClient 3.1 (used in Alfresco 3.4.x) method.setRequestHeader("Connection", "Keep-Alive"); method.setRequestHeader("Keep-Alive", "300"); method.setRequestHeader("User-Agent", USER_AGENT); method.setRequestHeader(HEADER_NAME_USER_ID, encryptedUserId); } }
From source file:org.alfresco.repo.web.scripts.BaseWebScriptTest.java
/** * Send Remote Request to stand-alone Web Script Server * /*from ww w . jav a 2 s . com*/ * @param req Request * @param expectedStatus int * @return response * @throws IOException */ protected Response sendRemoteRequest(Request req, int expectedStatus) throws IOException { String uri = req.getFullUri(); if (!uri.startsWith("http")) { uri = remoteServer.baseAddress + uri; } // construct method HttpMethod httpMethod = null; String method = req.getMethod(); if (method.equalsIgnoreCase("GET")) { GetMethod get = new GetMethod(req.getFullUri()); httpMethod = get; } else if (method.equalsIgnoreCase("POST")) { PostMethod post = new PostMethod(req.getFullUri()); post.setRequestEntity(new ByteArrayRequestEntity(req.getBody(), req.getType())); httpMethod = post; } else if (method.equalsIgnoreCase("PATCH")) { PatchMethod post = new PatchMethod(req.getFullUri()); post.setRequestEntity(new ByteArrayRequestEntity(req.getBody(), req.getType())); httpMethod = post; } else if (method.equalsIgnoreCase("PUT")) { PutMethod put = new PutMethod(req.getFullUri()); put.setRequestEntity(new ByteArrayRequestEntity(req.getBody(), req.getType())); httpMethod = put; } else if (method.equalsIgnoreCase("DELETE")) { DeleteMethod del = new DeleteMethod(req.getFullUri()); httpMethod = del; } 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()); } } // execute method httpClient.executeMethod(httpMethod); return new HttpMethodResponse(httpMethod); }
From source file:org.alfresco.web.bean.ajax.PresenceProxyBean.java
/** * Perform request//from w ww . j a v a 2 s . c o m */ public String getUrlResponse(String requestUrl) { String response = ""; HttpClient client = new HttpClient(); HttpMethod method = new GetMethod(requestUrl); method.setRequestHeader("Accept", "*/*"); client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); try { int statusCode = client.executeMethod(method); if (statusCode == HttpStatus.SC_OK) { response = method.getResponseBodyAsString(); } else { response = method.getStatusText(); } } catch (HttpException e) { response = e.getMessage(); } catch (IOException e) { response = e.getMessage(); } finally { // Release the connection. method.releaseConnection(); } return response; }
From source file:org.apache.abdera.protocol.client.util.MethodHelper.java
private static void initHeaders(RequestOptions options, HttpMethod method) { String[] headers = options.getHeaderNames(); for (String header : headers) { Object[] values = options.getHeaders(header); for (Object value : values) { method.addRequestHeader(header, value.toString()); }/*from www . j a v a 2 s .com*/ } String cc = options.getCacheControl(); if (cc != null && cc.length() != 0) method.setRequestHeader("Cache-Control", cc); if (options.getAuthorization() != null) method.setDoAuthentication(false); }
From source file:org.apache.axis2.transport.http.AbstractHTTPSender.java
public void addCustomHeaders(HttpMethod method, MessageContext msgContext) { boolean isCustomUserAgentSet = false; // set the custom headers, if available Object httpHeadersObj = msgContext.getProperty(HTTPConstants.HTTP_HEADERS); if (httpHeadersObj != null) { if (httpHeadersObj instanceof ArrayList) { ArrayList httpHeaders = (ArrayList) httpHeadersObj; Header header;/*from w w w. ja v a2 s.co m*/ for (int i = 0; i < httpHeaders.size(); i++) { header = (Header) httpHeaders.get(i); if (HTTPConstants.HEADER_USER_AGENT.equals(header.getName())) { isCustomUserAgentSet = true; } method.addRequestHeader(header); } } if (httpHeadersObj instanceof Map) { Map httpHeaders = (Map) httpHeadersObj; for (Iterator iterator = httpHeaders.entrySet().iterator(); iterator.hasNext();) { Map.Entry entry = (Map.Entry) iterator.next(); String key = (String) entry.getKey(); String value = (String) entry.getValue(); if (HTTPConstants.HEADER_USER_AGENT.equals(key)) { isCustomUserAgentSet = true; } method.addRequestHeader(key, value); } } } // we have to consider the TRANSPORT_HEADERS map as well Map transportHeaders = (Map) msgContext.getProperty(MessageContext.TRANSPORT_HEADERS); if (transportHeaders != null) { removeUnwantedHeaders(msgContext); Set headerEntries = transportHeaders.entrySet(); for (Object headerEntry : headerEntries) { if (headerEntry instanceof Map.Entry) { Header[] headers = method.getRequestHeaders(); boolean headerAdded = false; for (Header header : headers) { if (header.getName() != null) { Object headerKey = ((Map.Entry) headerEntry).getKey(); if (headerKey instanceof String && header.getName().equalsIgnoreCase((String) headerKey)) { // According to RFC2626 http headers are case insensitive. // So need to ignore case headerAdded = true; break; } } } if (!headerAdded) { method.addRequestHeader(((Map.Entry) headerEntry).getKey().toString(), ((Map.Entry) headerEntry).getValue().toString()); } } } } if (!isCustomUserAgentSet) { String userAgentString = getUserAgent(msgContext); method.setRequestHeader(HTTPConstants.HEADER_USER_AGENT, userAgentString); } }
From source file:org.apache.axis2.transport.http.impl.httpclient3.HTTPSenderImpl.java
public void addCustomHeaders(HttpMethod method, MessageContext msgContext) { boolean isCustomUserAgentSet = false; // set the custom headers, if available Object httpHeadersObj = msgContext.getProperty(HTTPConstants.HTTP_HEADERS); if (httpHeadersObj != null) { if (httpHeadersObj instanceof List) { List httpHeaders = (List) httpHeadersObj; for (int i = 0; i < httpHeaders.size(); i++) { NamedValue nv = (NamedValue) httpHeaders.get(i); if (nv != null) { Header header = new Header(nv.getName(), nv.getValue()); if (HTTPConstants.HEADER_USER_AGENT.equals(header.getName())) { isCustomUserAgentSet = true; }/*from w w w . j a va2 s . c o m*/ method.addRequestHeader(header); } } } if (httpHeadersObj instanceof Map) { Map httpHeaders = (Map) httpHeadersObj; for (Iterator iterator = httpHeaders.entrySet().iterator(); iterator.hasNext();) { Map.Entry entry = (Map.Entry) iterator.next(); String key = (String) entry.getKey(); String value = (String) entry.getValue(); if (HTTPConstants.HEADER_USER_AGENT.equals(key)) { isCustomUserAgentSet = true; } method.addRequestHeader(key, value); } } } // we have to consider the TRANSPORT_HEADERS map as well Map transportHeaders = (Map) msgContext.getProperty(MessageContext.TRANSPORT_HEADERS); if (transportHeaders != null) { removeUnwantedHeaders(msgContext); Set headerEntries = transportHeaders.entrySet(); for (Object headerEntry : headerEntries) { if (headerEntry instanceof Map.Entry) { Header[] headers = method.getRequestHeaders(); boolean headerAdded = false; for (Header header : headers) { if (header.getName() != null && header.getName().equals(((Map.Entry) headerEntry).getKey())) { headerAdded = true; break; } } if (!headerAdded) { method.addRequestHeader(((Map.Entry) headerEntry).getKey().toString(), ((Map.Entry) headerEntry).getValue().toString()); } } } } if (!isCustomUserAgentSet) { String userAgentString = getUserAgent(msgContext); method.setRequestHeader(HTTPConstants.HEADER_USER_AGENT, userAgentString); } }
From source file:org.apache.camel.component.jetty.HttpHeaderCaseTest.java
@Test public void testHttpHeaderCase() throws Exception { HttpClient client = new HttpClient(); HttpMethod method = new PostMethod("http://localhost:" + getPort() + "/myapp/mytest"); method.setRequestHeader("clientHeader", "fooBAR"); method.setRequestHeader("OTHER", "123"); method.setRequestHeader("beer", "Carlsberg"); client.executeMethod(method);//from w w w . j av a 2 s . c o m assertEquals("Bye World", method.getResponseBodyAsString()); assertEquals("aBc123", method.getResponseHeader("MyCaseHeader").getValue()); assertEquals("456DEf", method.getResponseHeader("otherCaseHeader").getValue()); }
From source file:org.apache.clerezza.integrationtest.web.performance.Get404.java
@Override public void run() { try {/*from w w w . j av a 2s. c om*/ URL serverURL = new URL(testSubjectUriPrefix + "/foobar"); HttpClient client = new HttpClient(); client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); HttpMethod method = new GetMethod(serverURL.toString()); method.setRequestHeader("Accept", "*/*"); method.setDoAuthentication(true); try { int responseCode = client.executeMethod(method); if (responseCode != HttpStatus.SC_NOT_FOUND) { throw new RuntimeException("Get404: unexpected " + "response code: " + responseCode); } } finally { method.releaseConnection(); } } catch (MalformedURLException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:org.apache.directory.fortress.rest.EmTest.java
/** * Add userId, password to HTTP Basic AuthN header. * * @param httpMethod/*from w w w . j a v a 2 s .c om*/ * @param name * @param password */ private static void setMethodHeaders(HttpMethod httpMethod, String name, String password) { if (httpMethod instanceof PostMethod || httpMethod instanceof PutMethod) { httpMethod.setRequestHeader("Content-Type", "application/xml"); httpMethod.setRequestHeader("Accept", "application/xml"); } httpMethod.setDoAuthentication(true); httpMethod.setRequestHeader("Authorization", "Basic " + base64Encode(name + ":" + password)); }
From source file:org.apache.hadoop.mapred.JobEndNotifier.java
private static int httpNotification(String uri) throws IOException { URI url = new URI(uri, false); HttpClient m_client = new HttpClient(); HttpMethod method = new GetMethod(url.getEscapedURI()); method.setRequestHeader("Accept", "*/*"); return m_client.executeMethod(method); }