Example usage for javax.servlet.http HttpServletResponse SC_MULTIPLE_CHOICES

List of usage examples for javax.servlet.http HttpServletResponse SC_MULTIPLE_CHOICES

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse SC_MULTIPLE_CHOICES.

Prototype

int SC_MULTIPLE_CHOICES

To view the source code for javax.servlet.http HttpServletResponse SC_MULTIPLE_CHOICES.

Click Source Link

Document

Status code (300) indicating that the requested resource corresponds to any one of a set of representations, each with its own specific location.

Usage

From source file:de.escidoc.core.test.common.client.servlet.oai.OaiProviderClient.java

/**
 * Retrieve the Containers of a Container.
 *
 * @param parameters The get-parameters.
 * @return The response as String./*from w w w.  j  a  va  2  s  .  c om*/
 * @throws Exception If the service call fails.
 */
public String retrieve(final String parameters) throws Exception {

    Map<String, String[]> parametersMap = new HashMap<String, String[]>();
    if (parameters != null && !parameters.isEmpty()) {
        String[] parametersParts = parameters.split("&");
        for (int i = 0; i < parametersParts.length; i++) {
            if (parametersParts[i] != null && !parametersParts[i].isEmpty()) {
                String[] parameterParts = parametersParts[i].split("=");
                if (parameterParts != null && parameterParts.length == 2) {
                    if (!parametersMap.containsKey(parameterParts[0])) {
                        parametersMap.put(parameterParts[0], new String[0]);
                    }
                    String[] values = parametersMap.get(parameterParts[0]);
                    String[] newValues = new String[values.length + 1];
                    int j = 0;
                    for (; j < values.length; j++) {
                        newValues[j] = values[j];
                    }
                    newValues[j] = parameterParts[1];
                    parametersMap.put(parameterParts[0], newValues);
                }
            }
        }
    }

    String url = HttpHelper.createUrl(Constants.HTTP_PROTOCOL,
            EscidocTestBase.getBaseHost() + ":" + EscidocTestBase.getBasePort(),
            Constants.WEB_CONTEXT_URI_OAI_PMH, new String[0], null, false);
    HttpResponse response = HttpHelper.executeHttpRequest(getHttpClient(), Constants.HTTP_METHOD_GET, url, null,
            null, parametersMap);
    if (((HttpResponse) response).getStatusLine().getStatusCode() >= HttpServletResponse.SC_MULTIPLE_CHOICES) {
        throwCorrespondingException((HttpResponse) response);
    }
    return handleXmlResult(response);

}

From source file:com.thoughtworks.go.agent.UrlBasedArtifactsRepository.java

@Override
public void upload(StreamConsumer console, File file, String destPath, String buildId) {
    if (!file.exists()) {
        String message = "Failed to find " + file.getAbsolutePath();
        consumeLineWithPrefix(console, message);
        throw bomb(message);
    }//from ww  w .  j a  va 2s  . c om

    int publishingAttempts = 0;
    Throwable lastException = null;
    while (publishingAttempts < PUBLISH_MAX_RETRIES) {
        File tmpDir = null;
        try {
            publishingAttempts++;

            tmpDir = FileUtil.createTempFolder();
            File dataToUpload = new File(tmpDir, file.getName() + ".zip");
            zipUtil.zip(file, dataToUpload, Deflater.BEST_SPEED);

            long size;
            if (file.isDirectory()) {
                size = FileUtils.sizeOfDirectory(file);
            } else {
                size = file.length();
            }

            consumeLineWithPrefix(console,
                    format("Uploading artifacts from %s to %s", file.getAbsolutePath(), getDestPath(destPath)));

            String normalizedDestPath = normalizePath(destPath);
            String url = getUploadUrl(buildId, normalizedDestPath, publishingAttempts);

            int statusCode = httpService.upload(url, size, dataToUpload,
                    artifactChecksums(file, normalizedDestPath));

            if (statusCode == HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE) {
                String message = format(
                        "Artifact upload for file %s (Size: %s) was denied by the server. This usually happens when server runs out of disk space.",
                        file.getAbsolutePath(), size);
                consumeLineWithPrefix(console, message);
                LOGGER.error(
                        "[Artifact Upload] Artifact upload was denied by the server. This usually happens when server runs out of disk space.");
                publishingAttempts = PUBLISH_MAX_RETRIES;
                throw bomb(message + ".  HTTP return code is " + statusCode);
            }
            if (statusCode < HttpServletResponse.SC_OK
                    || statusCode >= HttpServletResponse.SC_MULTIPLE_CHOICES) {
                throw bomb(
                        "Failed to upload " + file.getAbsolutePath() + ".  HTTP return code is " + statusCode);
            }
            return;
        } catch (Throwable e) {
            String message = "Failed to upload " + file.getAbsolutePath();
            LOGGER.error(message, e);
            consumeLineWithPrefix(console, message);
            lastException = e;
        } finally {
            FileUtil.deleteFolder(tmpDir);
        }
    }
    if (lastException != null) {
        throw new RuntimeException(lastException);
    }
}

From source file:com.thoughtworks.go.publishers.GoArtifactsManipulator.java

public void publish(DefaultGoPublisher goPublisher, String destPath, File source, JobIdentifier jobIdentifier) {
    if (!source.exists()) {
        String message = "Failed to find " + source.getAbsolutePath();
        goPublisher.taggedConsumeLineWithPrefix(PUBLISH_ERR, message);
        bomb(message);/*w w w  .  ja  v  a2s  . c  o m*/
    }

    int publishingAttempts = 0;
    Throwable lastException = null;
    while (publishingAttempts < PUBLISH_MAX_RETRIES) {
        File tmpDir = null;
        try {
            publishingAttempts++;

            tmpDir = FileUtil.createTempFolder();
            File dataToUpload = new File(tmpDir, source.getName() + ".zip");
            zipUtil.zip(source, dataToUpload, Deflater.BEST_SPEED);

            long size = 0;
            if (source.isDirectory()) {
                size = FileUtils.sizeOfDirectory(source);
            } else {
                size = source.length();
            }

            goPublisher.taggedConsumeLineWithPrefix(PUBLISH,
                    "Uploading artifacts from " + source.getAbsolutePath() + " to " + getDestPath(destPath));

            String normalizedDestPath = FilenameUtils.separatorsToUnix(destPath);
            String url = urlService.getUploadUrlOfAgent(jobIdentifier, normalizedDestPath, publishingAttempts);

            int statusCode = httpService.upload(url, size, dataToUpload,
                    artifactChecksums(source, normalizedDestPath));

            if (statusCode == HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE) {
                String message = String.format(
                        "Artifact upload for file %s (Size: %s) was denied by the server. This usually happens when server runs out of disk space.",
                        source.getAbsolutePath(), size);
                goPublisher.taggedConsumeLineWithPrefix(PUBLISH_ERR, message);
                LOGGER.error(
                        "[Artifact Upload] Artifact upload was denied by the server. This usually happens when server runs out of disk space.");
                publishingAttempts = PUBLISH_MAX_RETRIES;
                bomb(message + ".  HTTP return code is " + statusCode);
            }
            if (statusCode < HttpServletResponse.SC_OK
                    || statusCode >= HttpServletResponse.SC_MULTIPLE_CHOICES) {
                bomb("Failed to upload " + source.getAbsolutePath() + ".  HTTP return code is " + statusCode);
            }
            return;
        } catch (Throwable e) {
            String message = "Failed to upload " + source.getAbsolutePath();
            LOGGER.error(message, e);
            goPublisher.taggedConsumeLineWithPrefix(PUBLISH_ERR, message);
            lastException = e;
        } finally {
            FileUtils.deleteQuietly(tmpDir);
        }
    }
    if (lastException != null) {
        throw new RuntimeException(lastException);
    }
}

From source file:com.codeabovelab.dm.gateway.proxy.common.HttpProxy.java

private boolean doResponseRedirectOrNotModifiedLogic(HttpProxyContext proxyContext, HttpResponse proxyResponse,
        int statusCode) throws ServletException, IOException {
    HttpServletResponse servletResponse = proxyContext.getResponse();
    // Check if the proxy response is a redirect
    // The following code is adapted from org.tigris.noodle.filters.CheckForRedirect
    if (statusCode >= HttpServletResponse.SC_MULTIPLE_CHOICES /* 300 */
            && statusCode < HttpServletResponse.SC_NOT_MODIFIED /* 304 */) {
        Header locationHeader = proxyResponse.getLastHeader(HttpHeaders.LOCATION);
        if (locationHeader == null) {
            throw new ServletException("Received status code: " + statusCode + " but no " + HttpHeaders.LOCATION
                    + " header was found in the response");
        }//  ww w . ja  va2 s  .  c om
        // Modify the redirect to go to this proxy servlet rather that the proxied host
        String locStr = rewriteUrlFromResponse(proxyContext, locationHeader.getValue());

        servletResponse.sendRedirect(locStr);
        return true;
    }
    // 304 needs special handling.  See:
    // http://www.ics.uci.edu/pub/ietf/http/rfc1945.html#Code304
    // We get a 304 whenever passed an 'If-Modified-Since'
    // header and the data on disk has not changed; server
    // responds w/ a 304 saying I'm not going to send the
    // body because the file has not changed.
    if (statusCode == HttpServletResponse.SC_NOT_MODIFIED) {
        servletResponse.setIntHeader(HttpHeaders.CONTENT_LENGTH, 0);
        servletResponse.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        return true;
    }
    return false;
}

From source file:org.realityforge.proxy_servlet.AbstractProxyServlet.java

private boolean doResponseRedirectOrNotModifiedLogic(final HttpServletRequest servletRequest,
        final HttpServletResponse servletResponse, final HttpResponse proxyResponse, final int statusCode)
        throws ServletException, IOException {
    // Check if the proxy response is a redirect
    // The following code is adapted from org.tigris.noodle.filters.CheckForRedirect
    if (statusCode >= HttpServletResponse.SC_MULTIPLE_CHOICES && /* 300 */
            statusCode < HttpServletResponse.SC_NOT_MODIFIED /* 304 */ ) {
        final Header locationHeader = proxyResponse.getLastHeader(HttpHeaders.LOCATION);
        if (null == locationHeader) {
            final String message = "Received status code: " + statusCode + " but no " + HttpHeaders.LOCATION
                    + " header was found in the response";
            throw new ServletException(message);
        }//from   w w  w .  j  ava2  s.c  om
        // Modify the redirect to go to this proxy servlet rather that the proxied host
        final String locStr = rewriteUrlFromResponse(servletRequest, locationHeader.getValue());

        servletResponse.sendRedirect(locStr);
        return true;
    }

    // 304 needs special handling.  See:
    // http://www.ics.uci.edu/pub/ietf/http/rfc1945.html#Code304
    // We get a 304 whenever passed an 'If-Modified-Since'
    // header and the data on disk has not changed; server
    // responds w/ a 304 saying I'm not going to send the
    // body because the file has not changed.
    if (statusCode == HttpServletResponse.SC_NOT_MODIFIED) {
        servletResponse.setIntHeader(HttpHeaders.CONTENT_LENGTH, 0);
        servletResponse.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        return true;
    }
    return false;
}

From source file:org.kmnet.com.fw.web.exception.HandlerExceptionResolverLoggingInterceptor.java

/**
 * Outputs the log/*  ww  w . j ava 2  s . c o m*/
 * @param ex Exception
 * @param request HTTP Servlet Request
 * @param response HTTP Servlet Response
 * @param handler handler
 */
protected void log(Exception ex, HttpServletRequest request, HttpServletResponse response, Object handler) {
    int statusCode = response.getStatus();
    if (HttpServletResponse.SC_INTERNAL_SERVER_ERROR <= statusCode) {
        // responseCode is 500 ~
        logServerError(ex, request, response, handler);
        return;
    }
    if (HttpServletResponse.SC_BAD_REQUEST <= statusCode) {
        // 400 ~ 499
        logClientError(ex, request, response, handler);
        return;
    }
    if (HttpServletResponse.SC_MULTIPLE_CHOICES <= statusCode) {
        // 300 ~ 399
        logRedirection(ex, request, response, handler);
        return;
    }
    if (HttpServletResponse.SC_OK <= statusCode) {
        // 200 ~ 299
        logSuccess(ex, request, response, handler);
        return;
    }
    if (HttpServletResponse.SC_CONTINUE <= statusCode) {
        // 100 ~ 199
        logInformational(ex, request, response, handler);
        return;
    }

}

From source file:be.milieuinfo.core.proxy.controller.ProxyServlet.java

private boolean doResponseRedirectOrNotModifiedLogic(HttpServletRequest servletRequest,
        HttpServletResponse servletResponse, HttpResponse proxyResponse, int statusCode)
        throws ServletException, IOException {
    // Check if the proxy response is a redirect
    // The following code is adapted from org.tigris.noodle.filters.CheckForRedirect
    if (statusCode >= HttpServletResponse.SC_MULTIPLE_CHOICES /* 300 */
            && statusCode < HttpServletResponse.SC_NOT_MODIFIED /* 304 */) {
        Header locationHeader = proxyResponse.getLastHeader(HttpHeaders.LOCATION);
        if (locationHeader == null) {
            throw new ServletException("Received status code: " + statusCode + " but no " + HttpHeaders.LOCATION
                    + " header was found in the response");
        }/* w w  w  .  j  av a2s  .  c om*/
        // Modify the redirect to go to this proxy servlet rather that the proxied host
        String locStr = rewriteUrlFromResponse(servletRequest, locationHeader.getValue());

        servletResponse.sendRedirect(locStr);
        return true;
    }
    // 304 needs special handling.  See:
    // http://www.ics.uci.edu/pub/ietf/http/rfc1945.html#Code304
    // We get a 304 whenever passed an 'If-Modified-Since'
    // header and the data on disk has not changed; server
    // responds w/ a 304 saying I'm not going to send the
    // body because the file has not changed.
    if (statusCode == HttpServletResponse.SC_NOT_MODIFIED) {
        servletResponse.setIntHeader(HttpHeaders.CONTENT_LENGTH, 0);
        servletResponse.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        return true;
    }
    return false;
}

From source file:uk.ac.ebi.phenotype.web.proxy.ExternalUrlConfiguratbleProxyServlet.java

private boolean doResponseRedirectOrNotModifiedLogic(HttpServletRequest servletRequest,
        HttpServletResponse servletResponse, HttpResponse proxyResponse, int statusCode)
        throws ServletException, IOException {
    // Check if the proxy response is a redirect
    // The following code is adapted from
    // org.tigris.noodle.filters.CheckForRedirect
    if (statusCode >= HttpServletResponse.SC_MULTIPLE_CHOICES /* 300 */
            && statusCode < HttpServletResponse.SC_NOT_MODIFIED /* 304 */) {
        Header locationHeader = proxyResponse.getLastHeader(HttpHeaders.LOCATION);
        if (locationHeader == null) {
            throw new ServletException("Recieved status code: " + statusCode + " but no " + HttpHeaders.LOCATION
                    + " header was found in the response");
        }//from   ww  w  .  j  a v a  2s.  c  om
        // Modify the redirect to go to this proxy servlet rather that the
        // proxied host
        String locStr = rewriteUrlFromResponse(servletRequest, locationHeader.getValue());

        servletResponse.sendRedirect(locStr);
        return true;
    }
    // 304 needs special handling. See:
    // http://www.ics.uci.edu/pub/ietf/http/rfc1945.html#Code304
    // We get a 304 whenever passed an 'If-Modified-Since'
    // header and the data on disk has not changed; server
    // responds w/ a 304 saying I'm not going to send the
    // body because the file has not changed.
    if (statusCode == HttpServletResponse.SC_NOT_MODIFIED) {
        servletResponse.setIntHeader(HttpHeaders.CONTENT_LENGTH, 0);
        servletResponse.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        return true;
    }
    return false;
}

From source file:org.ocpsoft.rewrite.servlet.config.proxy.ProxyServlet.java

protected boolean doResponseRedirectOrNotModifiedLogic(HttpServletRequest servletRequest,
        HttpServletResponse servletResponse, HttpResponse proxyResponse, int statusCode)
        throws ServletException, IOException {
    /*//w w  w .j  a va2s.com
     * Check if the proxy response is a redirect. The following code is adapted from
     * org.tigris.noodle.filters.CheckForRedirect
     */
    if (statusCode >= HttpServletResponse.SC_MULTIPLE_CHOICES /* 300 */
            && statusCode < HttpServletResponse.SC_NOT_MODIFIED /* 304 */) {
        Header locationHeader = proxyResponse.getLastHeader(HttpHeaders.LOCATION);
        if (locationHeader == null) {
            throw new ServletException("Received status code: " + statusCode + " but no " + HttpHeaders.LOCATION
                    + " header was found in the response");
        }
        /*
         * Modify the redirect to go to this proxy servlet rather that the proxied host
         */
        String locStr = rewriteUrlFromResponse(servletRequest, locationHeader.getValue());

        servletResponse.sendRedirect(locStr);
        return true;
    }
    /*
     * 304 needs special handling. See: http://www.ics.uci.edu/pub/ietf/http/rfc1945.html#Code304 . We get a 304
     * whenever passed an 'If-Modified-Since' header and the data on disk has not changed; server responds w/ a 304
     * saying I'm not going to send the body because the file has not changed.
     */
    if (statusCode == HttpServletResponse.SC_NOT_MODIFIED) {
        servletResponse.setIntHeader(HttpHeaders.CONTENT_LENGTH, 0);
        servletResponse.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        return true;
    }
    return false;
}

From source file:freeciv.servlet.ProxyServlet.java

/**
 * Executes the {@link HttpMethod} passed in and sends the proxy response
 * back to the client via the given {@link HttpServletResponse}
 * @param httpMethodProxyRequest An object representing the proxy request to be made
 * @param httpServletResponse An object by which we can send the proxied
 *                             response back to the client
 * @throws IOException Can be thrown by the {@link HttpClient}.executeMethod
 * @throws ServletException Can be thrown to indicate that another error has occurred
 *///from   ww w .  j a  v a  2s.  c  om
private void executeProxyRequest(HttpMethod httpMethodProxyRequest, HttpServletRequest httpServletRequest,
        HttpServletResponse httpServletResponse) throws IOException, ServletException {

    httpMethodProxyRequest.setFollowRedirects(false);
    String port = "" + httpServletRequest.getSession().getAttribute("civserverport");
    String host = "" + httpServletRequest.getSession().getAttribute("civserverhost");
    String username = "" + httpServletRequest.getSession().getAttribute("username");
    httpMethodProxyRequest.addRequestHeader("civserverport", port);
    httpMethodProxyRequest.addRequestHeader("civserverhost", host);
    httpMethodProxyRequest.addRequestHeader("username", username);
    int intProxyResponseCode = 0;
    // Execute the request
    try {
        intProxyResponseCode = client.executeMethod(httpMethodProxyRequest);
    } catch (IOException ioErr) {
        //- If an I/O (transport) error occurs. Some transport exceptions can be recovered from. 
        //- If a protocol exception occurs. Usually protocol exceptions cannot be recovered from.
        OutputStream outputStreamClientResponse = httpServletResponse.getOutputStream();
        httpServletResponse.setStatus(502);
        outputStreamClientResponse
                .write("Freeciv web client proxy not responding (most likely died).".getBytes());
        httpMethodProxyRequest.releaseConnection();
        return;
    }

    // Check if the proxy response is a redirect
    // The following code is adapted from org.tigris.noodle.filters.CheckForRedirect
    // Hooray for open source software
    if (intProxyResponseCode >= HttpServletResponse.SC_MULTIPLE_CHOICES /* 300 */
            && intProxyResponseCode < HttpServletResponse.SC_NOT_MODIFIED /* 304 */) {
        String stringStatusCode = Integer.toString(intProxyResponseCode);
        String stringLocation = httpMethodProxyRequest.getResponseHeader(STRING_LOCATION_HEADER).getValue();
        if (stringLocation == null) {
            httpMethodProxyRequest.releaseConnection();
            throw new ServletException("Recieved status code: " + stringStatusCode + " but no "
                    + STRING_LOCATION_HEADER + " header was found in the response");
        }
        // Modify the redirect to go to this proxy servlet rather that the proxied host
        String stringMyHostName = httpServletRequest.getServerName();
        if (httpServletRequest.getServerPort() != 80) {
            stringMyHostName += ":" + httpServletRequest.getServerPort();
        }
        stringMyHostName += httpServletRequest.getContextPath();
        httpServletResponse.sendRedirect(
                stringLocation.replace(getProxyHostAndPort() + this.getProxyPath(), stringMyHostName));
        httpMethodProxyRequest.releaseConnection();
        return;
    } else if (intProxyResponseCode == HttpServletResponse.SC_NOT_MODIFIED) {
        // 304 needs special handling.  See:
        // http://www.ics.uci.edu/pub/ietf/http/rfc1945.html#Code304
        // We get a 304 whenever passed an 'If-Modified-Since'
        // header and the data on disk has not changed; server
        // responds w/ a 304 saying I'm not going to send the
        // body because the file has not changed.
        httpServletResponse.setIntHeader(STRING_CONTENT_LENGTH_HEADER_NAME, 0);
        httpServletResponse.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        httpMethodProxyRequest.releaseConnection();
        return;
    }

    // Pass the response code back to the client
    httpServletResponse.setStatus(intProxyResponseCode);

    // Pass response headers back to the client
    Header[] headerArrayResponse = httpMethodProxyRequest.getResponseHeaders();
    for (Header header : headerArrayResponse) {
        httpServletResponse.setHeader(header.getName(), header.getValue());
    }

    // Send the content to the client
    InputStream inputStreamProxyResponse = httpMethodProxyRequest.getResponseBodyAsStream();
    BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStreamProxyResponse);
    OutputStream outputStreamClientResponse = httpServletResponse.getOutputStream();
    int intNextByte;
    while ((intNextByte = bufferedInputStream.read()) != -1) {
        outputStreamClientResponse.write(intNextByte);
    }
    httpMethodProxyRequest.releaseConnection();
}