List of usage examples for org.apache.commons.httpclient.methods GetMethod GetMethod
public GetMethod(String uri)
From source file:com.sun.faban.harness.agent.Download.java
private void downloadDir(URL url, File dir) throws IOException { String dirName = dir.getName(); logger.finer("Creating directory " + dirName + " from " + url.toString()); dir.mkdir();/* ww w . ja v a 2 s. com*/ GetMethod get = new GetMethod(url.toString()); HttpClient client = new HttpClient(); client.getHttpConnectionManager().getParams().setConnectionTimeout(2000); int status = client.executeMethod(get); if (status != HttpStatus.SC_OK) throw new IOException( "Download request for " + url + " returned " + HttpStatus.getStatusText(status) + '.'); InputStream stream = get.getResponseBodyAsStream(); DirectoryParser parser = new DirectoryParser(); int length = 0; int bufEnd = 0; while (length != -1) { length = stream.read(buffer, bufEnd, buffer.length - bufEnd); if (length != -1) bufEnd += length; if (bufEnd > buffer.length / 4 * 3 || length == -1) { if (!parser.parse(buffer, bufEnd)) { String msg = "URL " + url.toString() + " is not a directory!"; logger.severe(msg); throw new IOException(msg); } bufEnd = 0; } } stream.close(); String[] entries = parser.getEntries(); for (int i = 0; i < entries.length; i++) { String entry = entries[i]; if ("META-INF/".equals(entry)) continue; String name = entry.substring(0, entry.length() - 1); if (entry.endsWith("/")) { // Directory downloadDir(new URL(url, entry), new File(dir, name)); } else { downloadFile(new URL(url, name), new File(dir, name)); } } }
From source file:com.noelios.restlet.ext.httpclient.HttpMethodCall.java
/** * Constructor./*from www.j a va 2 s . c o m*/ * * @param helper * The parent HTTP client helper. * @param method * The method name. * @param requestUri * The request URI. * @param hasEntity * Indicates if the call will have an entity to send to the * server. * @throws IOException */ public HttpMethodCall(HttpClientHelper helper, final String method, String requestUri, boolean hasEntity) throws IOException { super(helper, method, requestUri); this.clientHelper = helper; if (requestUri.startsWith("http")) { if (method.equalsIgnoreCase(Method.GET.getName())) { this.httpMethod = new GetMethod(requestUri); } else if (method.equalsIgnoreCase(Method.POST.getName())) { this.httpMethod = new PostMethod(requestUri); } else if (method.equalsIgnoreCase(Method.PUT.getName())) { this.httpMethod = new PutMethod(requestUri); } else if (method.equalsIgnoreCase(Method.HEAD.getName())) { this.httpMethod = new HeadMethod(requestUri); } else if (method.equalsIgnoreCase(Method.DELETE.getName())) { this.httpMethod = new DeleteMethod(requestUri); } else if (method.equalsIgnoreCase(Method.CONNECT.getName())) { final HostConfiguration host = new HostConfiguration(); host.setHost(new URI(requestUri, false)); this.httpMethod = new ConnectMethod(host); } else if (method.equalsIgnoreCase(Method.OPTIONS.getName())) { this.httpMethod = new OptionsMethod(requestUri); } else if (method.equalsIgnoreCase(Method.TRACE.getName())) { this.httpMethod = new TraceMethod(requestUri); } else { this.httpMethod = new EntityEnclosingMethod(requestUri) { @Override public String getName() { return method; } }; } this.httpMethod.setFollowRedirects(this.clientHelper.isFollowRedirects()); this.httpMethod.setDoAuthentication(false); if (this.clientHelper.getRetryHandler() != null) { try { this.httpMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, Engine.loadClass(this.clientHelper.getRetryHandler()).newInstance()); } catch (Exception e) { this.clientHelper.getLogger().log(Level.WARNING, "An error occurred during the instantiation of the retry handler.", e); } } this.responseHeadersAdded = false; setConfidential(this.httpMethod.getURI().getScheme().equalsIgnoreCase(Protocol.HTTPS.getSchemeName())); } else { throw new IllegalArgumentException("Only HTTP or HTTPS resource URIs are allowed here"); } }
From source file:HTTPChunkLocator.java
private InputStream openStream(final URI aUri) throws IOException { final GetMethod method = new GetMethod(aUri.toString()); method.setFollowRedirects(true);/*from ww w . ja va 2s.c o m*/ client.executeMethod(method); final int statusCode = method.getStatusCode(); if (statusCode != 200) { method.abort(); throw new IOException(String.format("HTTP Status Code %s for URL %s", statusCode, aUri)); } return method.getResponseBodyAsStream(); }
From source file:com.testmax.util.FileDownLoader.java
public String downloader(WebElement element, String attribute) throws Exception { //Assuming that getAttribute does some magic to return a fully qualified URL String downloadLocation = element.getAttribute(attribute); if (downloadLocation.trim().equals("")) { throw new Exception("The element you have specified does not link to anything!"); }//from ww w . ja v a 2 s . c o m URL downloadURL = new URL(downloadLocation); HttpClient client = new HttpClient(); client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); client.setHostConfiguration(mimicHostConfiguration(downloadURL.getHost(), downloadURL.getPort())); client.setState(mimicCookieState(driver.manage().getCookies())); HttpMethod getRequest = new GetMethod(downloadURL.getPath()); String file_path = downloadPath + downloadURL.getFile().replaceFirst("/|\\\\", ""); FileWriter downloadedFile = new FileWriter(file_path, true); try { int status = client.executeMethod(getRequest); WmLog.getCoreLogger().info("HTTP Status {} when getting '{}'" + status + downloadURL.toExternalForm()); BufferedInputStream in = new BufferedInputStream(getRequest.getResponseBodyAsStream()); int offset = 0; int len = 4096; int bytes = 0; byte[] block = new byte[len]; while ((bytes = in.read(block, offset, len)) > -1) { downloadedFile.write(bytes); } downloadedFile.close(); in.close(); WmLog.getCoreLogger().info("File downloaded to '{}'" + file_path); } catch (Exception Ex) { WmLog.getCoreLogger().error("Download failed: {}", Ex); throw new Exception("Download failed!"); } finally { getRequest.releaseConnection(); } return file_path; }
From source file:fr.bettinger.log4j.HttpAppender.java
@Override protected void append(LoggingEvent paramLoggingEvent) { /*/* w ww . j a v a2 s . c o m*/ * Dans le cas ou le layout n'est pas de type HttpLayout, on ne fait rien */ if (!(this.getLayout() instanceof HttpLayout)) { errorHandler.error("you must use a HttpLayout type"); return; } HttpLayout layout = (HttpLayout) getLayout(); HttpMethodBase httpMethod = null; HttpClient httpClient = new HttpClient(); /* * On positionne le timeout */ httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(timeOut); httpClient.getParams().setSoTimeout(timeOut); if (this.HttpMethodBase.equalsIgnoreCase(METHOD_GET)) { String query = layout.format(paramLoggingEvent); String url = this.logURL + query; LogLog.debug(url); httpMethod = new GetMethod(url); } else { if (this.postMethod.equalsIgnoreCase(POST_PARAMETERS)) { httpMethod = new PostMethod(this.logURL); // post requests don't need to be encoded manually layout.urlEncode = false; for (int i = 0; i < layout.subLayouts.size(); i += 2) { URLParameterNameLayout nameLayout = (URLParameterNameLayout) layout.subLayouts.get(i); String value = layout.subLayouts.get(i + 1).format(paramLoggingEvent); ((PostMethod) httpMethod).addParameter(nameLayout.getValue(), value); } } else { String message = layout.format(paramLoggingEvent); StringBuffer sb = new StringBuffer(this.logURL); sb.append(message); httpMethod = new PostMethod(sb.toString()); } } HttpThread httpThread = new HttpThread(httpClient, errorHandler); httpThread.setMethod(httpMethod); if (thread) { executorService.submit(httpThread); } else { httpThread.run(); } }