Example usage for org.apache.commons.httpclient HttpMethod getResponseBodyAsString

List of usage examples for org.apache.commons.httpclient HttpMethod getResponseBodyAsString

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpMethod getResponseBodyAsString.

Prototype

public abstract String getResponseBodyAsString() throws IOException;

Source Link

Usage

From source file:edu.du.penrose.systems.fedora.ResourceIndexUtils.java

/** USED 2-2-12
 * /* ww  w  . ja v  a 2 s.com*/
 * The administrator is just used to get host,port,user,pwd info.
 * The call to the resource index is actually via httpClient and rest call.
 * 
 * @param topCollection such as islandora:top or codu:top
 * @param namespace the names space of collections below the topCollection. <br>
 * <br>
 * For example, to retrieve the collection names for coduDuMaps and coduDuPicture...<br> <br>
 *    islandora:top - use topCollection=islandora:top and namespace=codu <br>
 *  &nbsp;&nbsp&nbsp;codu:duMaps <br>
 *  &nbsp;&nbsp&nbsp;codu:duPictures <br>
 *  &nbsp;&nbsp&nbsp;xxxx:yyyy <br>
 *  &nbsp;&nbsp&nbsp;zzzz:wwww <br><br>
 *      OR <br><br>
 *  islandora:top <br>
 *  &nbsp;&nbsp&nbsp;codu:top - use topCollection=codu:top and namespace=codu <br>
 *  &nbsp;&nbsp&nbsp;&nbsp;&nbsp&nbsp;codu:duMaps <br>
 *  &nbsp;&nbsp&nbsp;&nbsp;&nbsp&nbsp;codu:duPictures  <br> 
 *  
 *  @param administrator  The administrator is just used to get host,port,user,password info.
 *  @param topCollection
 *  @param nameSpace
 *  
 * @return map of collection names and  titles.
 */
public static Map<String, String> getChildCollectionsMap(Administrator administrator, String topCollection,
        String namespace) {
    final String host = administrator.getHost();
    final int port = administrator.getPort();
    String userName = administrator.getUserName();
    String pwd = administrator.getUserPassword();
    String realm = null; // "realm is actually Fedora or Fedora Repository Server"

    Map<String, String> collectionMap = new LinkedHashMap<String, String>();

    String UTF_8_ENCONDING = "UTF-8";

    String type = "tuples";
    String flush = "false"; // optional default false;
    String lang = "itql";
    String format = "CSV";
    String limit = "100"; // optional default unlimited;
    String distinct = "off"; // optional default off;
    String stream = "off"; // optional default off;
    String query = "select $subject $title from <#ri> where ( $subject <fedora-model:label> $title and $subject <fedora-rels-ext:isMemberOfCollection><info:fedora/"
            + topCollection + "> ) order by $title";
    // tbd quick kludge
    String query_2 = "select $subject $title from <#ri> where ( $subject <fedora-model:label> $title and $subject <fedora-rels-ext:isMemberOf><info:fedora/"
            + topCollection + "> ) order by $title";

    String queryEncoded;
    String queryEncoded_2;
    HttpMethod method = null;
    HttpMethod method_2 = null;
    try {
        queryEncoded = URLEncoder.encode(query, UTF_8_ENCONDING);
        queryEncoded_2 = URLEncoder.encode(query_2, UTF_8_ENCONDING);

        HttpClient myClient = new HttpClient();
        method = new GetMethod("http://" + host + ":" + port + "/fedora/risearch?type=" + type + "&lang=" + lang
                + "&format=" + format + "&limit=" + limit + "&distinct=" + distinct + "&stream=" + stream
                + "&query=" + queryEncoded);

        method_2 = new GetMethod("http://" + host + ":" + port + "/fedora/risearch?type=" + type + "&lang="
                + lang + "&format=" + format + "&limit=" + limit + "&distinct=" + distinct + "&stream=" + stream
                + "&query=" + queryEncoded_2);

        myClient.getState().setCredentials(new AuthScope(host, port, realm),
                new UsernamePasswordCredentials(userName, pwd));
        method.setDoAuthentication(true);
        myClient.getParams().setAuthenticationPreemptive(true);

        myClient.executeMethod(method);

        String response = method.getResponseBodyAsString();
        String lines[] = response.split("\n");

        for (int i = 0; i < lines.length; i++) {
            if (!lines[i].contains("islandora") && !lines[i].equalsIgnoreCase("\"subject\",\"title\"")) {

                String singleLine = lines[i].replace("info:fedora/", "");
                if (!singleLine.startsWith(namespace)) {
                    continue;
                }
                String[] pieces = singleLine.split(",");
                String collectionName = pieces[0];
                String collectionTitle = pieces[1];

                if (isCollection(administrator, collectionName)) // collectionName is the pid
                {
                    collectionMap.put(collectionName, collectionTitle);
                }
            }
        }

        myClient.executeMethod(method_2);

        String response_2 = method.getResponseBodyAsString();
        String lines_2[] = response_2.split("\n");

        for (int i = 0; i < lines_2.length; i++) {
            if (!lines_2[i].contains("islandora") && !lines_2[i].equalsIgnoreCase("\"subject\",\"title\"")) {

                String singleLine = lines_2[i].replace("info:fedora/", "");
                if (!singleLine.startsWith(namespace)) {
                    continue;
                }
                String[] pieces = singleLine.split(",");
                String collectionName = pieces[0];
                String collectionTitle = pieces[1];

                if (isCollection(administrator, collectionName)) // collectionName is the pid
                {
                    collectionMap.put(collectionName, collectionTitle);
                }
            }
        }
    } catch (Exception e) {
        collectionMap.put("", "Unable to get Collections:" + e);
        return collectionMap;
    } finally {
        method.releaseConnection();
        method_2.releaseConnection();
    }

    return collectionMap;
}

From source file:com.mirth.connect.connectors.ws.WebServiceMessageDispatcher.java

/**
 * Returns the URL for the passed in String. If the URL requires
 * authentication, then the WSDL is saved as a temp file and the URL for
 * that file is returned./*from ww w.  j a  v a2 s . co  m*/
 * 
 * @param wsdlUrl
 * @param username
 * @param password
 * @return
 * @throws Exception
 */
private URL getWsdlUrl(String wsdlUrl, String username, String password) throws Exception {
    URI uri = new URI(wsdlUrl);

    // If the URL points to file, just return it
    if (!uri.getScheme().equalsIgnoreCase("file")) {
        HttpClient client = new HttpClient();
        HttpMethod method = new GetMethod(wsdlUrl);

        int status = client.executeMethod(method);
        if ((status == HttpStatus.SC_UNAUTHORIZED) && (username != null) && (password != null)) {
            client.getState().setCredentials(AuthScope.ANY,
                    new UsernamePasswordCredentials(username, password));
            status = client.executeMethod(method);

            if (status == HttpStatus.SC_OK) {
                String wsdl = method.getResponseBodyAsString();
                File tempFile = File.createTempFile("WebServiceSender", ".wsdl");
                tempFile.deleteOnExit();

                FileUtils.writeStringToFile(tempFile, wsdl);

                return tempFile.toURI().toURL();
            }
        }
    }

    return uri.toURL();
}

From source file:gov.loc.ndmso.proxyfilter.RequestProxy.java

/**
 * This method performs the proxying of the request to the target address.
 *
 * @param target     The target address. Has to be a fully qualified address. The request is send as-is to this address.
 * @param hsRequest  The request data which should be send to the
 * @param hsResponse The response data which will contain the data returned by the proxied request to target.
 * @throws java.io.IOException Passed on from the connection logic.
 */// ww w.j  a  v  a  2  s  .  c o  m
public static void execute(final String target, final String collection, final HttpServletRequest hsRequest,
        final HttpServletResponse hsResponse, MultiThreadedHttpConnectionManager connManager)
        throws IOException {
    // log.info("execute, target is " + target);
    // log.info("response commit state: " + hsResponse.isCommitted());

    if (target == null || "".equals(target) || "".equals(target.trim())) {
        log.error("The target address is not given. Please provide a target address.");
        return;
    }

    // log.info("checking url");
    final URL url;
    try {
        url = new URL(target);
    } catch (MalformedURLException e) {
        // log.error("The provided target url is not valid.", e);
        return;
    }

    // log.info("setting up the host configuration");

    final HostConfiguration config = new HostConfiguration();

    ProxyHost proxyHost = getUseProxyServer((String) hsRequest.getAttribute("use-proxy"));
    if (proxyHost != null)
        config.setProxyHost(proxyHost);

    final int port = url.getPort() != -1 ? url.getPort() : url.getDefaultPort();
    config.setHost(url.getHost(), port, "http");

    // log.info("config is " + config.toString());

    final HttpMethod targetRequest = setupProxyRequest(hsRequest, url);
    if (targetRequest == null) {
        // log.error("Unsupported request method found: " + hsRequest.getMethod());
        return;
    }

    //perform the request to the target server
    final HttpClient client = new HttpClient(connManager);
    //if (log.isInfoEnabled()) {
    // log.info("client state" + client.getState());
    // log.info("client params" + client.getParams().toString());
    // log.info("executeMethod / fetching data ...");
    //}

    final int result = client.executeMethod(config, targetRequest);

    //copy the target response headers to our response
    setupResponseHeaders(targetRequest, hsResponse);

    String binRegex = ".*\\.(?i)(jpg|tif|png|gif|bmp|mp3|mpg)(.*$)*";
    String binRegexRedux = ".*(?i)(\\/thumb)(.*$)*";

    if (target.matches(binRegex) || target.matches(binRegexRedux)) {
        // log.info("binRegex matched: " + target);
        InputStream originalResponseStream = targetRequest.getResponseBodyAsStream();

        if (originalResponseStream != null) {

            if (targetRequest.getResponseHeaders().toString().matches("(?i).*content-type.*")) {
                PrintWriter responseStream = hsResponse.getWriter();
                copyStreamText(targetRequest.getResponseBodyAsString(), responseStream);
            } else {
                OutputStream responseStream = hsResponse.getOutputStream();
                copyStreamBinary(originalResponseStream, responseStream);
            }
        }

    } else {
        // log.info("binRegex NOT matched: " + target);
        String proxyResponseStr = targetRequest.getResponseBodyAsString();
        // the body might be null, i.e. for responses with cache-headers which leave out the body

        if (proxyResponseStr != null) {
            //proxyResponseStr = proxyResponseStr.replaceAll("xqy", "jsp");

            proxyResponseStr = proxyResponseStr.replaceAll("National Library Catalog",
                    "Library of Congress Data Service");
            proxyResponseStr = proxyResponseStr.replaceAll("Library of Congress collections",
                    "Library of Congress bibliographic data");
            proxyResponseStr = proxyResponseStr.replaceAll("Library of Congress Collections",
                    "Library of Congress Bibliographic Data");

            proxyResponseStr = proxyResponseStr.replaceAll("action=\"/", "action=\"/diglib/");
            proxyResponseStr = proxyResponseStr.replaceAll("href=\"/", "href=\"/diglib/");
            proxyResponseStr = proxyResponseStr.replaceAll("href=\"/diglib/loc\\.",
                    "href=\"/diglib/" + collection + "/loc.");
            proxyResponseStr = proxyResponseStr.replaceAll("src=\"/", "src=\"/diglib/");
            proxyResponseStr = proxyResponseStr.replaceAll("value=\"/", "value=\"/diglib/");
            proxyResponseStr = proxyResponseStr.replaceAll("url\\(/", "url\\(/diglib/");
            proxyResponseStr = proxyResponseStr.replaceAll("url\\(\"/", "url\\(\"/diglib/");
            proxyResponseStr = proxyResponseStr.replaceAll("src'\\) == \"/", "src'\\) == \"/diglib/");
            proxyResponseStr = proxyResponseStr.replaceAll("src\", \"/", "src\", \"/diglib/");
            proxyResponseStr = proxyResponseStr.replaceAll("natlibcat@loc.gov", "ndmso@loc.gov");

            proxyResponseStr = proxyResponseStr.replaceAll("/nlc/", "/lcds/");
            proxyResponseStr = proxyResponseStr.replaceAll("/lcwa/", "/lcwanew/");
            //proxyResponseStr = proxyResponseStr.replaceAll("/tohap/", "/x-tohap/");

            proxyResponseStr = proxyResponseStr.replaceAll(".xqy", ".jsp");

            PrintWriter responseStream = hsResponse.getWriter();
            copyStreamText(proxyResponseStr, responseStream);
        }
    }

    // log.info("set up response, result code was " + result);
    targetRequest.releaseConnection();

    // SimpleHttpConnectionManager connManager = (SimpleHttpConnectionManager) client.getHttpConnectionManager();
    // connManager.closeIdleConnections(1000);

    // HttpConnection httpConn = connManager.getConnection(config);
    // httpConn.releaseConnection();

}

From source file:autohit.call.modules.SimpleHttpModule.java

/**
 * Start method. It will set the target address for the client, as well as
 * clearing any state.//  w w w  .j a va 2 s .  c  o  m
 * 
 * @param url
 *            the Url path, not to include protocol, address, and port (ie.
 *            "/goats/index.html").
 * @return the data from the page as a String
 * @throws CallException
 */
private String get(String url) throws CallException {

    if (started == false) {
        throw buildException("module:SimpleHttp:Tried to get when a session wasn't started.",
                CallException.CODE_MODULE_FAULT);
    }

    String result = null;

    // Construct our method.
    HttpMethod method = new GetMethod(url);
    method.setFollowRedirects(true);
    method.setStrictMode(false);

    //execute the method
    try {
        // Do it
        debug("(get)get=" + url);
        httpClient.executeMethod(method);

        // Process result
        result = method.getResponseBodyAsString();
        log("(get)" + method.getStatusLine().toString() + " size=" + result.length());

    } catch (HttpException he) {
        // Bad but not fatal
        error("(get)Error on connect to url " + url + ".  Error=" + he.getMessage());
    } catch (IOException ioe) {
        // Fatal
        throw buildException("(get)Unable to connect.  Session is invalid.  message=" + ioe.getMessage(),
                CallException.CODE_MODULE_FAULT, ioe);
    } finally {
        try {
            method.releaseConnection();
            method.recycle();
        } catch (Exception e) {
            // Already FUBAR
        }
    }
    return result;
}

From source file:edu.du.penrose.systems.fedora.ResourceIndexUtils.java

/**
 * //ww  w. j  a v a 2s .  co  m
 * Get a list of all objects in the topCollection. The administrator is just used to get host,port,user, and pwd info.
 * The call to the resource index is actually via httpClient and rest call. 
 * 
 * @see FedoraAppBatchIngestController#getAdministrator()
 * @param administrator  The administrator is just used to get host,port,user,password info.
 * @param topCollection
 * @param onlyCollections =true to return only collection objects
 * 
 * @return all object pids in the topCollection that are also collections
 */
private static ArrayList<String> getChildren(Administrator administrator, String topCollection,
        boolean onlyCollections) {
    final String host = administrator.getHost();
    final int port = administrator.getPort();
    String userName = administrator.getUserName();
    String pwd = administrator.getUserPassword();
    String realm = null; // "realm is actually Fedora or Fedora Repository Server"

    ArrayList<String> children = new ArrayList<String>();

    String UTF_8_ENCONDING = "UTF-8";

    String type = "tuples";
    String flush = "false"; // optional default false;
    String lang = "itql";
    String format = "CSV";
    String limit = "100"; // optional default unlimited; NOT USED
    String distinct = "off"; // optional default off;
    String stream = "off"; // optional default off;
    String query = "select $subject from <#ri> where ( $subject <fedora-rels-ext:isMemberOfCollection><info:fedora/"
            + topCollection + "> )";
    // TBD kludge, I don't have time to do things right.
    String query_2 = "select $subject from <#ri> where ( $subject <fedora-rels-ext:isMemberOf><info:fedora/"
            + topCollection + "> )";

    String queryEncoded;
    String queryEncoded_2;
    HttpClient myClient = null;
    HttpMethod method = null;
    HttpMethod method_2 = null;
    try {
        queryEncoded = URLEncoder.encode(query, UTF_8_ENCONDING);
        queryEncoded_2 = URLEncoder.encode(query_2, UTF_8_ENCONDING);

        myClient = new HttpClient();
        String finalQuery = "http://" + host + ":" + port + "/fedora/risearch?type=" + type + "&lang=" + lang
                + "&format=" + format + "&distinct=" + distinct + "&stream=" + stream + "&query="
                + queryEncoded;
        String finalQuery_2 = "http://" + host + ":" + port + "/fedora/risearch?type=" + type + "&lang=" + lang
                + "&format=" + format + "&distinct=" + distinct + "&stream=" + stream + "&query="
                + queryEncoded_2;
        method = new GetMethod(finalQuery);
        method_2 = new GetMethod(finalQuery_2);

        myClient.getState().setCredentials(new AuthScope(host, port, realm),
                new UsernamePasswordCredentials(userName, pwd));
        method.setDoAuthentication(true);
        myClient.getParams().setAuthenticationPreemptive(true);

        myClient.executeMethod(method);

        String response = method.getResponseBodyAsString();
        String lines[] = response.split("\n");

        for (int i = 0; i < lines.length; i++) {
            if (!lines[i].contains("islandora") && !lines[i].equalsIgnoreCase("\"subject\"")) {
                String collectionPid = lines[i].replace("info:fedora/", "");
                if (onlyCollections) {
                    if (isCollection(administrator, collectionPid)) {
                        children.add(collectionPid);
                    }
                } else {
                    children.add(collectionPid);
                }
            }
        }

        myClient.executeMethod(method_2);

        String response_2 = method_2.getResponseBodyAsString();
        String lines_2[] = response_2.split("\n");

        for (int i = 0; i < lines_2.length; i++) {
            if (!lines_2[i].contains("islandora") && !lines_2[i].equalsIgnoreCase("\"subject\"")) {
                String collectionPid = lines_2[i].replace("info:fedora/", "");
                if (onlyCollections) {
                    if (isCollection(administrator, collectionPid)) {
                        children.add(collectionPid);
                    }
                } else {
                    children.add(collectionPid);
                }
            }
        }

    } catch (Exception e) {
        children.add("Unable to get Collections:" + e);
        return children;
    } finally {
        method.releaseConnection();
        method_2.releaseConnection();
    }

    return children;
}

From source file:net.sf.ehcache.constructs.web.filter.CachingFilterTest.java

/**
 * HEAD methods return an empty response body. If a HEAD request populates
 * a cache and then a GET follorws, a blank page will result.
 * This test ensures that the SimplePageCachingFilter implements calculateKey
 * properly to avoid this problem./*from www .ja  v  a2s  .c o  m*/
 */
public void testHeadThenGetOnCachedPage() throws Exception {
    HttpClient httpClient = new HttpClient();
    HttpMethod httpMethod = new HeadMethod(buildUrl(cachedPageUrl));
    int responseCode = httpClient.executeMethod(httpMethod);
    //httpclient follows redirects, so gets the home page.
    assertEquals(HttpURLConnection.HTTP_OK, responseCode);
    String responseBody = httpMethod.getResponseBodyAsString();
    assertNull(responseBody);
    assertNull(httpMethod.getResponseHeader("Content-Encoding"));

    httpMethod = new GetMethod(buildUrl(cachedPageUrl));
    responseCode = httpClient.executeMethod(httpMethod);
    responseBody = httpMethod.getResponseBodyAsString();
    assertNotNull(responseBody);

}

From source file:net.sourceforge.jwbf.actions.HttpActionClient.java

/**
 * Process a GET Message./*ww w . j  av a  2  s .  c o m*/
 * 
 * @param authgets
 *            a
 * @param cp
 *            a
 * @return a returning message, not null
 * @throws IOException on problems
 * @throws CookieException on problems
 * @throws ProcessException on problems
 */
protected String get(HttpMethod authgets, ContentProcessable cp)
        throws IOException, CookieException, ProcessException {
    showCookies(client);
    String out = "";
    authgets.getParams().setParameter("http.protocol.content-charset", MediaWikiBot.CHARSET);
    //      System.err.println(authgets.getParams().getParameter("http.protocol.content-charset"));

    client.executeMethod(authgets);
    cp.validateReturningCookies(client.getState().getCookies(), authgets);
    LOG.debug(authgets.getURI());
    LOG.debug("GET: " + authgets.getStatusLine().toString());

    out = authgets.getResponseBodyAsString();

    out = cp.processReturningText(out, authgets);
    // release any connection resources used by the method
    authgets.releaseConnection();
    int statuscode = authgets.getStatusCode();

    if (statuscode == HttpStatus.SC_NOT_FOUND) {
        LOG.warn("Not Found: " + authgets.getQueryString());

        throw new FileNotFoundException(authgets.getQueryString());
    }

    return out;
}

From source file:net.sf.ehcache.constructs.web.filter.CachingFilterTest.java

/**
 * When the servlet container generates a 404 page not found, we want to pass
 * it through without caching and without adding anything to it.
 * <p/>/*from   w w  w. java 2  s  .c  o  m*/
 * Manual Test: wget -d --server-response --header='Accept-Encoding: gzip'  http://localhost:9080/non_ok/SendRedirect.jsp
 */
public void testRedirect() throws Exception {

    String url = "http://localhost:9080/non_ok/SendRedirect.jsp";
    HttpClient httpClient = new HttpClient();
    HttpMethod httpMethod = new GetMethod(url);
    httpMethod.addRequestHeader("Accept-Encoding", "gzip");
    int responseCode = httpClient.executeMethod(httpMethod);
    //httpclient follows redirects, so gets the home page.
    assertEquals(HttpURLConnection.HTTP_OK, responseCode);
    String responseBody = httpMethod.getResponseBodyAsString();
    assertNotNull(responseBody);
    assertNull(httpMethod.getResponseHeader("Content-Encoding"));
}

From source file:it.drwolf.ridire.session.async.JobDBDataUpdater.java

private String[] getJobsArray() throws HttpException, IOException, SAXException, XPathExpressionException {
    if (this.getCrawlerEngineStatus().equals(CrawlerManager.STOPPED)) {
        return new String[] {};
    }//from   ww  w .jav a2 s. c o m
    HttpMethod method = null;
    List<String> ret = new ArrayList<String>();
    try {
        method = new PostMethod(this.engineUri);
        // method.setFollowRedirects(true);
        ((PostMethod) method).addParameter(new NameValuePair("action", "rescan"));
        // TODO check status code
        int status = this.httpClient.executeMethod(method);
        method.releaseConnection();
        method = new GetMethod(this.engineUri);
        status = this.httpClient.executeMethod(method);
        String body = method.getResponseBodyAsString();
        Matcher m = CrawlerManager.pJob.matcher(body);
        int start = 0;
        while (m.find(start)) {
            ret.add(m.group(1));
            start = m.end();
        }
        method.releaseConnection();
    } finally {
        if (method != null) {
            method.releaseConnection();
        }
    }
    return ret.toArray(new String[ret.size()]);
}

From source file:net.sf.ehcache.constructs.web.filter.CachingFilterTest.java

/**
 * When the servlet container generates a 404 page not found, we want to pass
 * it through without caching and without adding anything to it.
 * <p/>//from w  w w . ja v a  2  s. c  o m
 * Manual Test: wget -d --server-response --header='Accept-Encoding: gzip'  http://localhost:9080/non_ok/PageNotFound.jsp
 */
public void testNotFound() throws Exception {

    String url = "http://localhost:9080/non_ok/PageNotFound.jsp";
    HttpClient httpClient = new HttpClient();
    HttpMethod httpMethod = new GetMethod(url);
    httpMethod.addRequestHeader("If-modified-Since", "Fri, 13 May 3006 23:54:18 GMT");
    httpMethod.addRequestHeader("Accept-Encoding", "gzip");
    int responseCode = httpClient.executeMethod(httpMethod);
    assertEquals(HttpURLConnection.HTTP_NOT_FOUND, responseCode);
    String responseBody = httpMethod.getResponseBodyAsString();
    assertNotNull(responseBody);
    assertNull(httpMethod.getResponseHeader("Content-Encoding"));
}