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

The following examples show how to use org.apache.commons.httpclient.methods.GetMethod#setFollowRedirects() . 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: NetworkUtil.java    From openemm with GNU Affero General Public License v3.0 6 votes vote down vote up
public static byte[] loadUrlData(String url) throws Exception {
	HttpClient httpClient = new HttpClient();
	setHttpClientProxyFromSystem(httpClient, url);
	GetMethod get = new GetMethod(url);		
	get.setFollowRedirects(true);
	
	try {			
		httpClient.getParams().setParameter("http.connection.timeout", 5000);
		int httpReturnCode = httpClient.executeMethod(get); 

		if (httpReturnCode == 200) {
			// Don't use get.getResponseBody, it causes a warning in log
			try (InputStream inputStream = get.getResponseBodyAsStream()) {
				return IOUtils.toByteArray(inputStream);
			}
		} else {
			throw new Exception("ERROR: Received httpreturncode " + httpReturnCode);
		}
	} finally {
		get.releaseConnection();
	}
}
 
Example 2
Source File: VmRuntimeJettyAuthTest.java    From appengine-java-vm-runtime with Apache License 2.0 6 votes vote down vote up
public void testAuth_UserRequiredNoUser() throws Exception {
  String loginUrl = "http://login-url?url=http://test-app.googleapp.com/user/test-auth";
  CreateLoginURLResponse loginUrlResponse = new CreateLoginURLResponse();
  loginUrlResponse.setLoginUrl(loginUrl);
  // Fake the expected call to "user/CreateLoginUrl".
  FakeableVmApiProxyDelegate fakeApiProxy = new FakeableVmApiProxyDelegate();
  ApiProxy.setDelegate(fakeApiProxy);
  fakeApiProxy.addApiResponse(loginUrlResponse);

  HttpClient httpClient = new HttpClient();
  httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
  GetMethod get = new GetMethod(createUrl("/user/test-auth").toString());
  get.setFollowRedirects(false);
  int httpCode = httpClient.executeMethod(get);
  assertEquals(302, httpCode);
  Header redirUrl = get.getResponseHeader("Location");
  assertEquals(loginUrl, redirUrl.getValue());
}
 
Example 3
Source File: ServerStatusChecker.java    From development with Apache License 2.0 6 votes vote down vote up
/**
 * Checks if the offline servers has come back online
 * again.
 */
private synchronized void checkOfflineServers() {
    Iterator itr = offline.listIterator();
    while (itr.hasNext()) {
        Server server = (Server) itr.next();
        String url = getServerURL(server);
        GetMethod get = new GetMethod(url);
        get.setFollowRedirects(false);
        
        try {
            httpClient.executeMethod(get);
            if (okServerResponse(get.getStatusCode())) {
                online.add(server);
                itr.remove();
                log.debug("Server back online " + getServerURL(server));
                listener.serverOnline(server); 
            }
        } catch (Exception e) {
            listener.serverOffline(server);
        } finally {
            get.releaseConnection();
        }
    }
}
 
Example 4
Source File: VmRuntimeJettyAuthTest.java    From appengine-java-vm-runtime with Apache License 2.0 6 votes vote down vote up
public void testAuth_AdminRequiredNoUser() throws Exception {
  String loginUrl = "http://login-url?url=http://test-app.googleapp.com/user/test-auth";
  CreateLoginURLResponse loginUrlResponse = new CreateLoginURLResponse();
  loginUrlResponse.setLoginUrl(loginUrl);
  // Fake the expected call to "user/CreateLoginUrl".
  FakeableVmApiProxyDelegate fakeApiProxy = new FakeableVmApiProxyDelegate();
  ApiProxy.setDelegate(fakeApiProxy);
  fakeApiProxy.addApiResponse(loginUrlResponse);

  HttpClient httpClient = new HttpClient();
  httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
  GetMethod get = new GetMethod(createUrl("/admin/test-auth").toString());
  get.setFollowRedirects(false);
  int httpCode = httpClient.executeMethod(get);
  assertEquals(302, httpCode);
  Header redirUrl = get.getResponseHeader("Location");
  assertEquals(loginUrl, redirUrl.getValue());
}
 
Example 5
Source File: VmRuntimeJettyAuthTest.java    From appengine-java-vm-runtime with Apache License 2.0 5 votes vote down vote up
public void testAuth_AdminRequiredNoUser_SkipAdminCheck() throws Exception {
  HttpClient httpClient = new HttpClient();
  httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
  GetMethod get = new GetMethod(createUrl("/admin/test-auth").toString());
  get.addRequestHeader("X-Google-Internal-SkipAdminCheck", "1");
  get.setFollowRedirects(false);
  int httpCode = httpClient.executeMethod(get);
  assertEquals(200, httpCode);
  assertEquals("null: null", get.getResponseBodyAsString());
}
 
Example 6
Source File: ServerStatusChecker.java    From development with Apache License 2.0 5 votes vote down vote up
/**
 * Checks all the servers marked as being online
 * if they still are online.
 */
private synchronized void checkOnlineServers() {
    Iterator itr;
    itr = online.listIterator();
    while (itr.hasNext()) {
        Server server = (Server) itr.next();
        String url = getServerURL(server);
        GetMethod get = new GetMethod(url);
        get.setFollowRedirects(false);
        
        try {
            httpClient.executeMethod(get);
            if (!okServerResponse(get.getStatusCode())) {
                offline.add(server);
                itr.remove();
                log.debug("Server going OFFLINE! " + getServerURL(server));
                listener.serverOffline(server);
            }
        } catch (Exception e) { 
            offline.add(server);
            itr.remove();
            log.debug("Server going OFFLINE! " + getServerURL(server));
            listener.serverOffline(server);
        } finally {
            get.releaseConnection();
        }
    }
}
 
Example 7
Source File: HttpClientUtil.java    From spider with GNU General Public License v3.0 5 votes vote down vote up
public String getHeader(String url, String cookies, String headername) throws IOException {
//        clearCookies();
        GetMethod g = new GetMethod(url);
        g.setFollowRedirects(false);
        if (StringUtils.isNotEmpty(cookies)) {
            g.addRequestHeader("cookie", cookies);
        }
        hc.executeMethod(g);
        return g.getResponseHeader(headername) == null ? null : g.getResponseHeader(headername).getValue();
    }
 
Example 8
Source File: VmRuntimeJettyAuthTest.java    From appengine-java-vm-runtime with Apache License 2.0 5 votes vote down vote up
public void testAuth_TrustedRealIP() throws Exception {
  HttpClient httpClient = new HttpClient();
  httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
  GetMethod get = new GetMethod(createUrlForHostIP("/admin/test-auth").toString());
  get.addRequestHeader(VmApiProxyEnvironment.REAL_IP_HEADER, "127.0.0.1");
  get.addRequestHeader(VmApiProxyEnvironment.EMAIL_HEADER, "[email protected]");
  get.addRequestHeader(VmApiProxyEnvironment.AUTH_DOMAIN_HEADER, "google.com");
  get.addRequestHeader(VmApiProxyEnvironment.IS_ADMIN_HEADER, "1");
  get.setFollowRedirects(false);
  int httpCode = httpClient.executeMethod(get);
  assertEquals(200, httpCode);
  assertEquals("[email protected]: [email protected]", get.getResponseBodyAsString());
}
 
Example 9
Source File: HttpClientUtil.java    From spider with GNU General Public License v3.0 5 votes vote down vote up
public String get(String url, String cookies, boolean followRedirects) throws
            IOException {
//        clearCookies();
        GetMethod g = new GetMethod(url);
        g.setFollowRedirects(followRedirects);
        if (StringUtils.isNotEmpty(cookies)) {
            g.addRequestHeader("cookie", cookies);
        }
        hc.executeMethod(g);
        return g.getResponseBodyAsString();
    }
 
Example 10
Source File: VmRuntimeJettyAuthTest.java    From appengine-java-vm-runtime with Apache License 2.0 5 votes vote down vote up
public void testAuth_UntrustedInboundIpWithQuery() throws Exception {
  HttpClient httpClient = new HttpClient();
  httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
  GetMethod get = new GetMethod(createUrlForHostIP("/admin/test-auth?foo=bar").toString());
  get.addRequestHeader(VmApiProxyEnvironment.REAL_IP_HEADER, "127.0.0.2"); // Force untrusted dev IP
  get.setFollowRedirects(false);
  int httpCode = httpClient.executeMethod(get);
  assertEquals(307, httpCode);
  assertEquals("https://testversion-dot-testbackend-dot-testhostname/admin/test-auth?foo=bar",
          get.getResponseHeader("Location").getValue());
}
 
Example 11
Source File: HttpClientUtil.java    From spider with GNU General Public License v3.0 5 votes vote down vote up
public String get(String url, String cookies) throws
            IOException {
//        clearCookies();
        GetMethod g = new GetMethod(url);
        g.setFollowRedirects(false);
        if (StringUtils.isNotEmpty(cookies)) {
            g.addRequestHeader("cookie", cookies);
        }
        hc.executeMethod(g);
        return g.getResponseBodyAsString();
    }
 
Example 12
Source File: HttpClientUtil.java    From Gather-Platform with GNU General Public License v3.0 5 votes vote down vote up
public String get(String url, boolean followRedirects) throws
            IOException {
//        clearCookies();
        GetMethod g = new GetMethod(url);
        g.setFollowRedirects(followRedirects);
        hc.executeMethod(g);
        return g.getResponseBodyAsString();
    }
 
Example 13
Source File: VmRuntimeJettyAuthTest.java    From appengine-java-vm-runtime with Apache License 2.0 5 votes vote down vote up
public void testAuth_UntrustedRealIP() throws Exception {
  HttpClient httpClient = new HttpClient();
  httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
  GetMethod get = new GetMethod(createUrl("/admin/test-auth").toString());
  get.addRequestHeader(VmApiProxyEnvironment.REAL_IP_HEADER, "123.123.123.123");
  get.setFollowRedirects(false);
  int httpCode = httpClient.executeMethod(get);
  assertEquals(307, httpCode);
  assertEquals("https://testversion-dot-testbackend-dot-testhostname/admin/test-auth",
          get.getResponseHeader("Location").getValue());
}
 
Example 14
Source File: HttpClientUtil.java    From Gather-Platform with GNU General Public License v3.0 5 votes vote down vote up
public String get(String url, Cookie[] cookies) throws
            IOException {
//        clearCookies();
        GetMethod g = new GetMethod(url);
        g.setFollowRedirects(false);
        hc.getState().addCookies(cookies);
        hc.executeMethod(g);
        return g.getResponseBodyAsString();
    }
 
Example 15
Source File: VmRuntimeJettyAuthTest.java    From appengine-java-vm-runtime with Apache License 2.0 5 votes vote down vote up
public void testAuth_AdminRequiredWithNonAdmin() throws Exception {
  HttpClient httpClient = new HttpClient();
  httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
  GetMethod get = new GetMethod(createUrl("/admin/test-auth").toString());
  get.addRequestHeader(VmApiProxyEnvironment.EMAIL_HEADER, "[email protected]");
  get.addRequestHeader(VmApiProxyEnvironment.AUTH_DOMAIN_HEADER, "google.com");
  get.setFollowRedirects(false);
  int httpCode = httpClient.executeMethod(get);
  assertEquals(403, httpCode);
}
 
Example 16
Source File: MetalinkTemplateDownloader.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
protected GetMethod createRequest(String downloadUrl) {
    GetMethod request = new GetMethod(downloadUrl);
    request.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, myretryhandler);
    request.setFollowRedirects(true);
    if (!toFileSet) {
        String[] parts = downloadUrl.split("/");
        String filename = parts[parts.length - 1];
        _toFile = _toDir + File.separator + filename;
        toFileSet = true;
    }
    return request;
}
 
Example 17
Source File: DavGatewayHttpClientFacade.java    From davmail with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Execute test method from checkConfig, with proxy credentials, but without Exchange credentials.
 *
 * @param httpClient Http client instance
 * @param method     Http method
 * @return Http status
 * @throws IOException on error
 */
public static int executeTestMethod(HttpClient httpClient, GetMethod method) throws IOException {
    // do not follow redirects in expired sessions
    method.setFollowRedirects(false);
    int status = httpClient.executeMethod(method);
    if (status == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED
            && acceptsNTLMOnly(method) && !hasNTLMorNegotiate(httpClient)) {
        resetMethod(method);
        LOGGER.debug("Received " + status + " unauthorized at " + method.getURI() + ", retrying with NTLM");
        addNTLM(httpClient);
        status = httpClient.executeMethod(method);
    }

    return status;
}
 
Example 18
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 19
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 20
Source File: MailingComponentImpl.java    From openemm with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public boolean loadContentFromURL() {
	boolean returnValue = true;

	// return false;

	if ((type != TYPE_IMAGE) && (type != TYPE_ATTACHMENT)) {
		return false;
	}
	
	HttpClient httpClient = new HttpClient();
	String encodedURI = encodeURI(componentName);
	GetMethod get = new GetMethod(encodedURI);
	get.setFollowRedirects(true);
	
	try {
		NetworkUtil.setHttpClientProxyFromSystem(httpClient, encodedURI);
		
		httpClient.getParams().setParameter("http.connection.timeout", 5000);

		if (httpClient.executeMethod(get) == 200) {
			get.getResponseHeaders();
			
			// TODO: Due to data types of DB columns binblock and emmblock, replacing getResponseBody() cannot be replaced by safer getResponseBodyAsStream(). Better solutions?
			Header contentType = get.getResponseHeader("Content-Type");
			String contentTypeValue = "";
			if(contentType != null) {
				contentTypeValue = contentType.getValue();
			} else {
				logger.debug("No content-type in response from: " + encodedURI);
			}
			setBinaryBlock(get.getResponseBody(), contentTypeValue);
		}
	} catch (Exception e) {
		logger.error("loadContentFromURL: " + encodedURI, e);
		returnValue = false;
	} finally {
		get.releaseConnection();
	}
	
	if( logger.isInfoEnabled()) {
		logger.info("loadContentFromURL: loaded " + componentName);
	}
	
	return returnValue;
}