Example usage for org.apache.commons.httpclient.methods GetMethod GetMethod

List of usage examples for org.apache.commons.httpclient.methods GetMethod GetMethod

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.methods GetMethod GetMethod.

Prototype

public GetMethod(String uri) 

Source Link

Usage

From source file:com.liusoft.dlog4j.xml.RSSFetcher.java

/**
 * ??//from   w  w w.  j  ava 2s .c om
 * @param type
 * @param url
 * @return
 * @throws IOException 
 * @throws HttpException 
 * @throws SAXException 
 * @throws ParseException 
 */
private static Channel fetchChannelViaHTTP(String url) throws HttpException, IOException, SAXException {
    Digester parser = new XMLDigester();
    GetMethod get = new GetMethod(url);
    //get.setRequestHeader("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; zh-cn) Opera 8.52");
    try {
        http_client.executeMethod(get);
        if (get.getStatusCode() == HttpServletResponse.SC_OK) {
            Charset cs = null;
            Header header_cs = getResponseHeader(get, "Content-Type");
            if (header_cs == null) {
                cs = Charset.forName(get.getResponseCharSet());
            } else {
                String content_type = header_cs.getValue().toLowerCase();
                try {
                    Object[] values = content_type_parser.parse(content_type);
                    cs = Charset.forName((String) values[1]);
                } catch (ParseException e) {
                    URL o_url = new URL(url);
                    String host = o_url.getHost();
                    Iterator hosts = charsets.keySet().iterator();
                    while (hosts.hasNext()) {
                        String t_host = (String) hosts.next();
                        if (host.toLowerCase().endsWith(t_host)) {
                            cs = Charset.forName((String) charsets.get(t_host));
                            break;
                        }
                    }
                    if (cs == null)
                        cs = default_charset;

                }
            }

            BufferedReader rd = new BufferedReader(new InputStreamReader(get.getResponseBodyAsStream(), cs));

            char[] cbuf = new char[1];
            int read_idx = 1;
            do {
                rd.mark(read_idx++);
                if (rd.read(cbuf) == -1)
                    break;
                if (cbuf[0] != '<')
                    continue;
                rd.reset();
                break;
            } while (true);
            return (Channel) parser.parse(rd);
        } else {
            log.error("Fetch RSS from " + url + " failed, code=" + get.getStatusCode());
        }
    } finally {
        get.releaseConnection();
    }
    return null;
}

From source file:com.wandisco.s3hdfs.rewrite.filter.TestMetadata.java

@Test
@SuppressWarnings("deprecation")
public void testBasicMetadataRead()
        throws IOException, URISyntaxException, ServiceException, NoSuchAlgorithmException {
    NameNode nn = cluster.getNameNode();
    System.out.println(nn.getHttpAddress().toString());

    S3HdfsPath s3HdfsPath = testUtil.setUpS3HdfsPath("myBucket", "bigFile");

    // Put new object
    byte[] data = new byte[SMALL_SIZE];
    for (int i = 0; i < SMALL_SIZE; i++) {
        data[i] = (byte) (i % 256);
    }/*from   ww w.jav  a 2 s .  c  om*/
    S3Object object = new S3Object(s3HdfsPath.getObjectName(), data);
    Map<String, Object> metaEntries = new HashMap<String, Object>();
    metaEntries.put("scared", "yes");
    metaEntries.put("tired", "yes");
    metaEntries.put("hopeless", "never");
    object.addAllMetadata(metaEntries);
    object.setMetadataComplete(true);
    s3Service.putObject(s3HdfsPath.getBucketName(), object);

    HttpClient httpClient = new HttpClient();

    // Set up HttpGet and get response
    FileStatus fs = hdfs.getFileStatus(new Path(s3HdfsPath.getFullHdfsMetaPath()));
    assertTrue(fs.isFile());
    assertTrue(fs.getPath().getName().equals(META_FILE_NAME));
    String url = "http://" + hostName + ":" + PROXY_PORT + "/webhdfs/v1/s3hdfs/" + s3HdfsPath.getUserName()
            + "/myBucket/bigFile/" + DEFAULT_VERSION + "/" + META_FILE_NAME + "?op=OPEN";
    GetMethod httpGet = new GetMethod(url);
    httpClient.executeMethod(httpGet);
    InputStream is = httpGet.getResponseBodyAsStream();
    Properties retVal = testUtil.parseMap(is);
    System.out.println(retVal);

    // consume response and re-allocate connection
    httpGet.releaseConnection();
    assert httpGet.getStatusCode() == 200;
    assert retVal.getProperty("x-amz-meta-scared").equals("yes");
    assert retVal.getProperty("x-amz-meta-tired").equals("yes");
    assert retVal.getProperty("x-amz-meta-hopeless").equals("never");
}

From source file:com.zimbra.cs.account.auth.HostedAuth.java

/**
 * zmprov md test.com zimbraAuthMech 'custom:hosted http://auth.customer.com:80'
 * /*from  w  w  w  .  j a v  a  2  s.c o  m*/
 *  This custom auth module takes arguments in the following form:
 * {URL} [GET|POST - default is GET] [encryption method - defautl is plain] [auth protocol - default is imap] 
 * e.g.: http://auth.customer.com:80 GET
 **/
public void authenticate(Account acct, String password, Map<String, Object> context, List<String> args)
        throws Exception {
    HttpClient client = ZimbraHttpConnectionManager.getExternalHttpConnMgr().newHttpClient();
    HttpMethod method = null;

    String targetURL = args.get(0);
    /*
    if (args.size()>2) {
       authMethod = args.get(2);
    }
            
    if (args.size()>3) {
       authMethod = args.get(3);
    }*/

    if (args.size() > 1) {
        if (args.get(1).equalsIgnoreCase("GET"))
            method = new GetMethod(targetURL);
        else
            method = new PostMethod(targetURL);
    } else
        method = new GetMethod(targetURL);

    if (context.get(AuthContext.AC_ORIGINATING_CLIENT_IP) != null)
        method.addRequestHeader(HEADER_CLIENT_IP, context.get(AuthContext.AC_ORIGINATING_CLIENT_IP).toString());

    if (context.get(AuthContext.AC_REMOTE_IP) != null)
        method.addRequestHeader(HEADER_X_ZIMBRA_REMOTE_ADDR, context.get(AuthContext.AC_REMOTE_IP).toString());

    method.addRequestHeader(HEADER_AUTH_USER, acct.getName());
    method.addRequestHeader(HEADER_AUTH_PASSWORD, password);

    AuthContext.Protocol proto = (AuthContext.Protocol) context.get(AuthContext.AC_PROTOCOL);
    if (proto != null)
        method.addRequestHeader(HEADER_AUTH_PROTOCOL, proto.toString());

    if (context.get(AuthContext.AC_USER_AGENT) != null)
        method.addRequestHeader(HEADER_AUTH_USER_AGENT, context.get(AuthContext.AC_USER_AGENT).toString());

    try {
        HttpClientUtil.executeMethod(client, method);
    } catch (HttpException ex) {
        throw AuthFailedServiceException.AUTH_FAILED(acct.getName(), acct.getName(),
                "HTTP request to remote authentication server failed", ex);
    } catch (IOException ex) {
        throw AuthFailedServiceException.AUTH_FAILED(acct.getName(), acct.getName(),
                "HTTP request to remote authentication server failed", ex);
    } finally {
        if (method != null)
            method.releaseConnection();
    }

    int status = method.getStatusCode();
    if (status != HttpStatus.SC_OK) {
        throw AuthFailedServiceException.AUTH_FAILED(acct.getName(),
                "HTTP request to remote authentication server failed. Remote response code: "
                        + Integer.toString(status));
    }

    String responseMessage;
    if (method.getResponseHeader(HEADER_AUTH_STATUS) != null) {
        responseMessage = method.getResponseHeader(HEADER_AUTH_STATUS).getValue();
    } else {
        throw AuthFailedServiceException.AUTH_FAILED(acct.getName(),
                "Empty response from remote authentication server.");
    }
    if (responseMessage.equalsIgnoreCase(AUTH_STATUS_OK)) {
        return;
    } else {
        throw AuthFailedServiceException.AUTH_FAILED(acct.getName(), responseMessage);
    }

}

From source file:com.github.maven.plugin.client.impl.GithubClientImpl.java

public Set<String> listAvailableDownloads(String repositoryUrl) {
    assertStartsWith(repositoryUrl, GITHUB_REPOSITORY_URL_PREFIX, "repositoryUrl");

    final Set<String> downloads = new HashSet<String>();
    final GetMethod githubGet = new GetMethod(toRepositoryDownloadUrl(repositoryUrl));

    int response;

    try {//w w w .ja va2  s  .  co  m

        response = httpClient.executeMethod(githubGet);
    } catch (IOException e) {
        throw new GithubRepositoryNotFoundException(
                "Cannot retrieve github repository " + repositoryUrl + " informations", e);
    }

    if (response == HttpStatus.SC_OK) {

        String githubResponse;

        try {

            githubResponse = githubGet.getResponseBodyAsString();
        } catch (IOException e) {
            throw new GithubRepositoryNotFoundException(
                    "Cannot retrieve github repository " + repositoryUrl + "  informations", e);
        }

        Pattern pattern = Pattern.compile(
                String.format("<a href=\"/downloads%s/?([^\"]+)\"", removeGithubUrlPart(repositoryUrl)));

        Matcher matcher = pattern.matcher(githubResponse);
        while (matcher.find()) {
            downloads.add(matcher.group(1));
        }

    } else if (response == HttpStatus.SC_NOT_FOUND) {
        throw new GithubRepositoryNotFoundException("Cannot found repository " + repositoryUrl);
    } else {
        throw new GithubRepositoryNotFoundException(
                "Cannot retrieve github repository " + repositoryUrl + " informations");
    }

    githubGet.releaseConnection();

    return downloads;
}

From source file:de.escidoc.core.test.examples.RetrieveCompressedExamplesIT.java

@Test
public void testRetrieveCompressedExampleOrganizationalUnits() throws Exception {

    for (int i = 0; i < EXAMPLE_OU_IDS.length; ++i) {

        String ou = handleXmlResult(ouClient.retrieve(EXAMPLE_OU_IDS[i]));

        String response = null;//from  w  w  w.j  av  a 2  s  .  com

        String url = "http://localhost:8080" + Constants.ORGANIZATIONAL_UNIT_BASE_URI + "/" + EXAMPLE_OU_IDS[i];

        HttpClient client = new HttpClient();

        try {
            if (PropertiesProvider.getInstance().getProperty("http.proxyHost") != null
                    && PropertiesProvider.getInstance().getProperty("http.proxyPort") != null) {
                ProxyHost proxyHost = new ProxyHost(
                        PropertiesProvider.getInstance().getProperty("http.proxyHost"),
                        Integer.parseInt(PropertiesProvider.getInstance().getProperty("http.proxyPort")));

                client.getHostConfiguration().setProxyHost(proxyHost);
            }
        } catch (final Exception e) {
            throw new RuntimeException("[ClientBase] Error occured loading properties! " + e.getMessage(), e);
        }

        GetMethod getMethod = new GetMethod(url);
        getMethod.setRequestHeader("Accept-Encoding", "gzip");

        InputStream responseBody = null;
        try {

            int statusCode = client.executeMethod(getMethod);

            if (statusCode != HttpStatus.SC_OK) {
                throw new RuntimeException("getMethod failed:" + getMethod.getStatusLine());
            }
            responseBody = new GZIPInputStream(getMethod.getResponseBodyAsStream());

            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(responseBody, getMethod.getResponseCharSet()));
            StringBuffer result = new StringBuffer();

            char[] buffer = new char[4 * 1024];
            int charsRead;
            while ((charsRead = bufferedReader.read(buffer)) != -1) {
                result.append(buffer, 0, charsRead);
            }

            response = result.toString();
        } catch (Exception e) {
            throw new RuntimeException("Error occured in retrieving compressed example! " + e.getMessage(), e);
        }

        assertXmlEquals("Compressed document not the same like the uncompressed one", ou, response);
    }
}

From source file:exception.handler.authorization.HandledAuthorizationExceptionTest.java

@Test
public void authorizationException() throws HttpException, IOException {
    HttpClient client = new HttpClient();
    GetMethod method = new GetMethod(deploymentUrl + "/index");

    int status = client.executeMethod(method);
    String message = method.getResponseBodyAsString();

    assertEquals(SC_OK, status);// w  ww .  j a  va 2 s.  c  o  m
    assertTrue(message.contains("Authorization Exception!"));
}

From source file:mesquite.tol.lib.BaseHttpRequestMaker.java

public static boolean sendInfoToServer(NameValuePair[] pairs, String URI, StringBuffer response) {
    HttpClient client = new HttpClient();
    GetMethod method = new GetMethod(URI);
    method.setQueryString(pairs);//from w  w  w. j a  va2  s .  c  om

    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    return executeMethod(client, method, response);
}

From source file:com.google.apphosting.vmruntime.jetty9.VmRuntimeJettyAuthTest.java

public void testAuth_UserRequiredWithAdmin() throws Exception {
    HttpClient httpClient = new HttpClient();
    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
    GetMethod get = new GetMethod(createUrl("/user/test-auth").toString());
    get.addRequestHeader(VmApiProxyEnvironment.EMAIL_HEADER, "isdal@google.com");
    get.addRequestHeader(VmApiProxyEnvironment.AUTH_DOMAIN_HEADER, "google.com");
    get.addRequestHeader(VmApiProxyEnvironment.IS_ADMIN_HEADER, "1");

    get.setFollowRedirects(false);//from w  ww .  ja  v a 2s.c om
    int httpCode = httpClient.executeMethod(get);
    assertEquals(200, httpCode);
    assertEquals("isdal@google.com: isdal@google.com", get.getResponseBodyAsString());
}

From source file:com.bonitasoft.connector.Trello_Get_BoardImpl.java

@Override
protected void executeBusinessLogic() throws ConnectorException {
    // Get access to the connector input parameters
    // getBoard();
    // getToken();
    // getApiKey();
    HttpMethod method = null;/*from w  w  w  .j a v  a2 s .c  o  m*/
    try {
        HttpClient client = new HttpClient();
        method = new GetMethod(String.format(GET_TRELLO_BOARD_URL, getBoard(), getApiKey(), getToken()));
        method.addRequestHeader(new Header("Content-Type", "application/json; charset=UTF-8"));
        client.executeMethod(method);
        JSONArray array = getJSONFromResponse(method);

        List<Map<String, Object>> result = generateBaseStructure(array);
        setTrelloList(result);
        setBonitaColumn(TITLES);
        setBonitaList(getBonitaTableList(result));
        setStringCSV(generateCSVFromList(result));
    } catch (Exception e) {
        throw new ConnectorException(e.getMessage());
    } finally {
        try {
            method.releaseConnection();
        } catch (Exception e) {
            logger.severe("There is a problem releasing Trello Connection");
        }
    }
}

From source file:exception.handler.configuration.ExceptionHandlerDefaultConfigTest.java

@Test
public void defaultConfiguration() throws HttpException, IOException {
    HttpClient client = new HttpClient();
    GetMethod method = new GetMethod(deploymentUrl + "/index.jsf");

    client.executeMethod(method);//  w  w  w  . j  a  v a2 s.c om
    String message = method.getResponseBodyAsString();
    assertTrue(message.contains("Called the page /application_error"));
}