List of usage examples for org.apache.commons.httpclient HttpClient getParams
public HttpClientParams getParams()
From source file:edu.unc.lib.dl.ui.service.FedoraContentService.java
private void streamData(String simplepid, Datastream datastream, String slug, HttpServletResponse response, boolean asAttachment, int retryServerError) throws FedoraException, IOException { OutputStream outStream = response.getOutputStream(); String dataUrl = fedoraUtil.getFedoraUrl() + "/objects/" + simplepid + "/datastreams/" + datastream.getName() + "/content"; HttpClient client = HttpClientUtil.getAuthenticatedClient(dataUrl, accessClient.getUsername(), accessClient.getPassword()); client.getParams().setAuthenticationPreemptive(true); GetMethod method = new GetMethod(dataUrl); method.addRequestHeader(HttpClientUtil.FORWARDED_GROUPS_HEADER, GroupsThreadStore.getGroupString()); try {// w w w.java 2s . c o m client.executeMethod(method); if (method.getStatusCode() == HttpStatus.SC_OK) { if (response != null) { PID pid = new PID(simplepid); // Adjusting content related headers // Use the content length from Fedora it is not provided or negative, in which case use solr's long contentLength; try { String contentLengthString = method.getResponseHeader("content-length").getValue(); contentLength = Long.parseLong(contentLengthString); } catch (Exception e) { // If the content length wasn't provided or wasn't a number, set it to -1 contentLength = -1L; } if (contentLength < 0L) { contentLength = datastream.getFilesize(); } response.setHeader("Content-Length", Long.toString(contentLength)); // Use Fedora's content type unless it is unset or octet-stream String mimeType; try { mimeType = method.getResponseHeader("content-type").getValue(); if (mimeType == null || "application/octet-stream".equals(mimeType)) { if ("mp3".equals(datastream.getExtension())) { mimeType = "audio/mpeg"; } else { mimeType = datastream.getMimetype(); } } } catch (Exception e) { mimeType = datastream.getMimetype(); } response.setHeader("Content-Type", mimeType); // Setting the filename header for the response if (slug == null) { slug = pid.getPid(); } // For metadata types files, append the datastream name if (datastream.getDatastreamCategory().equals(DatastreamCategory.METADATA) || datastream.getDatastreamCategory().equals(DatastreamCategory.ADMINISTRATIVE)) { slug += "_" + datastream.getName(); } // Add the file extension unless its already in there. if (datastream.getExtension() != null && datastream.getExtension().length() > 0 && !slug.toLowerCase().endsWith("." + datastream.getExtension()) && !"unknown".equals(datastream.getExtension())) { slug += "." + datastream.getExtension(); } if (asAttachment) { response.setHeader("content-disposition", "attachment; filename=\"" + slug + "\""); } else { response.setHeader("content-disposition", "inline; filename=\"" + slug + "\""); } } // Stream the content FileIOUtil.stream(outStream, method); } else if (method.getStatusCode() == HttpStatus.SC_FORBIDDEN) { throw new AuthorizationException( "User does not have sufficient permissions to retrieve the specified object"); } else { // Retry server errors if (method.getStatusCode() == 500 && retryServerError > 0) { LOG.warn("Failed to retrieve " + dataUrl + ", retrying."); this.streamData(simplepid, datastream, slug, response, asAttachment, retryServerError - 1); } else { throw new ResourceNotFoundException("Failure to stream fedora content due to response of: " + method.getStatusLine().toString() + "\nPath was: " + dataUrl); } } } catch (ClientAbortException e) { if (LOG.isDebugEnabled()) LOG.debug("User client aborted request to stream Fedora content for " + simplepid, e); } catch (HttpException e) { LOG.error("Error while attempting to stream Fedora content for " + simplepid, e); } catch (IOException e) { LOG.warn("Problem retrieving " + dataUrl + " for " + simplepid, e); } finally { if (method != null) method.releaseConnection(); } }
From source file:de.extra.client.plugins.outputplugin.transport.ExtraTransportHttp.java
/** * Sets up the HttpClient by setting Parameters. * /*from w w w . j a va2 s. c o m*/ * @param extraConnectData * @param client */ private void setupHttpClient(final HttpOutputPluginConnectConfiguration extraConnectData, final HttpClient client) { // Setup user agent and charset client.getParams().setParameter("http.useragent", extraConnectData.getUserAgent()); client.getParams().setParameter("http.protocol.content-charset", ExtraConstants.REQ_ENCODING); }
From source file:com.celamanzi.liferay.portlets.rails286.OnlineClient.java
/** * Prepares client./* www . j av a2s .c o m*/ */ protected HttpClient preparedClient() { // Create an instance of HttpClient and prepare it HttpClient client = new HttpClient(); client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); client.getParams().setParameter(HttpMethodParams.SINGLE_COOKIE_HEADER, true); // magic cookie line (all cookies on one header line) client.getParams().setParameter("http.protocol.single-cookie-header", true); // Set state (session cookies) client.setState(preparedHttpState()); // Set timeout client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout); return client; }
From source file:com.basho.riak.client.util.TestClientUtils.java
@Test public void newHttpClient_sets_retry_handler() { final HttpMethodRetryHandler handler = mock(HttpMethodRetryHandler.class); config.setRetryHandler(handler);//ww w .ja v a 2 s . c o m HttpClient httpClient = ClientUtils.newHttpClient(config); assertSame(handler, httpClient.getParams().getParameter(HttpClientParams.RETRY_HANDLER)); }
From source file:com.basho.riak.client.util.TestClientUtils.java
@Test public void newHttpClient_sets_connection_timeout() { final int timeout = 11; config.setTimeout(timeout);// w w w .j a va2s .co m HttpClient httpClient = ClientUtils.newHttpClient(config); assertEquals(timeout, httpClient.getParams().getConnectionManagerTimeout()); assertEquals(timeout, httpClient.getParams().getSoTimeout()); assertEquals(timeout, httpClient.getHttpConnectionManager().getParams().getConnectionTimeout()); }
From source file:com.baidu.qa.service.test.client.HttpReqImpl.java
/** * use httpclient/*from ww w . j av a 2 s . c o m*/ * @param file * @param config * @param vargen * @return */ public Object requestHttpByHttpClient(File file, Config config, VariableGenerator vargen) { FileCharsetDetector det = new FileCharsetDetector(); try { String oldcharset = det.guestFileEncoding(file); if (oldcharset.equalsIgnoreCase("UTF-8") == false) FileUtil.transferFile(file, oldcharset, "UTF-8"); } catch (Exception ex) { log.error("[change expect file charset error]:" + ex); } Map<String, String> datalist = FileUtil.getMapFromFile(file, "="); if (datalist.size() <= 1) { return true; } if (!datalist.containsKey(Constant.KW_ITEST_HOST) && !datalist.containsKey(Constant.KW_ITEST_URL)) { log.error("[wrong file]:" + file.getName() + " hasn't Url"); return null; } if (datalist.containsKey(Constant.KW_ITEST_HOST)) { this.url = datalist.get(Constant.KW_ITEST_HOST); this.hashost = true; datalist.remove(Constant.KW_ITEST_HOST); } else { String action = datalist.get(Constant.KW_ITEST_URL); if (config.getHost().lastIndexOf("/") == config.getHost().length() - 1 && action.indexOf("/") == 0) { action = action.substring(1); } this.url = config.getHost() + action; datalist.remove("itest_url"); } if (datalist.containsKey(Constant.KW_ITEST_EXPECT)) { this.itest_expect = datalist.get(Constant.KW_ITEST_EXPECT); datalist.remove(Constant.KW_ITEST_EXPECT); } if (datalist.containsKey(Constant.KW_ITEST_JSON)) { this.itest_expect_json = datalist.get(Constant.KW_ITEST_JSON); datalist.remove(Constant.KW_ITEST_JSON); } parammap = datalist; this.config = config; //HttpClient HttpClient httpClient = new HttpClient(); // httpClient.setConnectionTimeout(30000); httpClient.setTimeout(30000); httpClient.getParams().setParameter("http.protocol.content-charset", "UTF-8"); PostMethod postMethod = new PostMethod(url); if (hashost == false) { //cookie copy if (config.getVariable() != null && config.getVariable().containsKey(Constant.V_CONFIG_VARIABLE_COOKIE)) { postMethod.addRequestHeader(Constant.V_CONFIG_VARIABLE_COOKIE, (String) config.getVariable().get(Constant.V_CONFIG_VARIABLE_COOKIE)); log.info("[HTTP Request Cookie]" + (String) config.getVariable().get(Constant.V_CONFIG_VARIABLE_COOKIE)); } } //??keysparams??body??key=value? if (parammap.size() == 1 && (parammap.containsKey("params") || parammap.containsKey("Params"))) { String key = ""; if (parammap.containsKey("params")) { key = "params"; } else if (parammap.containsKey("Params")) { key = "Params"; } postMethod.setRequestHeader("Content-Type", "text/json;charset=utf-8"); postMethod.setRequestBody(parammap.get(key).toString()); } else { NameValuePair[] data = new NameValuePair[parammap.size()]; int i = 0; for (Map.Entry<String, String> entry : parammap.entrySet()) { log.info("[HTTP post params]" + (String) entry.getKey() + ":" + (String) entry.getValue() + ";"); if (entry.getValue().toString().contains("###")) { } else { data[i] = new NameValuePair((String) entry.getKey(), (String) entry.getValue()); } i++; } // ?postMethod postMethod.setRequestBody(data); } Assert.assertNotNull("get request error,check the input file", postMethod); String response = ""; // postMethod try { int statusCode = httpClient.executeMethod(postMethod); // HttpClient????POSTPUT??? // 301302 if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) { // ??? Header locationHeader = postMethod.getResponseHeader("location"); String location = null; if (locationHeader != null) { location = locationHeader.getValue(); log.info("The page was redirected to:" + location); } else { log.info("Location field value is null."); } } //? byte[] responseBody = postMethod.getResponseBody(); if (responseBody == null) { log.error("[HTTP response is null]:please check login or servlet"); return ""; } //?utf-8 response = new String(responseBody, "UTF-8"); //? log.info("[The Post Request's Response][" + url + "]" + response); // responseoutput File resfile = FileUtil.rewriteFile(file.getParentFile().getParent() + Constant.FILENAME_OUTPUT, file.getName().substring(0, file.getName().indexOf(".")) + ".response", response); // vargen.processProps(resfile); //?? if (this.itest_expect != null && this.itest_expect.trim().length() != 0) { Assert.assertTrue( "response different with expect:[expect]:" + this.itest_expect + "[actual]:" + response, response.contains(this.itest_expect)); } // if(this.itest_expect_json!=null&&this.itest_expect_json.trim().length()!=0){ // VerifyJsonTypeResponseImpl.verifyResponseWithJson(this.itest_expect_json,response); // // } } catch (HttpException e) { //????? log.error("Please check your provided http address!" + e.getMessage()); } catch (IOException e) { //? log.error(e.getMessage()); } catch (Exception e) { log.error("[HTTP REQUEST ERROR]:", e); //case fail throw new RuntimeException("HTTP REQUEST ERROR:" + e.getMessage()); } finally { // postMethod.releaseConnection(); } return response; }
From source file:easyshop.downloadhelper.HttpPageGetter.java
public HttpPage getHttpPage(PageRef thisRef, HttpClient client, String charSet) { client.getParams().setParameter(HttpMethodParams.USER_AGENT, HTTP_USER_AGENT); //?IE String urlStr = thisRef.getUrlStr(); // try { // urlStr=new String(urlStr.getBytes("utf-8"),"gb2312"); // } catch (UnsupportedEncodingException e1) { // // log error here // log.error(e1.getMessage()); // }//from ww w . j a v a2s .c om // System.out.println(urlStr); // get.setRequestHeader("connection","keep-alive"); GetMethod get = null; try { get = new GetMethod(urlStr); get.setFollowRedirects(true); //???? long startTime = System.currentTimeMillis(); int iGetResultCode = client.executeMethod(get); Header[] rheaders = get.getRequestHeaders(); Header[] headers = get.getResponseHeaders(); boolean is11 = get.isHttp11(); boolean redirect = get.getFollowRedirects(); if (get.getResponseContentLength() >= 2024000) { log.info("content is too large, can't download!"); ConnResponse conRes = new ConnResponse(null, null, 0, 0, 0); return new OriHttpPage(-1, urlStr, null, null, conRes, null); } BufferedInputStream remoteBIS = new BufferedInputStream(get.getResponseBodyAsStream()); ByteArrayOutputStream baos = new ByteArrayOutputStream(10240); byte[] buf = new byte[1024]; int bytesRead = 0; while (bytesRead >= 0) { baos.write(buf, 0, bytesRead); bytesRead = remoteBIS.read(buf); } remoteBIS.close(); byte[] content = baos.toByteArray(); // byte[] content=get.getResponseBody(); long timeTaken = System.currentTimeMillis() - startTime; if (timeTaken < 100) timeTaken = 500; int bytesPerSec = (int) ((double) content.length / ((double) timeTaken / 1000.0)); // log.info("Downloaded " + content.length + " bytes, " + bytesPerSec + " bytes/sec"); // log.info("urlstr:"+urlStr); ConnResponse conRes = new ConnResponse(get.getResponseHeader("Content-type").getValue(), null, 0, 0, get.getStatusCode()); String charset = conRes.getCharSet(); if (charset == null) { String cc = new String(content); if (cc.indexOf("content=\"text/html; charset=gb2312") > 0) charset = "gb2312"; else if (cc.indexOf("content=\"text/html; charset=utf-8") > 0) charset = "utf-8"; else if (cc.indexOf("content=\"text/html; charset=gbk") > 0) charset = "gbk"; } return new HttpPage(urlStr, content, conRes, charset); } catch (IOException ioe) { log.warn("Caught IO Exception: " + ioe.getMessage(), ioe); failureCount++; ConnResponse conRes = new ConnResponse(null, null, 0, 0, 0); return new OriHttpPage(-1, urlStr, null, null, conRes, null); } catch (Exception e) { log.warn("Caught Exception: " + e.getMessage(), e); failureCount++; ConnResponse conRes = new ConnResponse(null, null, 0, 0, 0); return new OriHttpPage(-1, urlStr, null, null, conRes, null); } finally { get.releaseConnection(); } /**/ }
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;/*from w w w. j av a 2 s . co m*/ try { 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.linkedin.pinot.common.http.MultiGetRequest.java
/** * GET urls in parallel using the executor service. * @param urls absolute URLs to GET/*from w w w . j ava2 s .c om*/ * @param timeoutMs timeout in milliseconds for each GET request * @return instance of CompletionService. Completion service will provide * results as they arrive. The order is NOT same as the order of URLs */ public CompletionService<GetMethod> execute(@Nonnull List<String> urls, final int timeoutMs) { Preconditions.checkNotNull(urls); Preconditions.checkArgument(timeoutMs > 0, "Timeout value for multi-get must be greater than 0"); CompletionService<GetMethod> completionService = new ExecutorCompletionService<>(executor); for (final String url : urls) { completionService.submit(new Callable<GetMethod>() { @Override public GetMethod call() throws Exception { HttpClient client = new HttpClient(connectionManager); GetMethod getMethod = new GetMethod(url); getMethod.getParams().setSoTimeout(timeoutMs); // if all connections in the connection manager are busy this will wait to retrieve a connection // set time to wait to retrieve a connection from connection manager client.getParams().setConnectionManagerTimeout(timeoutMs); client.executeMethod(getMethod); return getMethod; } }); } return completionService; }
From source file:fedora.localservices.saxon.SaxonServlet.java
/** * Get the content at the given location using the configured credentials * (if any).//from www .jav a2 s . c om */ private InputStream getInputStream(String url) throws Exception { GetMethod getMethod = new GetMethod(url); HttpClient client = new HttpClient(m_cManager); UsernamePasswordCredentials creds = getCreds(url); if (creds != null) { client.getState().setCredentials(AuthScope.ANY, creds); client.getParams().setAuthenticationPreemptive(true); getMethod.setDoAuthentication(true); } getMethod.setFollowRedirects(true); HttpInputStream in = new HttpInputStream(client, getMethod, url); if (in.getStatusCode() != 200) { try { in.close(); } catch (Exception e) { } throw new IOException("HTTP request failed. Got status code " + in.getStatusCode() + " from remote server while attempting to GET " + url); } else { return in; } }