Example usage for org.apache.commons.httpclient HttpStatus SC_MOVED_PERMANENTLY

List of usage examples for org.apache.commons.httpclient HttpStatus SC_MOVED_PERMANENTLY

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpStatus SC_MOVED_PERMANENTLY.

Prototype

int SC_MOVED_PERMANENTLY

To view the source code for org.apache.commons.httpclient HttpStatus SC_MOVED_PERMANENTLY.

Click Source Link

Document

<tt>301 Moved Permanently</tt> (HTTP/1.0 - RFC 1945)

Usage

From source file:org.yccheok.jstock.gui.UtilsRef.java

/**
 * Get response body through non-standard POST method.
 * Please refer to <url>http://stackoverflow.com/questions/1473255/is-jakarta-httpclient-sutitable-for-the-following-task/1473305#1473305</url>
 *
 * @param uri For example, http://X/%5bvUpJYKw4QvGRMBmhATUxRwv4JrU9aDnwNEuangVyy6OuHxi2YiY=%5dImage?
 * @param formData For example, [SORT]=0,1,0,10,5,0,KL,0&[FIELD]=33,38,51
 * @return the response body. null if fail.
 *//*from  w ww. j av  a 2  s  .  c om*/
public static String getPOSTResponseBodyAsStringBasedOnProxyAuthOption(String uri, String formData) {
    ///org.yccheok.jstock.engine.Utils.setHttpClientProxyFromSystemProperties(httpClient);
    org.yccheok.jstock.gui.UtilsRef.setHttpClientProxyCredentialsFromJStockOptions(httpClient);

    final PostMethod method = new PostMethod(uri);
    final RequestEntity entity;
    try {
        entity = new StringRequestEntity(formData, "application/x-www-form-urlencoded", "UTF-8");
    } catch (UnsupportedEncodingException exp) {
        log.error(null, exp);
        return null;
    }
    method.setRequestEntity(entity);
    method.setContentChunked(false);

    ///final JStockOptions jStockOptions = MainFrame.getInstance().getJStockOptions();
    String respond = null;
    try {
        if (false/*jStockOptions.isProxyAuthEnabled()*/) {
            /* WARNING : This chunck of code block is not tested! */
            method.setFollowRedirects(false);
            httpClient.executeMethod(method);

            int statuscode = method.getStatusCode();
            if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY)
                    || (statuscode == HttpStatus.SC_MOVED_PERMANENTLY)
                    || (statuscode == HttpStatus.SC_SEE_OTHER)
                    || (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
                //Make new Request with new URL
                Header header = method.getResponseHeader("location");
                /* WARNING : Correct method to redirect? Shall we use POST? How about form data? */
                HttpMethod RedirectMethod = new GetMethod(header.getValue());
                // I assume it is OK to release method for twice. (The second
                // release will happen in finally block). We shouldn't have an
                // unreleased method, before executing another new method.
                method.releaseConnection();
                // Do RedirectMethod within try-catch-finally, so that we can have a
                // exception free way to release RedirectMethod connection.
                // #2836422
                try {
                    httpClient.executeMethod(RedirectMethod);
                    respond = RedirectMethod.getResponseBodyAsString();
                } catch (HttpException exp) {
                    log.error(null, exp);
                    return null;
                } catch (IOException exp) {
                    log.error(null, exp);
                    return null;
                } finally {
                    RedirectMethod.releaseConnection();
                }
            } else {
                respond = method.getResponseBodyAsString();
            } // if statuscode = Redirect
        } else {
            httpClient.executeMethod(method);
            respond = method.getResponseBodyAsString();
        } //  if jStockOptions.isProxyAuthEnabled()
    } catch (HttpException exp) {
        log.error(null, exp);
        return null;
    } catch (IOException exp) {
        log.error(null, exp);
        return null;
    } finally {
        method.releaseConnection();
    }
    return respond;
}

From source file:org.yccheok.jstock.gui.UtilsRef.java

public static String getResponseBodyAsStringBasedOnProxyAuthOption(String request) {
    ///org.yccheok.jstock.engine.Utils.setHttpClientProxyFromSystemProperties(httpClient);
    if (!mytest)/*from   ww  w.java 2s.  com*/
        org.yccheok.jstock.gui.UtilsRef.setHttpClientProxyCredentialsFromJStockOptions(httpClient);

    final HttpMethod method = new GetMethod(request);
    ///final JStockOptions jStockOptions = MainFrame.getInstance().getJStockOptions();
    String respond = null;
    try {
        if (true/*!mytest&&jStockOptions.isProxyAuthEnabled()*/) {
            method.setFollowRedirects(false);
            httpClient.executeMethod(method);

            int statuscode = method.getStatusCode();
            if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY)
                    || (statuscode == HttpStatus.SC_MOVED_PERMANENTLY)
                    || (statuscode == HttpStatus.SC_SEE_OTHER)
                    || (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
                //Make new Request with new URL
                Header header = method.getResponseHeader("location");
                HttpMethod RedirectMethod = new GetMethod(header.getValue());
                // I assume it is OK to release method for twice. (The second
                // release will happen in finally block). We shouldn't have an
                // unreleased method, before executing another new method.
                method.releaseConnection();
                // Do RedirectMethod within try-catch-finally, so that we can have a
                // exception free way to release RedirectMethod connection.
                // #2836422
                try {
                    httpClient.executeMethod(RedirectMethod);
                    respond = RedirectMethod.getResponseBodyAsString();
                } catch (HttpException exp) {
                    log.error(null, exp);
                    return null;
                } catch (IOException exp) {
                    log.error(null, exp);
                    return null;
                } finally {
                    RedirectMethod.releaseConnection();
                }
            } else {
                respond = method.getResponseBodyAsString();
            } // if statuscode = Redirect
        } else {
            httpClient.executeMethod(method);
            respond = method.getResponseBodyAsString();
        } //  if jStockOptions.isProxyAuthEnabled()
    } catch (HttpException exp) {
        log.error(null, exp);
        return null;
    } catch (IOException exp) {
        log.error(null, exp);
        return null;
    } finally {
        method.releaseConnection();
    }
    return respond;
}

From source file:org.yccheok.jstock.gui.UtilsRef.java

public static InputStreamAndMethod getResponseBodyAsStreamBasedOnProxyAuthOption(String request) {
    ///org.yccheok.jstock.engine.Utils.setHttpClientProxyFromSystemProperties(httpClient);
    org.yccheok.jstock.gui.UtilsRef.setHttpClientProxyCredentialsFromJStockOptions(httpClient);

    final GetMethod method = new GetMethod(request);
    ///final JStockOptions jStockOptions = MainFrame.getInstance().getJStockOptions();
    InputStreamAndMethod inputStreamAndMethod = null;
    InputStream respond = null;/*from w w  w.j  a  v  a 2 s.c  om*/
    HttpMethod methodToClosed = method;

    try {
        if (false/*jStockOptions.isProxyAuthEnabled()*/) {
            method.setFollowRedirects(false);
            httpClient.executeMethod(method);

            int statuscode = method.getStatusCode();
            if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY)
                    || (statuscode == HttpStatus.SC_MOVED_PERMANENTLY)
                    || (statuscode == HttpStatus.SC_SEE_OTHER)
                    || (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
                //Make new Request with new URL
                Header header = method.getResponseHeader("location");
                GetMethod RedirectMethod = new GetMethod(header.getValue());
                methodToClosed = RedirectMethod;
                method.releaseConnection();
                // Do RedirectMethod within try-catch-finally, so that we can have a
                // exception free way to release RedirectMethod connection.
                // #2836422                    
                try {
                    httpClient.executeMethod(RedirectMethod);
                    respond = RedirectMethod.getResponseBodyAsStream();
                } catch (HttpException exp) {
                    log.error(null, exp);
                } catch (IOException exp) {
                    log.error(null, exp);
                }
            } else {
                methodToClosed = method;
                respond = method.getResponseBodyAsStream();
            } // if statuscode = Redirect
        } else {
            methodToClosed = method;
            httpClient.executeMethod(method);
            respond = method.getResponseBodyAsStream();
        } //  if jStockOptions.isProxyAuthEnabled()
    } catch (HttpException exp) {
        log.error(null, exp);
    } catch (IOException exp) {
        log.error(null, exp);
    } finally {
        inputStreamAndMethod = new InputStreamAndMethod(respond, methodToClosed);
    }

    return inputStreamAndMethod;
}

From source file:org.yccheok.jstock.gui.Utils.java

/**
 * Get response body through non-standard POST method.
 * Please refer to <url>http://stackoverflow.com/questions/1473255/is-jakarta-httpclient-sutitable-for-the-following-task/1473305#1473305</url>
 *
 * @param uri For example, http://X/%5bvUpJYKw4QvGRMBmhATUxRwv4JrU9aDnwNEuangVyy6OuHxi2YiY=%5dImage?
 * @param formData For example, [SORT]=0,1,0,10,5,0,KL,0&[FIELD]=33,38,51
 * @return the response body. null if fail.
 *///  w  ww  .ja  v a2  s. c  o  m
public static String getPOSTResponseBodyAsStringBasedOnProxyAuthOption(String uri, String formData) {
    org.yccheok.jstock.engine.Utils.setHttpClientProxyFromSystemProperties(httpClient);
    org.yccheok.jstock.gui.Utils.setHttpClientProxyCredentialsFromJStockOptions(httpClient);

    final PostMethod method = new PostMethod(uri);
    final RequestEntity entity;
    try {
        entity = new StringRequestEntity(formData, "application/x-www-form-urlencoded", "UTF-8");
    } catch (UnsupportedEncodingException exp) {
        log.error(null, exp);
        return null;
    }
    method.setRequestEntity(entity);
    method.setContentChunked(false);

    final JStockOptions jStockOptions = JStock.instance().getJStockOptions();
    String respond = null;
    try {
        if (jStockOptions.isProxyAuthEnabled()) {
            /* WARNING : This chunck of code block is not tested! */
            method.setFollowRedirects(false);
            httpClient.executeMethod(method);

            int statuscode = method.getStatusCode();
            if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY)
                    || (statuscode == HttpStatus.SC_MOVED_PERMANENTLY)
                    || (statuscode == HttpStatus.SC_SEE_OTHER)
                    || (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
                //Make new Request with new URL
                Header header = method.getResponseHeader("location");
                /* WARNING : Correct method to redirect? Shall we use POST? How about form data? */
                HttpMethod RedirectMethod = new GetMethod(header.getValue());
                // I assume it is OK to release method for twice. (The second
                // release will happen in finally block). We shouldn't have an
                // unreleased method, before executing another new method.
                method.releaseConnection();
                // Do RedirectMethod within try-catch-finally, so that we can have a
                // exception free way to release RedirectMethod connection.
                // #2836422
                try {
                    httpClient.executeMethod(RedirectMethod);
                    respond = RedirectMethod.getResponseBodyAsString();
                } catch (HttpException exp) {
                    log.error(null, exp);
                    return null;
                } catch (IOException exp) {
                    log.error(null, exp);
                    return null;
                } finally {
                    RedirectMethod.releaseConnection();
                }
            } else {
                respond = method.getResponseBodyAsString();
            } // if statuscode = Redirect
        } else {
            httpClient.executeMethod(method);
            respond = method.getResponseBodyAsString();
        } //  if jStockOptions.isProxyAuthEnabled()
    } catch (HttpException exp) {
        log.error(null, exp);
        return null;
    } catch (IOException exp) {
        log.error(null, exp);
        return null;
    } finally {
        method.releaseConnection();
    }
    return respond;
}

From source file:org.yccheok.jstock.gui.Utils.java

private static String _getResponseBodyAsStringBasedOnProxyAuthOption(HttpClient client, String request) {
    org.yccheok.jstock.engine.Utils.setHttpClientProxyFromSystemProperties(client);
    org.yccheok.jstock.gui.Utils.setHttpClientProxyCredentialsFromJStockOptions(client);

    final HttpMethod method = new GetMethod(request);
    final JStockOptions jStockOptions = JStock.instance().getJStockOptions();
    String respond = null;/* w w w.j a va  2s.co m*/
    try {
        if (jStockOptions.isProxyAuthEnabled()) {
            method.setFollowRedirects(false);
            client.executeMethod(method);

            int statuscode = method.getStatusCode();
            if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY)
                    || (statuscode == HttpStatus.SC_MOVED_PERMANENTLY)
                    || (statuscode == HttpStatus.SC_SEE_OTHER)
                    || (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
                //Make new Request with new URL
                Header header = method.getResponseHeader("location");
                HttpMethod RedirectMethod = new GetMethod(header.getValue());
                // I assume it is OK to release method for twice. (The second
                // release will happen in finally block). We shouldn't have an
                // unreleased method, before executing another new method.
                method.releaseConnection();
                // Do RedirectMethod within try-catch-finally, so that we can have a
                // exception free way to release RedirectMethod connection.
                // #2836422
                try {
                    client.executeMethod(RedirectMethod);
                    respond = RedirectMethod.getResponseBodyAsString();
                } catch (HttpException exp) {
                    log.error(null, exp);
                    return null;
                } catch (IOException exp) {
                    log.error(null, exp);
                    return null;
                } finally {
                    RedirectMethod.releaseConnection();
                }
            } else {
                respond = method.getResponseBodyAsString();
            } // if statuscode = Redirect
        } else {
            client.executeMethod(method);
            respond = method.getResponseBodyAsString();
        } //  if jStockOptions.isProxyAuthEnabled()
    } catch (HttpException exp) {
        log.error(null, exp);
        return null;
    } catch (IOException exp) {
        log.error(null, exp);
        return null;
    } finally {
        method.releaseConnection();
    }
    return respond;
}

From source file:org.yccheok.jstock.gui.Utils.java

public static InputStreamAndMethod getResponseBodyAsStreamBasedOnProxyAuthOption(String request) {
    org.yccheok.jstock.engine.Utils.setHttpClientProxyFromSystemProperties(httpClient);
    org.yccheok.jstock.gui.Utils.setHttpClientProxyCredentialsFromJStockOptions(httpClient);

    final GetMethod method = new GetMethod(request);
    final JStockOptions jStockOptions = JStock.instance().getJStockOptions();
    InputStreamAndMethod inputStreamAndMethod = null;
    InputStream respond = null;/*from   w  w w .j  a va 2s  . c om*/
    HttpMethod methodToClosed = method;

    try {
        if (jStockOptions.isProxyAuthEnabled()) {
            method.setFollowRedirects(false);
            httpClient.executeMethod(method);

            int statuscode = method.getStatusCode();
            if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY)
                    || (statuscode == HttpStatus.SC_MOVED_PERMANENTLY)
                    || (statuscode == HttpStatus.SC_SEE_OTHER)
                    || (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
                //Make new Request with new URL
                Header header = method.getResponseHeader("location");
                GetMethod RedirectMethod = new GetMethod(header.getValue());
                methodToClosed = RedirectMethod;
                method.releaseConnection();
                // Do RedirectMethod within try-catch-finally, so that we can have a
                // exception free way to release RedirectMethod connection.
                // #2836422                    
                try {
                    httpClient.executeMethod(RedirectMethod);
                    respond = RedirectMethod.getResponseBodyAsStream();
                } catch (HttpException exp) {
                    log.error(null, exp);
                } catch (IOException exp) {
                    log.error(null, exp);
                }
            } else {
                methodToClosed = method;
                respond = method.getResponseBodyAsStream();
            } // if statuscode = Redirect
        } else {
            methodToClosed = method;
            httpClient.executeMethod(method);
            respond = method.getResponseBodyAsStream();
        } //  if jStockOptions.isProxyAuthEnabled()
    } catch (HttpException exp) {
        log.error(null, exp);
    } catch (IOException exp) {
        log.error(null, exp);
    } finally {
        inputStreamAndMethod = new InputStreamAndMethod(respond, methodToClosed);
    }

    return inputStreamAndMethod;
}

From source file:org.zaizi.alfresco.redlink.service.search.solr.SensefySolrQueryHTTPClient.java

public ResultSet executeQuery(SearchParameters searchParameters)// , String language)
{
    if (repositoryState.isBootstrapping()) {
        throw new AlfrescoRuntimeException(
                "SOLR queries can not be executed while the repository is bootstrapping");
    }//from  w w w . ja va 2  s . c o m

    try {
        if (searchParameters.getStores().size() == 0) {
            throw new AlfrescoRuntimeException("No store for query");
        }

        URLCodec encoder = new URLCodec();
        StringBuilder url = new StringBuilder();
        url.append(sensefySearchEndpoint);

        // Send the query
        url.append("?query=");
        url.append(encoder.encode(searchParameters.getQuery(), "UTF-8"));
        // url.append("?wt=").append(encoder.encode("json", "UTF-8"));
        url.append("&fields=").append(encoder.encode("DBID,score", "UTF-8"));

        // Emulate old limiting behaviour and metadata
        final LimitBy limitBy;
        int maxResults = -1;
        if (searchParameters.getMaxItems() >= 0) {
            maxResults = searchParameters.getMaxItems();
            limitBy = LimitBy.FINAL_SIZE;
        } else if (searchParameters.getLimitBy() == LimitBy.FINAL_SIZE && searchParameters.getLimit() >= 0) {
            maxResults = searchParameters.getLimit();
            limitBy = LimitBy.FINAL_SIZE;
        } else {
            maxResults = searchParameters.getMaxPermissionChecks();
            if (maxResults < 0) {
                maxResults = maximumResultsFromUnlimitedQuery;
            }
            limitBy = LimitBy.NUMBER_OF_PERMISSION_EVALUATIONS;
        }
        url.append("&rows=").append(String.valueOf(maxResults));

        // url.append("&df=").append(encoder.encode(searchParameters.getDefaultFieldName(), "UTF-8"));
        url.append("&start=").append(encoder.encode("" + searchParameters.getSkipCount(), "UTF-8"));

        // Locale locale = I18NUtil.getLocale();
        // if (searchParameters.getLocales().size() > 0)
        // {
        // locale = searchParameters.getLocales().get(0);
        // }
        // url.append("&locale=");
        // url.append(encoder.encode(locale.toString(), "UTF-8"));

        StringBuffer sortBuffer = new StringBuffer();
        for (SortDefinition sortDefinition : searchParameters.getSortDefinitions()) {
            if (sortBuffer.length() == 0) {
                sortBuffer.append("&sort=");
            } else {
                sortBuffer.append(encoder.encode(", ", "UTF-8"));
            }
            sortBuffer.append(encoder.encode(sortDefinition.getField(), "UTF-8"))
                    .append(encoder.encode(" ", "UTF-8"));
            if (sortDefinition.isAscending()) {
                sortBuffer.append(encoder.encode("asc", "UTF-8"));
            } else {
                sortBuffer.append(encoder.encode("desc", "UTF-8"));
            }

        }
        url.append(sortBuffer);

        if (searchParameters.getFieldFacets().size() > 0) {
            for (FieldFacet facet : searchParameters.getFieldFacets()) {
                url.append("&facet=").append(encoder.encode(facet.getField(), "UTF-8"));
            }
        }
        // end of field factes

        // add current username doing the request for permissions
        url.append("&userName=").append(encoder.encode(AuthenticationUtil.getRunAsUser(), "UTF-8"));

        GetMethod get = new GetMethod(url.toString());

        try {
            httpClient.executeMethod(get);

            if (get.getStatusCode() == HttpStatus.SC_MOVED_PERMANENTLY
                    || get.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) {
                Header locationHeader = get.getResponseHeader("location");
                if (locationHeader != null) {
                    String redirectLocation = locationHeader.getValue();
                    get.setURI(new URI(redirectLocation, true));
                    httpClient.executeMethod(get);
                }
            }

            if (get.getStatusCode() != HttpServletResponse.SC_OK) {
                throw new LuceneQueryParserException(
                        "Request failed " + get.getStatusCode() + " " + url.toString());
            }

            Reader reader = new BufferedReader(new InputStreamReader(get.getResponseBodyAsStream()));
            // TODO - replace with streaming-based solution e.g. SimpleJSON ContentHandler
            JSONObject json = new JSONObject(new JSONTokener(reader));
            SensefySolrJSONResultSet results = new SensefySolrJSONResultSet(json, searchParameters, nodeService,
                    nodeDAO, limitBy, maxResults);
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Sent :" + url);
                s_logger.debug("Got: " + results.getNumberFound() + " in " + results.getQueryTime() + " ms");
            }

            return results;
        } finally {
            get.releaseConnection();
        }
    } catch (UnsupportedEncodingException e) {
        throw new LuceneQueryParserException("", e);
    } catch (HttpException e) {
        throw new LuceneQueryParserException("", e);
    } catch (IOException e) {
        throw new LuceneQueryParserException("", e);
    } catch (JSONException e) {
        throw new LuceneQueryParserException("", e);
    }
}