List of usage examples for org.apache.commons.httpclient Header Header
public Header(String paramString1, String paramString2)
From source file:ehu.ned.DBpediaSpotlightClient.java
public Document extract_with_endpoint(Text text, String endpoint) throws AnnotationException { LOG.info("Querying API at: " + endpoint); String spotlightResponse = ""; Document doc = null;/* w w w.j av a 2 s.c o m*/ try { PostMethod method = new PostMethod(endpoint); method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); NameValuePair[] params = { new NameValuePair("text", text.text()), new NameValuePair("spotter", "SpotXmlParser"), new NameValuePair("confidence", Double.toString(CONFIDENCE)), new NameValuePair("support", Integer.toString(SUPPORT)) }; method.setRequestBody(params); method.setRequestHeader(new Header("Accept", "text/xml")); spotlightResponse = request(method); doc = loadXMLFromString(spotlightResponse); } catch (javax.xml.parsers.ParserConfigurationException ex) { } catch (org.xml.sax.SAXException ex) { } catch (java.io.IOException ex) { } return doc; }
From source file:ixa.pipe.ned.DBpediaSpotlightClient.java
public Document extract(TextAdaptation text, String host, String port, String endpoint) throws AnnotationException { LOG.info("Querying API."); String spotlightResponse = ""; Document doc = null;// w ww . ja v a 2 s .co m try { String url = host + ":" + port + "/rest/" + endpoint; PostMethod method = new PostMethod(url); method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); NameValuePair[] params = { new NameValuePair("text", text.text()), new NameValuePair("spotter", "SpotXmlParser"), new NameValuePair("confidence", Double.toString(CONFIDENCE)), new NameValuePair("support", Integer.toString(SUPPORT)), new NameValuePair("coreferenceResolution", Boolean.toString(COREFERENCE)) }; method.setRequestBody(params); method.setRequestHeader(new Header("Accept", "text/xml")); spotlightResponse = request(method); doc = loadXMLFromString(spotlightResponse); } catch (javax.xml.parsers.ParserConfigurationException ex) { } catch (org.xml.sax.SAXException ex) { } catch (java.io.IOException ex) { } return doc; }
From source file:com.iflytek.spider.protocol.httpclient.Http.java
/** * Configures the HTTP client/* ww w. ja v a 2s . co m*/ */ private void configureClient() { // Set up an HTTPS socket factory that accepts self-signed certs. Protocol https = new Protocol("https", new DummySSLProtocolSocketFactory(), 443); Protocol.registerProtocol("https", https); HttpConnectionManagerParams params = connectionManager.getParams(); params.setConnectionTimeout(timeout); params.setSoTimeout(timeout); params.setSendBufferSize(BUFFER_SIZE); params.setReceiveBufferSize(BUFFER_SIZE); params.setMaxTotalConnections(maxThreadsTotal); params.setDefaultMaxConnectionsPerHost(maxThreadsTotal); // executeMethod(HttpMethod) seems to ignore the connection timeout on // the connection manager. // set it explicitly on the HttpClient. client.getParams().setConnectionManagerTimeout(timeout); HostConfiguration hostConf = client.getHostConfiguration(); ArrayList headers = new ArrayList(); // Set the User Agent in the header headers.add(new Header("User-Agent", userAgent)); // prefer English headers.add(new Header("Accept-Language", acceptLanguage)); // prefer UTF-8 headers.add(new Header("Accept-Charset", "utf-8,ISO-8859-1;q=0.7,*;q=0.7")); // prefer understandable formats headers.add(new Header("Accept", "text/html,application/xml;q=0.9,application/xhtml+xml,text/xml;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5")); // accept gzipped content headers.add(new Header("Accept-Encoding", "x-gzip, gzip, deflate")); hostConf.getParams().setParameter("http.default-headers", headers); // HTTP proxy server details if (useProxy) { hostConf.setProxy(proxyHost, proxyPort); if (proxyUsername.length() > 0) { AuthScope proxyAuthScope = getAuthScope(this.proxyHost, this.proxyPort, this.proxyRealm); NTCredentials proxyCredentials = new NTCredentials(this.proxyUsername, this.proxyPassword, this.agentHost, this.proxyRealm); client.getState().setProxyCredentials(proxyAuthScope, proxyCredentials); } } }
From source file:com.iflytek.spider.protocol.httpclient.HttpSimply.java
/** * Configures the HTTP client/* ww w . ja va2s. com*/ */ private void configureClient() { // Set up an HTTPS socket factory that accepts self-signed certs. Protocol https = new Protocol("https", new DummySSLProtocolSocketFactory(), 443); Protocol.registerProtocol("https", https); HttpConnectionManagerParams params = connectionManager.getParams(); params.setConnectionTimeout(timeout); //params.setSoTimeout(timeout); timeouttimeout params.setSendBufferSize(BUFFER_SIZE); params.setReceiveBufferSize(BUFFER_SIZE); params.setMaxTotalConnections(maxThreadsTotal); params.setDefaultMaxConnectionsPerHost(maxThreadsTotal); // executeMethod(HttpMethod) seems to ignore the connection timeout on // the connection manager. // set it explicitly on the HttpClient. client.getParams().setConnectionManagerTimeout(timeout); HostConfiguration hostConf = client.getHostConfiguration(); ArrayList headers = new ArrayList(); // Set the User Agent in the header headers.add(new Header("User-Agent", userAgent)); // prefer English headers.add(new Header("Accept-Language", acceptLanguage)); // prefer UTF-8 headers.add(new Header("Accept-Charset", "utf-8,ISO-8859-1;q=0.7,*;q=0.7")); // prefer understandable formats headers.add(new Header("Accept", "text/html,application/xml;q=0.9,application/xhtml+xml,text/xml;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5")); // accept gzipped content headers.add(new Header("Accept-Encoding", "x-gzip, gzip, deflate")); hostConf.getParams().setParameter("http.default-headers", headers); // HTTP proxy server details if (useProxy) { hostConf.setProxy(proxyHost, proxyPort); if (proxyUsername.length() > 0) { AuthScope proxyAuthScope = getAuthScope(this.proxyHost, this.proxyPort, this.proxyRealm); NTCredentials proxyCredentials = new NTCredentials(this.proxyUsername, this.proxyPassword, this.agentHost, this.proxyRealm); client.getState().setProxyCredentials(proxyAuthScope, proxyCredentials); } } }
From source file:com.bigdata.rdf.sail.remoting.GraphRepositoryClient.java
/** * @see {@link GraphRepository#delete(String)} *///from www . ja v a 2 s .c om public void delete(String rdfXml) throws Exception { // DELETE DeleteMethod del = new DeleteMethod(servletURL); try { // add the range header if (rdfXml != null) { String triples = "triples[" + trim(rdfXml) + "]"; Header range = new Header(GraphRepositoryServlet.HTTP_RANGE, triples); del.addRequestHeader(range); } // Execute the method. int sc = getHttpClient().executeMethod(del); if (sc != HttpStatus.SC_OK) { throw new IOException("HTTP-DELETE failed: " + del.getStatusLine()); } } finally { // Release the connection. del.releaseConnection(); } }
From source file:cdr.forms.SwordDepositHandler.java
public DepositResult deposit(Deposit deposit) { // Prepare the submission package Submission submission = Submission.create(deposit, this); File zipFile = makeZipFile(submission.getMetsDocumentRoot(), submission.getFiles()); // Obtain the path for the collection in which we'll attempt to make the deposit Form form = deposit.getForm();/* www .j av a 2s .c o m*/ String containerId = form.getDepositContainerId(); if (containerId == null || "".equals(containerId.trim())) containerId = this.getDefaultContainer(); String depositPath = getServiceUrl() + "collection/" + containerId; // Make the SWORD request String pid = "uuid:" + UUID.randomUUID().toString(); HttpClient client = new HttpClient(); UsernamePasswordCredentials creds = new UsernamePasswordCredentials(this.getUsername(), this.getPassword()); client.getState().setCredentials(getAuthenticationScope(depositPath), creds); client.getParams().setAuthenticationPreemptive(true); PostMethod post = new PostMethod(depositPath); RequestEntity fileRequestEntity = new FileRequestEntity(zipFile, "application/zip"); Header contentDispositionHeader = new Header("Content-Disposition", "attachment; filename=package.zip"); post.addRequestHeader(contentDispositionHeader); Header packagingHeader = new Header("Packaging", "http://cdr.unc.edu/METS/profiles/Simple"); post.addRequestHeader(packagingHeader); Header slugHeader = new Header("Slug", pid); post.addRequestHeader(slugHeader); post.setRequestEntity(fileRequestEntity); // Interpret the response from the SWORD endpoint DepositResult result = new DepositResult(); try { // Set the result's status based on the HTTP response code int responseCode = client.executeMethod(post); if (responseCode >= 300) { LOG.error(String.valueOf(responseCode)); LOG.error(post.getResponseBodyAsString()); result.setStatus(Status.FAILED); } else { result.setStatus(Status.COMPLETE); } // Save the response body result.setResponseBody(post.getResponseBodyAsString()); // Assign additional attributes based on the response body. try { Namespace atom = Namespace.getNamespace("http://www.w3.org/2005/Atom"); SAXBuilder sx = new SAXBuilder(); org.jdom.Document d = sx.build(post.getResponseBodyAsStream()); // Set accessURL to the href of the first <link rel="alternate"> inside an Atom entry if (result.getStatus() == Status.COMPLETE) { if (d.getRootElement().getNamespace().equals(atom) && d.getRootElement().getName().equals("entry")) { @SuppressWarnings("unchecked") List<Element> links = d.getRootElement().getChildren("link", atom); for (Element link : links) { if ("alternate".equals(link.getAttributeValue("rel"))) { result.setAccessURL(link.getAttributeValue("href")); break; } } } } } catch (JDOMException e) { LOG.error("There was a problem parsing the SWORD response.", e); } LOG.debug("response was: \n" + post.getResponseBodyAsString()); } catch (HttpException e) { LOG.error("Exception during SWORD deposit", e); throw new Error(e); } catch (IOException e) { LOG.error("Exception during SWORD deposit", e); throw new Error(e); } return result; }
From source file:com.cyberway.issue.crawler.extractor.ExtractorTool.java
protected CrawlURI getCrawlURI(final ARCRecord record, final HttpRecorder hr) throws URIException { CrawlURI curi = new CrawlURI(UURIFactory.getInstance(record.getMetaData().getUrl())); curi.setContentSize(record.getMetaData().getLength()); curi.setContentType(record.getMetaData().getMimetype()); curi.setHttpRecorder(hr);//from w ww . java2 s. co m // Fake out the extractor that this is a legit HTTP transaction. if (!curi.getUURI().getScheme().equals("filedesc")) { curi.putObject(CoreAttributeConstants.A_HTTP_TRANSACTION, new HttpMethodBase() { public String getName() { return this.getClass().getName() + "_method"; } public Header getResponseHeader(String headerName) { String value = (String) record.getMetaData().getHeaderValue(headerName); return (value == null || value.length() == 0) ? null : new Header(headerName, value); } }); String statusCode = record.getMetaData().getStatusCode(); curi.setFetchStatus(statusCode == null ? 200 : Integer.parseInt(statusCode)); } return curi; }
From source file:com.tasktop.c2c.server.common.service.tests.http.HttpProxyTest.java
@Test public void testBasicGet() throws IOException { setupMock(HttpStatus.OK);// w w w. ja v a 2 s . c o m MockHttpServletRequest clientRequest = new MockHttpServletRequest("GET", "unused"); String randomRequestHeader = "RandomRequestHeader"; String randomResponseHeader = "RandomResponseHeader"; String connectionHeader = "Connection"; clientRequest.addHeader(randomRequestHeader, "RandomHeaderValue"); clientRequest.addHeader(connectionHeader, "ConnectionValue"); proxyResponseHeaders.add(new Header(randomResponseHeader, "RandomHeaderValue")); proxyResponseHeaders.add(new Header(connectionHeader, "ConnectionValue")); // Should not pass along MockHttpServletResponse clientResponse = new MockHttpServletResponse(); proxy.proxyRequest("foo", clientRequest, clientResponse); Assert.assertNotNull(getProxyRequestHeaderValues(randomRequestHeader)); Assert.assertNull(getProxyRequestHeaderValues(connectionHeader)); Assert.assertTrue(clientResponse.containsHeader(randomResponseHeader)); Assert.assertFalse(clientResponse.containsHeader(connectionHeader)); // FIXME, unsure if this req is correct context.assertIsSatisfied(); }
From source file:com.zimbra.common.soap.SoapHttpTransport.java
public Element invoke(Element document, boolean raw, boolean noSession, String requestedAccountId, String changeToken, String tokenType, ResponseHandler respHandler) throws IOException, HttpException, ServiceException { PostMethod method = null;// w w w . ja v a 2s .c o m try { // Assemble post method. Append document name, so that the request // type is written to the access log. String uri, query; int i = mUri.indexOf('?'); if (i >= 0) { uri = mUri.substring(0, i); query = mUri.substring(i); } else { uri = mUri; query = ""; } if (!uri.endsWith("/")) uri += '/'; uri += getDocumentName(document); method = new PostMethod(uri + query); // Set user agent if it's specified. String agentName = getUserAgentName(); if (agentName != null) { String agentVersion = getUserAgentVersion(); if (agentVersion != null) agentName += " " + agentVersion; method.setRequestHeader(new Header("User-Agent", agentName)); } // the content-type charset will determine encoding used // when we set the request body method.setRequestHeader("Content-Type", getRequestProtocol().getContentType()); if (getClientIp() != null) { method.setRequestHeader(RemoteIP.X_ORIGINATING_IP_HEADER, getClientIp()); if (ZimbraLog.misc.isDebugEnabled()) { ZimbraLog.misc.debug("set remote IP header [%s] to [%s]", RemoteIP.X_ORIGINATING_IP_HEADER, getClientIp()); } } Element soapReq = generateSoapMessage(document, raw, noSession, requestedAccountId, changeToken, tokenType); String soapMessage = SoapProtocol.toString(soapReq, getPrettyPrint()); HttpMethodParams params = method.getParams(); method.setRequestEntity(new StringRequestEntity(soapMessage, null, "UTF-8")); if (getRequestProtocol().hasSOAPActionHeader()) method.setRequestHeader("SOAPAction", mUri); if (mCustomHeaders != null) { for (Map.Entry<String, String> entry : mCustomHeaders.entrySet()) method.setRequestHeader(entry.getKey(), entry.getValue()); } String host = method.getURI().getHost(); HttpState state = HttpClientUtil.newHttpState(getAuthToken(), host, this.isAdmin()); String trustedToken = getTrustedToken(); if (trustedToken != null) { state.addCookie( new Cookie(host, ZimbraCookie.COOKIE_ZM_TRUST_TOKEN, trustedToken, "/", null, false)); } params.setCookiePolicy(state.getCookies().length == 0 ? CookiePolicy.IGNORE_COOKIES : CookiePolicy.BROWSER_COMPATIBILITY); params.setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(mRetryCount - 1, true)); params.setSoTimeout(mTimeout); params.setVersion(HttpVersion.HTTP_1_1); method.setRequestHeader("Connection", mKeepAlive ? "Keep-alive" : "Close"); if (mHostConfig != null && mHostConfig.getUsername() != null && mHostConfig.getPassword() != null) { state.setProxyCredentials(new AuthScope(null, -1), new UsernamePasswordCredentials(mHostConfig.getUsername(), mHostConfig.getPassword())); } if (mHttpDebugListener != null) { mHttpDebugListener.sendSoapMessage(method, soapReq, state); } int responseCode = mClient.executeMethod(mHostConfig, method, state); // SOAP allows for "200" on success and "500" on failure; // real server issues will probably be "503" or "404" if (responseCode != HttpServletResponse.SC_OK && responseCode != HttpServletResponse.SC_INTERNAL_SERVER_ERROR) throw ServiceException.PROXY_ERROR(method.getStatusLine().toString(), uri); // Read the response body. Use the stream API instead of the byte[] // version to avoid HTTPClient whining about a large response. InputStreamReader reader = new InputStreamReader(method.getResponseBodyAsStream(), SoapProtocol.getCharset()); String responseStr = ""; try { if (respHandler != null) { respHandler.process(reader); return null; } else { responseStr = ByteUtil.getContent(reader, (int) method.getResponseContentLength(), false); Element soapResp = parseSoapResponse(responseStr, raw); if (mHttpDebugListener != null) { mHttpDebugListener.receiveSoapMessage(method, soapResp); } return soapResp; } } catch (SoapFaultException x) { // attach request/response to the exception and rethrow x.setFaultRequest(soapMessage); x.setFaultResponse(responseStr.substring(0, Math.min(10240, responseStr.length()))); throw x; } } finally { // Release the connection to the connection manager if (method != null) method.releaseConnection(); // really not necessary if running in the server because the reaper thread // of our connection manager will take care it. // if called from CLI, all connections will be closed when the CLI // exits. Leave it here anyway. if (!mKeepAlive) mClient.getHttpConnectionManager().closeIdleConnections(0); } }
From source file:com.google.gsa.valve.modules.httpbasic.HTTPBasicAuthorizationProcess.java
/** * /* w w w .j a v a2 s. c o m*/ * This is the main method that does the authorization and should be * invoked by the classes that would like to check if the user is * priviledged to access to the document (url) against the HTTP Basic * protected source that serves it. * <p> * The Basic credential is read from the cookie created during the * authentication process. If that cookie does not exist, the process returns * a 401 (Unauthorized) error code. * <p> * If it is a HEAD request, it means it's not necessary to get the content, * and that's why this process only cares about the HTTP result code. That * result code is returned back to the caller, and if the request is not a * HEAD (i.e. usually GET), the content is sent as well if the overall * result is OK. * * @param request HTTP request * @param response HTTP response * @param authCookies vector that contains the authentication cookies * @param url the document url * @param id the default credential id * * @return the HTTP error code * * @throws HttpException * @throws IOException */ public int authorize(HttpServletRequest request, HttpServletResponse response, Cookie[] authCookies, String url, String id) throws HttpException, IOException { logger.debug("HTTP Basic Authorization"); String loginUrl = null; loginUrl = valveConf.getLoginUrl(); //Get Max connections maxConnectionsPerHost = new Integer(valveConf.getMaxConnectionsPerHost()).intValue(); maxTotalConnections = (new Integer(valveConf.getMaxTotalConnections())).intValue(); logger.debug("HttpBasic AuthZ maxConnectionsPerHost: " + maxConnectionsPerHost); logger.debug("HttpBasic AuthZ maxTotalConnections: " + maxTotalConnections); // Protection if (webProcessor == null) { // Instantiate Web processor if ((maxConnectionsPerHost != -1) && (maxTotalConnections != -1)) { webProcessor = new WebProcessor(maxConnectionsPerHost, maxTotalConnections); } else { webProcessor = new WebProcessor(); } } //protection authHeader = null; headers = null; //Get the http AuthZ header Cookie[] requestCookies = null; //add support to authCookies requestCookies = authCookies; // Protection logger.debug("Checking request cookies"); if (requestCookies != null) { // Check if the authentication process already happened by looking at the existing cookie // The gsa_basic_auth cookie contains the HTTP Basic AuthZ header logger.debug("Number of cookies: " + requestCookies.length); for (int i = 0; i < requestCookies.length; i++) { // Check cookie name logger.debug("request cookie: " + requestCookies[i].getName() + ":" + requestCookies[i].getValue()); if ((requestCookies[i].getName()).equals(BASIC_COOKIE)) { if (requestCookies[i].getValue() != null) { logger.debug(BASIC_COOKIE + ": " + requestCookies[i].getValue()); String basicCookie = null; try { basicCookie = URLDecoder.decode(requestCookies[i].getValue(), encoder); if ((basicCookie != null) && (!basicCookie.equals(""))) { authHeader = new Header("Authorization", setBasicAuthNChain(basicCookie)); } } catch (Exception ex) { logger.error("Error when getting cookie value: " + ex.getMessage(), ex); } } } } } // // Launch the authorization process // // Initialize status code int statusCode = HttpServletResponse.SC_UNAUTHORIZED; if (authHeader == null) { // no authZ header, can't auth this URL logger.debug("No authZ header"); return statusCode; } else { //is a Head request? boolean isHead = AuthorizationUtils.isHead(request, valveConf); logger.debug("isHead?: " + isHead); setHeaders(); // Protection if (webProcessor != null) { // Protection try { // Process authz request String requestType = RequestType.GET_REQUEST; if (isHead) { requestType = RequestType.HEAD_REQUEST; } method = webProcessor.sendRequest(null, requestType, headers, null, url); // Protection if (method != null) { // Cache status code statusCode = method.getStatusCode(); logger.debug("statusCode is.... " + statusCode); if (statusCode == HttpServletResponse.SC_OK) { //check if it's a Head request if (!isHead) { //Process content HTTPAuthZProcessor.processResponse(response, method, url, loginUrl); } } else { logger.debug("not AuthZ : should return response Code"); } } // Garbagge collect if (method != null) { method.releaseConnection(); method = null; } } catch (Exception e) { // Log error logger.error("authorization failure: " + e.getMessage(), e); statusCode = HttpServletResponse.SC_INTERNAL_SERVER_ERROR; // Garbagge collect method.releaseConnection(); method = null; } } // // End of the authorization process // // Return status code return statusCode; } }