Java Code Examples for org.apache.commons.httpclient.methods.GetMethod#setRequestHeader()

The following examples show how to use org.apache.commons.httpclient.methods.GetMethod#setRequestHeader() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DavExchangeSession.java    From davmail with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected String getFreeBusyData(String attendee, String start, String end, int interval) throws IOException {
    String freebusyUrl = publicFolderUrl + "/?cmd=freebusy" +
            "&start=" + start +
            "&end=" + end +
            "&interval=" + interval +
            "&u=SMTP:" + attendee;
    GetMethod getMethod = new GetMethod(freebusyUrl);
    getMethod.setRequestHeader("Content-Type", "text/xml");
    String fbdata;
    try {
        DavGatewayHttpClientFacade.executeGetMethod(httpClient, getMethod, true);
        fbdata = StringUtil.getLastToken(getMethod.getResponseBodyAsString(), "<a:fbdata>", "</a:fbdata>");
    } finally {
        getMethod.releaseConnection();
    }
    return fbdata;
}
 
Example 2
Source File: HTTPMetadataProvider.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Builds the HTTP GET method used to fetch the metadata. The returned method advertises support for GZIP and
 * deflate compression, enables conditional GETs if the cached metadata came with either an ETag or Last-Modified
 * information, and sets up basic authentication if such is configured.
 * 
 * @return the constructed GET method
 */
protected GetMethod buildGetMethod() {
    GetMethod getMethod = new GetMethod(getMetadataURI());
    getMethod.addRequestHeader("Connection", "close");

    getMethod.setRequestHeader("Accept-Encoding", "gzip,deflate");
    if (cachedMetadataETag != null) {
        getMethod.setRequestHeader("If-None-Match", cachedMetadataETag);
    }
    if (cachedMetadataLastModified != null) {
        getMethod.setRequestHeader("If-Modified-Since", cachedMetadataLastModified);
    }

    if (httpClient.getState().getCredentials(authScope) != null) {
        log.debug("Using BASIC authentication when retrieving metadata from '{}", metadataURI);
        getMethod.setDoAuthentication(true);
    }

    return getMethod;
}
 
Example 3
Source File: AbstractTestRestApi.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
protected static GetMethod httpGet(String path, String user, String pwd, String cookies)
        throws IOException {
  LOG.info("Connecting to {}", URL + path);
  HttpClient httpClient = new HttpClient();
  GetMethod getMethod = new GetMethod(URL + path);
  getMethod.addRequestHeader("Origin", URL);
  if (userAndPasswordAreNotBlank(user, pwd)) {
    getMethod.setRequestHeader("Cookie", "JSESSIONID=" + getCookie(user, pwd));
  }
  if (!StringUtils.isBlank(cookies)) {
    getMethod.setRequestHeader("Cookie", getMethod.getResponseHeader("Cookie") + ";" + cookies);
  }
  httpClient.executeMethod(getMethod);
  LOG.info("{} - {}", getMethod.getStatusCode(), getMethod.getStatusText());
  return getMethod;
}
 
Example 4
Source File: ZeppelinServerMock.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
protected static GetMethod httpGet(String path, String user, String pwd, String cookies)
    throws IOException {
  LOG.info("Connecting to {}", URL + path);
  HttpClient httpClient = new HttpClient();
  GetMethod getMethod = new GetMethod(URL + path);
  getMethod.addRequestHeader("Origin", URL);
  if (userAndPasswordAreNotBlank(user, pwd)) {
    getMethod.setRequestHeader("Cookie", "JSESSIONID=" + getCookie(user, pwd));
  }
  if (!StringUtils.isBlank(cookies)) {
    getMethod.setRequestHeader("Cookie", getMethod.getResponseHeader("Cookie") + ";" + cookies);
  }
  httpClient.executeMethod(getMethod);
  LOG.info("{} - {}", getMethod.getStatusCode(), getMethod.getStatusText());
  return getMethod;
}
 
Example 5
Source File: BaseJavascriptTest.java    From seldon-server with Apache License 2.0 6 votes vote down vote up
private <T> T retrievePayload(String path, NameValuePair[] queryParameters, Class<T> valueType) throws IOException {
    GetMethod getMethod = new GetMethod(testState.getEndpoint() + path);
    getMethod.setQueryString(queryParameters);
    String httpReferer = testState.getHttpReferer();
    if ( httpReferer != null ) {
        getMethod.setRequestHeader("Referer", httpReferer);
    }
    client.executeMethod(getMethod);
    String response = getMethod.getResponseBodyAsString();

    // Remove jsonp prefix and suffix:
    String payload = stripJsonp(response);
    logger.info("Payload: " + payload);

    return objectMapper.readValue(payload, valueType);
}
 
Example 6
Source File: DavExchangeSession.java    From davmail with GNU General Public License v2.0 5 votes vote down vote up
@Override
public ExchangeSession.ContactPhoto getContactPhoto(ExchangeSession.Contact contact) throws IOException {
    ContactPhoto contactPhoto;
    final GetMethod method = new GetMethod(URIUtil.encodePath(contact.getHref()) + "/ContactPicture.jpg");
    method.setRequestHeader("Translate", "f");
    method.setRequestHeader("Accept-Encoding", "gzip");

    InputStream inputStream = null;
    try {
        DavGatewayHttpClientFacade.executeGetMethod(httpClient, method, true);
        if (DavGatewayHttpClientFacade.isGzipEncoded(method)) {
            inputStream = (new GZIPInputStream(method.getResponseBodyAsStream()));
        } else {
            inputStream = method.getResponseBodyAsStream();
        }

        contactPhoto = new ContactPhoto();
        contactPhoto.contentType = "image/jpeg";

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        InputStream partInputStream = inputStream;
        IOUtil.write(partInputStream, baos);
        contactPhoto.content = IOUtil.encodeBase64AsString(baos.toByteArray());
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                LOGGER.debug(e);
            }
        }
        method.releaseConnection();
    }
    return contactPhoto;
}
 
Example 7
Source File: Hc3HttpUtils.java    From wpcleaner with Apache License 2.0 5 votes vote down vote up
/**
 * Create an HTTP GET Method.
 * 
 * @param url URL of the request.
 * @param properties Properties to drive the API.
 * @return GET Method
 * @throws URIException Exception if the URL is not correct.
 */
private static GetMethod createHttpGetMethod(
    String url,
    Map<String, String> properties) throws URIException {

  // Initialize GET Method
  org.apache.commons.httpclient.URI uri = new org.apache.commons.httpclient.URI(url, false, "UTF8");
  GetMethod method = new GetMethod();
  method.setURI(uri);
  method.getParams().setSoTimeout(60000);
  method.getParams().setContentCharset("UTF-8");
  method.setRequestHeader("Accept-Encoding", "gzip");

  // Manager query string
  StringBuilder debugUrl = (DEBUG_URL) ? new StringBuilder("GET  " + url) : null;
  List<NameValuePair> params = new ArrayList<NameValuePair>();
  if (properties != null) {
    boolean first = true;
    Iterator<Map.Entry<String, String>> iter = properties.entrySet().iterator();
    while (iter.hasNext()) {
      Map.Entry<String, String> property = iter.next();
      String key = property.getKey();
      String value = property.getValue();
      params.add(new NameValuePair(key, value));
      first = fillDebugUrl(debugUrl, first, key, value);
    }
  }
  if (DEBUG_URL && (debugUrl != null)) {
    debugText(debugUrl.toString());
  }
  NameValuePair[] tmpParams = new NameValuePair[params.size()];
  method.setQueryString(params.toArray(tmpParams));

  return method;
}
 
Example 8
Source File: PurchaseService.java    From computoser with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    HttpClient client = new HttpClient();
    String url = "https://coinbase.com/api/v1/currencies/exchange_rates?api_key=226d24b176455557d2928e54321b5ec35fd6e054d9d682ad0927f4e7ea6ed7b1";
    GetMethod get = new GetMethod(url);
    get.setRequestHeader("Content-Type", "application/json");
    get.setRequestHeader("Accept", "application/json");
    get.setRequestHeader("User-Agent", "");
    client.executeMethod(get);
    System.out.println(new String(get.getResponseBody()));
}
 
Example 9
Source File: NetClient.java    From qingyang with Apache License 2.0 5 votes vote down vote up
private static GetMethod getHttpGet(String url, String userAgent) {
	GetMethod httpGet = new GetMethod(url);
	// 设置请求超时时间
	httpGet.getParams().setSoTimeout(TIMEOUT_CONNECTION);
	httpGet.setRequestHeader("Host", URL.HOST);
	httpGet.setRequestHeader("Connection", "Keep_Alive");
	httpGet.setRequestHeader("User_Agent", userAgent);
	return httpGet;
}
 
Example 10
Source File: QueueInformation.java    From sequenceiq-samples with Apache License 2.0 5 votes vote down vote up
public void printQueueInfo(HttpClient client, ObjectMapper mapper, String schedulerResourceURL) {
	
	//http://sandbox.hortonworks.com:8088/ws/v1/cluster/scheduler in case of HDP
	GetMethod get = new GetMethod(schedulerResourceURL);
    get.setRequestHeader("Accept", "application/json");
    try {
        int statusCode = client.executeMethod(get);

        if (statusCode != HttpStatus.SC_OK) {
        	LOGGER.error("Method failed: " + get.getStatusLine());
        }
        
		InputStream in = get.getResponseBodyAsStream();
		
		JsonNode jsonNode = mapper.readValue(in, JsonNode.class);
		ArrayNode queues = (ArrayNode) jsonNode.path("scheduler").path("schedulerInfo").path("queues").get("queue");
		for (int i = 0; i < queues.size(); i++) {
			JsonNode queueNode = queues.get(i);						
			LOGGER.info("queueName / usedCapacity / absoluteUsedCap / absoluteCapacity / absMaxCapacity: " + 
					queueNode.findValue("queueName") + " / " +
					queueNode.findValue("usedCapacity") + " / " + 
					queueNode.findValue("absoluteUsedCapacity") + " / " + 
					queueNode.findValue("absoluteCapacity") + " / " +
					queueNode.findValue("absoluteMaxCapacity"));
		}
	} catch (IOException e) {
		LOGGER.error("Exception occured", e);
	} finally {	        
		get.releaseConnection();
	}	      
        
}
 
Example 11
Source File: HttpDirectTemplateDownloader.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
protected GetMethod createRequest(String downloadUrl, Map<String, String> headers) {
    GetMethod request = new GetMethod(downloadUrl);
    request.setFollowRedirects(true);
    if (MapUtils.isNotEmpty(headers)) {
        for (String key : headers.keySet()) {
            request.setRequestHeader(key, headers.get(key));
            reqHeaders.put(key, headers.get(key));
        }
    }
    return request;
}
 
Example 12
Source File: GenBaseCase.java    From zap-extensions with Apache License 2.0 4 votes vote down vote up
public static BaseCase genURLFuzzBaseCase(Manager manager, String fuzzStart, String FuzzEnd)
        throws MalformedURLException, IOException {
    BaseCase baseCase = null;
    int failcode = 0;
    String failString = Config.failCaseString;
    String baseResponce = "";

    /*
     * markers for using regex instead
     */
    boolean useRegexInstead = false;
    String regex = null;

    URL failurl = new URL(fuzzStart + failString + FuzzEnd);

    GetMethod httpget = new GetMethod(failurl.toString());
    // set the custom HTTP headers
    Vector HTTPheaders = manager.getHTTPHeaders();
    for (int a = 0; a < HTTPheaders.size(); a++) {
        HTTPHeader httpHeader = (HTTPHeader) HTTPheaders.elementAt(a);
        /*
         * Host header has to be set in a different way!
         */
        if (httpHeader.getHeader().startsWith("Host")) {
            httpget.getParams().setVirtualHost(httpHeader.getValue());
        } else {
            httpget.setRequestHeader(httpHeader.getHeader(), httpHeader.getValue());
        }
    }
    httpget.setFollowRedirects(Config.followRedirects);

    // save the http responce code for the base case
    failcode = manager.getHttpclient().executeMethod(httpget);
    manager.workDone();

    if (failcode == 200) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Base case for " + failurl.toString() + " came back as 200!");
        }

        BufferedReader input =
                new BufferedReader(new InputStreamReader(httpget.getResponseBodyAsStream()));
        String tempLine;
        StringBuffer buf = new StringBuffer();
        while ((tempLine = input.readLine()) != null) {
            buf.append("\r\n" + tempLine);
        }
        baseResponce = buf.toString();
        input.close();

        // clean up the base case, based on the basecase URL
        baseResponce = FilterResponce.CleanResponce(baseResponce, failurl, failString);

        if (LOG.isDebugEnabled()) {
            LOG.debug("Base case was set to: " + baseResponce);
        }
    }

    httpget.releaseConnection();

    /*
     * create the base case object
     */
    baseCase =
            new BaseCase(
                    null, failcode, false, failurl, baseResponce, null, useRegexInstead, regex);

    return baseCase;
}
 
Example 13
Source File: GenBaseCase.java    From zap-extensions with Apache License 2.0 4 votes vote down vote up
private static String getBaseCaseAgain(Manager manager, URL failurl, String failString)
        throws IOException {
    int failcode;
    String baseResponce = "";

    GetMethod httpget = new GetMethod(failurl.toString());
    // set the custom HTTP headers
    Vector HTTPheaders = manager.getHTTPHeaders();
    for (int a = 0; a < HTTPheaders.size(); a++) {
        HTTPHeader httpHeader = (HTTPHeader) HTTPheaders.elementAt(a);
        /*
         * Host header has to be set in a different way!
         */
        if (httpHeader.getHeader().startsWith("Host")) {
            httpget.getParams().setVirtualHost(httpHeader.getValue());
        } else {
            httpget.setRequestHeader(httpHeader.getHeader(), httpHeader.getValue());
        }
    }
    httpget.setFollowRedirects(Config.followRedirects);

    // save the http responce code for the base case
    failcode = manager.getHttpclient().executeMethod(httpget);
    manager.workDone();

    // we now need to get the content as we need a base case!
    if (failcode == 200) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Base case for " + failurl.toString() + " came back as 200!");
        }

        BufferedReader input =
                new BufferedReader(new InputStreamReader(httpget.getResponseBodyAsStream()));
        String tempLine;
        StringBuffer buf = new StringBuffer();
        while ((tempLine = input.readLine()) != null) {
            buf.append("\r\n" + tempLine);
        }
        baseResponce = buf.toString();
        input.close();

        // HTMLparse.parseHTML();

        // HTMLparse htmlParse = new HTMLparse(baseResponce, null);
        // Thread parse  = new Thread(htmlParse);
        // parse.start();

        // clean up the base case, based on the basecase URL
        baseResponce = FilterResponce.CleanResponce(baseResponce, failurl, failString);

        httpget.releaseConnection();

        /*
         * return the cleaned responce
         */
        return baseResponce;
    } else {
        /*
         * we have a big problem here as the server has returned an other responce code, for the same request
         * TODO: think of a way to deal with this!
         */
        return null;
    }
}
 
Example 14
Source File: RestApiLoginFilterITCase.java    From olat with Apache License 2.0 4 votes vote down vote up
/**
 * Test if the token survive several requests
 * 
 * @throws HttpException
 * @throws IOException
 */
@Test
public void testFollowTokenBasedDiscussion() throws HttpException, IOException {
    String securityToken = getAuthenticatedTokenBasedClient("administrator", "olat");
    assertTrue(StringHelper.containsNonWhitespace(securityToken));

    // path is protected
    final GetMethod method1 = createGet("/users/version", MediaType.TEXT_PLAIN, false);
    method1.setRequestHeader(RestSecurityHelper.SEC_TOKEN, securityToken);
    final int code1 = getHttpClient().executeMethod(method1);
    method1.releaseConnection();
    securityToken = getToken(method1);
    assertEquals(code1, 200);
    assertTrue(StringHelper.containsNonWhitespace(securityToken));

    // path is protected
    final GetMethod method2 = createGet("/repo/entries", MediaType.TEXT_HTML, false);
    method2.setRequestHeader(RestSecurityHelper.SEC_TOKEN, securityToken);
    final int code2 = getHttpClient().executeMethod(method2);
    method2.releaseConnection();
    securityToken = getToken(method2);
    assertEquals(code2, 200);
    assertTrue(StringHelper.containsNonWhitespace(securityToken));

    // path is not protected
    final GetMethod method3 = createGet("/api/copyright", MediaType.TEXT_PLAIN, false);
    method3.setRequestHeader(RestSecurityHelper.SEC_TOKEN, securityToken);
    final int code3 = getHttpClient().executeMethod(method3);
    method3.releaseConnection();
    securityToken = getToken(method3);
    assertEquals(code3, 200);
    assertTrue(StringHelper.containsNonWhitespace(securityToken));

    // path is protected
    final GetMethod method4 = createGet("/repo/entries", MediaType.TEXT_HTML, false);
    method4.setRequestHeader(RestSecurityHelper.SEC_TOKEN, securityToken);
    final int code4 = getHttpClient().executeMethod(method4);
    method4.releaseConnection();
    securityToken = getToken(method4);
    assertEquals(code4, 200);
    assertTrue(StringHelper.containsNonWhitespace(securityToken));
}
 
Example 15
Source File: RestApiLoginFilterITCase.java    From olat with Apache License 2.0 4 votes vote down vote up
/**
 * Test if the token survive several requests
 * 
 * @throws HttpException
 * @throws IOException
 */
@Test
public void testFollowTokenBasedDiscussion() throws HttpException, IOException {
    String securityToken = getAuthenticatedTokenBasedClient("administrator", "olat");
    assertTrue(StringHelper.containsNonWhitespace(securityToken));

    // path is protected
    final GetMethod method1 = createGet("/users/version", MediaType.TEXT_PLAIN, false);
    method1.setRequestHeader(RestSecurityHelper.SEC_TOKEN, securityToken);
    final int code1 = getHttpClient().executeMethod(method1);
    method1.releaseConnection();
    securityToken = getToken(method1);
    assertEquals(code1, 200);
    assertTrue(StringHelper.containsNonWhitespace(securityToken));

    // path is protected
    final GetMethod method2 = createGet("/repo/entries", MediaType.TEXT_HTML, false);
    method2.setRequestHeader(RestSecurityHelper.SEC_TOKEN, securityToken);
    final int code2 = getHttpClient().executeMethod(method2);
    method2.releaseConnection();
    securityToken = getToken(method2);
    assertEquals(code2, 200);
    assertTrue(StringHelper.containsNonWhitespace(securityToken));

    // path is not protected
    final GetMethod method3 = createGet("/api/copyright", MediaType.TEXT_PLAIN, false);
    method3.setRequestHeader(RestSecurityHelper.SEC_TOKEN, securityToken);
    final int code3 = getHttpClient().executeMethod(method3);
    method3.releaseConnection();
    securityToken = getToken(method3);
    assertEquals(code3, 200);
    assertTrue(StringHelper.containsNonWhitespace(securityToken));

    // path is protected
    final GetMethod method4 = createGet("/repo/entries", MediaType.TEXT_HTML, false);
    method4.setRequestHeader(RestSecurityHelper.SEC_TOKEN, securityToken);
    final int code4 = getHttpClient().executeMethod(method4);
    method4.releaseConnection();
    securityToken = getToken(method4);
    assertEquals(code4, 200);
    assertTrue(StringHelper.containsNonWhitespace(securityToken));
}
 
Example 16
Source File: ChangeLogListener.java    From RestServices with Apache License 2.0 4 votes vote down vote up
void startConnection() throws IOException,
		HttpException {
	String requestUrl = getChangesRequestUrl(true);
	
	GetMethod get = this.currentRequest = new GetMethod(requestUrl);
	get.setRequestHeader(RestServices.HEADER_ACCEPT, RestServices.CONTENTTYPE_APPLICATIONJSON);
	
	RestConsumer.includeHeaders(get, headers);
	int status = RestConsumer.client.executeMethod(get);
	try {
		if (status != IMxRuntimeResponse.OK)
			throw new RuntimeException("Failed to setup stream to " + url +  ", status: " + status);

		InputStream inputStream = get.getResponseBodyAsStream();
	
		JSONTokener jt = new JSONTokener(inputStream);
		JSONObject instr = null;
		
		try {
			isConnected  = true;
			while(true) {
				instr = new JSONObject(jt);
				
				processChange(instr);
			}
		}
		catch(InterruptedException e2) {
			cancelled = true;
			RestServices.LOGCONSUME.warn("Changefeed interrupted", e2);
		}
		catch(Exception e) {
			//Not graceful disconnected?
			if (!cancelled && !(jt.end() && e instanceof JSONException))
				throw new RuntimeException(e);
		}
	}
	finally {
		isConnected = false;
		get.releaseConnection();
	}
}