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

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

Introduction

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

Prototype

public void setQueryString(String queryString) 

Source Link

Usage

From source file:com.gisgraphy.servlet.GeolocServletTest.java

@Test
public void testShouldReturnCorrectStatusCode() {
    String url = geolocServletUrl + GEOLOC_SERVLET_CONTEXT + "/geolocsearch";

    String queryStringWithMissingLat;
    GetMethod get = null;
    try {/*from  w w w  .j  av a2 s.c  o  m*/
        queryStringWithMissingLat = GeolocQuery.LONG_PARAMETER + "=4&format=" + OutputFormat.JSON.toString();
        HttpClient client = new HttpClient();
        get = new GetMethod(url);

        get.setQueryString(queryStringWithMissingLat);
        client.executeMethod(get);
        // result = get.getResponseBodyAsString();

        assertEquals("status code is not correct ", 500, get.getStatusCode());

    } catch (IOException e) {
        fail("An exception has occured " + e.getMessage());
    } finally {
        if (get != null) {
            get.releaseConnection();
        }
    }

}

From source file:com.gisgraphy.servlet.GeolocServletTest.java

@Test
public void testShouldReturnCorrectContentTypeForSupportedFormatWhenErrorOccured() {
    String url = geolocServletUrl + GEOLOC_SERVLET_CONTEXT + "/geolocsearch";

    String queryStringWithMissingLat;
    for (OutputFormat format : OutputFormat.values()) {
        GetMethod get = null;
        try {/*from  w w w.j  a  va2 s.  c o m*/
            queryStringWithMissingLat = GeolocQuery.LONG_PARAMETER + "=4&format=" + format.toString();
            HttpClient client = new HttpClient();
            get = new GetMethod(url);

            get.setQueryString(queryStringWithMissingLat);
            client.executeMethod(get);
            // result = get.getResponseBodyAsString();

            Header contentType = get.getResponseHeader("Content-Type");
            OutputFormat expectedformat = OutputFormatHelper.getDefaultForServiceIfNotSupported(format,
                    GisgraphyServiceType.GEOLOC);
            assertEquals("The content-type is not correct", expectedformat.getContentType(),
                    contentType.getValue());

        } catch (IOException e) {
            fail("An exception has occured " + e.getMessage());
        } finally {
            if (get != null) {
                get.releaseConnection();
            }
        }
    }

}

From source file:com.gisgraphy.servlet.FulltextServletTest.java

@Test
public void testFulltextServletShouldReturnCorrectJSONError() {

    JsTester jsTester = null;//from   w w w . ja va  2s  . c o  m
    String url = fulltextServletUrl + FULLTEXT_SERVLET_CONTEXT + "/fulltextsearch";

    String result;
    String queryString;
    OutputFormat format = OutputFormat.JSON;
    GetMethod get = null;
    try {
        jsTester = new JsTester();
        jsTester.onSetUp();
        queryString = "q=&format=" + format.getParameterValue();
        HttpClient client = new HttpClient();
        get = new GetMethod(url);

        get.setQueryString(queryString);
        client.executeMethod(get);
        result = get.getResponseBodyAsString();

        //check content 
        Header contentType = get.getResponseHeader("Content-Type");
        assertEquals("The content-type is not correct", OutputFormat.JSON.getContentType(),
                contentType.getValue());

        // JsTester
        jsTester.eval("evalresult= eval(" + result + ");");
        jsTester.assertNotNull("evalresult");
        String error = jsTester.eval("evalresult.responseHeader.error").toString();
        String expected = ResourceBundle.getBundle(Constants.BUNDLE_ERROR_KEY).getString("error.emptyQuery");
        assertEquals(expected, error);

        error = jsTester.eval("evalresult.responseHeader.status").toString();
        assertEquals("-1.0", error);// -1.0 because it is considered as a
        // float

    } catch (IOException e) {
        fail("An exception has occured " + e.getMessage());
    } finally {
        if (jsTester != null) {
            jsTester.onTearDown();
        }
        if (get != null) {
            get.releaseConnection();
        }
    }

}

From source file:com.gisgraphy.servlet.ReverseGeocodingServletTest.java

@Test
public void testShouldReturnCorrectContentTypeForSupportedFormatWhenErrorOccured() {
    String url = reverseGeocodingServletUrl + REVERSE_GECODOING_SERVLET_CONTEXT + "/search";

    String queryStringWithMissingLat;
    for (OutputFormat format : OutputFormat.values()) {
        GetMethod get = null;
        try {/* w ww. j ava  2  s . c  o  m*/
            queryStringWithMissingLat = ReverseGeocodingQuery.LONG_PARAMETER + "=4&format=" + format.toString();
            HttpClient client = new HttpClient();
            get = new GetMethod(url);

            get.setQueryString(queryStringWithMissingLat);
            client.executeMethod(get);
            // result = get.getResponseBodyAsString();

            Header contentType = get.getResponseHeader("Content-Type");
            OutputFormat expectedformat = OutputFormatHelper.getDefaultForServiceIfNotSupported(format,
                    GisgraphyServiceType.REVERSEGEOCODING);
            assertEquals("The content-type is not correct", expectedformat.getContentType(),
                    contentType.getValue());

        } catch (IOException e) {
            fail("An exception has occured " + e.getMessage());
        } finally {
            if (get != null) {
                get.releaseConnection();
            }
        }
    }

}

From source file:com.gisgraphy.servlet.ReverseGeocodingServletTest.java

@Test
public void testShouldReturnCorrectStatusCode() {
    String url = reverseGeocodingServletUrl + REVERSE_GECODOING_SERVLET_CONTEXT + "/geolocsearch";

    String queryStringWithMissingLat;
    GetMethod get = null;
    try {/*from w w w.  j  a v  a2s  .c o m*/
        queryStringWithMissingLat = ReverseGeocodingQuery.LONG_PARAMETER + "=4&format="
                + OutputFormat.JSON.toString();
        HttpClient client = new HttpClient();
        get = new GetMethod(url);

        get.setQueryString(queryStringWithMissingLat);
        client.executeMethod(get);
        // result = get.getResponseBodyAsString();

        assertEquals("status code is not correct ", 500, get.getStatusCode());

    } catch (IOException e) {
        fail("An exception has occured " + e.getMessage());
    } finally {
        if (get != null) {
            get.releaseConnection();
        }
    }

}

From source file:com.gisgraphy.servlet.GeolocServletTest.java

@Test
public void testShouldReturnCorrectJSONError() {

    JsTester jsTester = null;//from w  w w.j av  a 2 s.  co m
    String url = geolocServletUrl + GEOLOC_SERVLET_CONTEXT + "/geolocsearch";

    String result;
    String queryString;
    OutputFormat format = OutputFormat.JSON;
    GetMethod get = null;
    try {
        jsTester = new JsTester();
        jsTester.onSetUp();
        queryString = "format=" + format.getParameterValue();
        HttpClient client = new HttpClient();
        get = new GetMethod(url);

        get.setQueryString(queryString);
        client.executeMethod(get);
        result = get.getResponseBodyAsString();

        // JsTester
        jsTester.eval("evalresult= eval(" + result + ");");
        jsTester.assertNotNull("evalresult");
        String error = jsTester.eval("evalresult.error").toString();
        String expected = ResourceBundle.getBundle(Constants.BUNDLE_ERROR_KEY).getString("error.emptyLatLong");
        assertEquals(expected, error);

    } catch (IOException e) {
        fail("An exception has occured " + e.getMessage());
    } finally {
        if (jsTester != null) {
            jsTester.onTearDown();
        }
        if (get != null) {
            get.releaseConnection();
        }
    }

}

From source file:com.gisgraphy.servlet.ReverseGeocodingServletTest.java

@Test
public void testShouldReturnCorrectJSONError() {

    JsTester jsTester = null;//from   w w  w  . j a  v a2 s .c  o m
    String url = reverseGeocodingServletUrl + REVERSE_GECODOING_SERVLET_CONTEXT + "/search";

    String result;
    String queryString;
    OutputFormat format = OutputFormat.JSON;
    GetMethod get = null;
    try {
        jsTester = new JsTester();
        jsTester.onSetUp();
        queryString = "format=" + format.getParameterValue();
        HttpClient client = new HttpClient();
        get = new GetMethod(url);

        get.setQueryString(queryString);
        client.executeMethod(get);
        result = get.getResponseBodyAsString();

        // JsTester
        jsTester.eval("evalresult= eval(" + result + ");");
        jsTester.assertNotNull("evalresult");
        String error = jsTester.eval("evalresult.error").toString();
        String expected = ResourceBundle.getBundle(Constants.BUNDLE_ERROR_KEY).getString("error.emptyLatLong");
        assertEquals(expected, error);

    } catch (IOException e) {
        fail("An exception has occured " + e.getMessage());
    } finally {
        if (jsTester != null) {
            jsTester.onTearDown();
        }
        if (get != null) {
            get.releaseConnection();
        }
    }

}

From source file:com.owncloud.android.lib.resources.users.RemoteGetUserQuotaOperation.java

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    RemoteOperationResult result = null;
    int status;/*from  w  w w.j  av  a  2 s  .  c o  m*/
    GetMethod get = null;

    //Get the user
    try {
        OwnCloudBasicCredentials credentials = (OwnCloudBasicCredentials) client.getCredentials();
        String url = client.getBaseUri() + OCS_ROUTE + credentials.getUsername();

        get = new GetMethod(url);
        get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
        get.setQueryString(new NameValuePair[] { new NameValuePair("format", "json") });
        status = client.executeMethod(get);

        if (isSuccess(status)) {
            String response = get.getResponseBodyAsString();

            // Parse the response
            JSONObject respJSON = new JSONObject(response);
            JSONObject respOCS = respJSON.getJSONObject(NODE_OCS);
            JSONObject respData = respOCS.getJSONObject(NODE_DATA);
            JSONObject quota = respData.getJSONObject(NODE_QUOTA);
            final Long quotaFree = quota.getLong(NODE_QUOTA_FREE);
            final Long quotaUsed = quota.getLong(NODE_QUOTA_USED);
            final Long quotaTotal = quota.getLong(NODE_QUOTA_TOTAL);
            final Double quotaRelative = quota.getDouble(NODE_QUOTA_RELATIVE);
            Long quotaValue;
            try {
                quotaValue = quota.getLong(NODE_QUOTA);
            } catch (JSONException e) {
                Log_OC.i(TAG, "Legacy server in use < Nextcloud 9.0.54");
                quotaValue = QUOTA_LIMIT_INFO_NOT_AVAILABLE;
            }

            // Result
            result = new RemoteOperationResult(true, status, get.getResponseHeaders());

            // Quota data in data collection
            ArrayList<Object> data = new ArrayList<>();
            data.add(new Quota(quotaFree, quotaUsed, quotaTotal, quotaRelative, quotaValue));
            result.setData(data);
        } else {
            result = new RemoteOperationResult(false, status, get.getResponseHeaders());
            String response = get.getResponseBodyAsString();
            Log_OC.e(TAG, "Failed response while getting user quota information ");
            if (response != null) {
                Log_OC.e(TAG, "*** status code: " + status + " ; response message: " + response);
            } else {
                Log_OC.e(TAG, "*** status code: " + status);
            }
        }
    } catch (Exception e) {
        result = new RemoteOperationResult(e);
        Log_OC.e(TAG, "Exception while getting OC user information", e);
    } finally {
        if (get != null) {
            get.releaseConnection();
        }
    }
    return result;
}

From source file:com.gisgraphy.servlet.GeolocServletTest.java

@Test
public void testShouldReturnCorrectContentTypeForSupportedFormat() {
    String url = geolocServletUrl + GEOLOC_SERVLET_CONTEXT + "/geolocsearch";

    String queryString;/*  w  ww .  j  a  v  a2s  . c o m*/
    for (OutputFormat format : OutputFormat.values()) {
        GetMethod get = null;
        try {
            queryString = GeolocQuery.LAT_PARAMETER + "=3&" + GeolocQuery.LONG_PARAMETER + "=4&format="
                    + format.toString();
            HttpClient client = new HttpClient();
            get = new GetMethod(url);

            get.setQueryString(queryString);
            client.executeMethod(get);
            // result = get.getResponseBodyAsString();

            Header contentType = get.getResponseHeader("Content-Type");
            OutputFormat expectedformat = OutputFormatHelper.getDefaultForServiceIfNotSupported(format,
                    GisgraphyServiceType.GEOLOC);
            assertTrue(contentType.getValue().equals(expectedformat.getContentType()));

        } catch (IOException e) {
            fail("An exception has occured " + e.getMessage());
        } finally {
            if (get != null) {
                get.releaseConnection();
            }
        }
    }

}

From source file:de.laeubisoft.tools.ant.validation.W3CMarkupValidationTask.java

/**
 * Creates the actual request to the validation server for a given
 * {@link URL} and returns an inputstream the result can be read from
 * /*w w w.  ja v  a  2 s . co  m*/
 * @param uriToCheck
 *            the URL to check
 * @return the stream to read the response from
 * @throws IOException
 *             if unrecoverable communication error occurs
 * @throws BuildException
 *             if server returned unexspected results
 */
private InputStream buildConnection(final URL uriToCheck) throws IOException, BuildException {
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new NameValuePair("output", VALIDATOR_FORMAT_OUTPUT));
    if (uriToCheck != null) {
        params.add(new NameValuePair("uri", uriToCheck.toString()));
    } else {
        if (fragment != null) {
            params.add(new NameValuePair("fragment", fragment));
        }
    }
    if (debug) {
        params.add(new NameValuePair("debug", "1"));
    }
    if (charset != null) {
        params.add(new NameValuePair("charset", charset));
    }
    if (doctype != null) {
        params.add(new NameValuePair("doctype", doctype));
    }
    HttpClient httpClient = new HttpClient();
    HttpMethodBase method;
    if (uriToCheck != null) {
        //URIs must be checked wia traditonal GET...
        GetMethod getMethod = new GetMethod(validator);
        getMethod.setQueryString(params.toArray(new NameValuePair[0]));
        method = getMethod;
    } else {
        PostMethod postMethod = new PostMethod(validator);
        if (fragment != null) {
            //Fragment request can be checked via FORM Submission
            postMethod.addParameters(params.toArray(new NameValuePair[0]));
        } else {
            //Finally files must be checked with multipart-forms....
            postMethod.setRequestEntity(Tools.createFileUpload(uploaded_file, "uploaded_file", charset, params,
                    postMethod.getParams()));
        }
        method = postMethod;
    }
    int result = httpClient.executeMethod(method);
    if (result == HttpStatus.SC_OK) {
        return method.getResponseBodyAsStream();
    } else {
        throw new BuildException("Server returned " + result + " " + method.getStatusText());
    }

}