Java Code Examples for org.apache.http.HttpStatus#SC_MOVED_PERMANENTLY

The following examples show how to use org.apache.http.HttpStatus#SC_MOVED_PERMANENTLY . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpClientRequestExecutorTest.java    From esigate with Apache License 2.0 6 votes vote down vote up
/**
 * Test that we don't have a NullpointerException when forcing the caching (ttl).
 * 
 * @throws Exception
 */
public void testForcedTtlWith301ResponseCode() throws Exception {
    properties = new PropertiesBuilder() //
            .set(Parameters.REMOTE_URL_BASE, "http://localhost:8080") //
            .set(Parameters.TTL, 1000) // Default value
            .build();

    createHttpClientRequestExecutor();
    DriverRequest originalRequest = TestUtils.createDriverRequest(driver);
    OutgoingRequest request =
            httpClientRequestExecutor.createOutgoingRequest(originalRequest, "http://localhost:8080", true);
    HttpResponse response =
            new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1),
                    HttpStatus.SC_MOVED_PERMANENTLY, "Moved permanently"));
    response.addHeader("Location", "http://www.foo.com");
    mockConnectionManager.setResponse(response);
    HttpResponse result = httpClientRequestExecutor.execute(request);
    if (result.getEntity() != null) {
        result.getEntity().writeTo(new NullOutputStream());
        // We should have had a NullpointerException
    }
}
 
Example 2
Source File: SimpleRedirectStrategy.java    From gerbil with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public boolean isRedirected(final HttpRequest request, final HttpResponse response, final HttpContext context)
        throws ProtocolException {
    Args.notNull(request, "HTTP request");
    Args.notNull(response, "HTTP response");

    final int statusCode = response.getStatusLine().getStatusCode();
    final String method = request.getRequestLine().getMethod();
    final Header locationHeader = response.getFirstHeader("location");
    switch (statusCode) {
    case HttpStatus.SC_MOVED_TEMPORARILY:
        return isRedirectable(method) && locationHeader != null;
    case HttpStatus.SC_MOVED_PERMANENTLY:
    case HttpStatus.SC_TEMPORARY_REDIRECT:
    case 308: // Ensure that HTTP 308 is redirected as well
        return isRedirectable(method);
    case HttpStatus.SC_SEE_OTHER:
        return true;
    default:
        return false;
    } // end of switch
}
 
Example 3
Source File: MyRedirectHandler.java    From Android-Basics-Codes with Artistic License 2.0 6 votes vote down vote up
@Override
public boolean isRedirectRequested(
        final HttpResponse response,
        final HttpContext context) {
    if (!enableRedirects) {
        return false;
    }
    if (response == null) {
        throw new IllegalArgumentException("HTTP response may not be null");
    }
    int statusCode = response.getStatusLine().getStatusCode();
    switch (statusCode) {
        case HttpStatus.SC_MOVED_TEMPORARILY:
        case HttpStatus.SC_MOVED_PERMANENTLY:
        case HttpStatus.SC_SEE_OTHER:
        case HttpStatus.SC_TEMPORARY_REDIRECT:
            return true;
        default:
            return false;
    } //end of switch
}
 
Example 4
Source File: MyRedirectHandler.java    From Android-Basics-Codes with Artistic License 2.0 6 votes vote down vote up
@Override
public boolean isRedirectRequested(
        final HttpResponse response,
        final HttpContext context) {
    if (!enableRedirects) {
        return false;
    }
    if (response == null) {
        throw new IllegalArgumentException("HTTP response may not be null");
    }
    int statusCode = response.getStatusLine().getStatusCode();
    switch (statusCode) {
        case HttpStatus.SC_MOVED_TEMPORARILY:
        case HttpStatus.SC_MOVED_PERMANENTLY:
        case HttpStatus.SC_SEE_OTHER:
        case HttpStatus.SC_TEMPORARY_REDIRECT:
            return true;
        default:
            return false;
    } //end of switch
}
 
Example 5
Source File: MyRedirectHandler.java    From Android-Basics-Codes with Artistic License 2.0 6 votes vote down vote up
@Override
public boolean isRedirectRequested(
        final HttpResponse response,
        final HttpContext context) {
    if (!enableRedirects) {
        return false;
    }
    if (response == null) {
        throw new IllegalArgumentException("HTTP response may not be null");
    }
    int statusCode = response.getStatusLine().getStatusCode();
    switch (statusCode) {
        case HttpStatus.SC_MOVED_TEMPORARILY:
        case HttpStatus.SC_MOVED_PERMANENTLY:
        case HttpStatus.SC_SEE_OTHER:
        case HttpStatus.SC_TEMPORARY_REDIRECT:
            return true;
        default:
            return false;
    } //end of switch
}
 
Example 6
Source File: MartiDiceRoller.java    From triplea with GNU General Public License v3.0 6 votes vote down vote up
@Override
public HttpUriRequest getRedirect(
    final HttpRequest request, final HttpResponse response, final HttpContext context)
    throws ProtocolException {
  final URI uri = getLocationURI(request, response, context);
  final String method = request.getRequestLine().getMethod();
  if (method.equalsIgnoreCase(HttpHead.METHOD_NAME)) {
    return new HttpHead(uri);
  } else if (method.equalsIgnoreCase(HttpGet.METHOD_NAME)) {
    return new HttpGet(uri);
  } else {
    final int status = response.getStatusLine().getStatusCode();
    if (status == HttpStatus.SC_TEMPORARY_REDIRECT
        || status == HttpStatus.SC_MOVED_PERMANENTLY
        || status == HttpStatus.SC_MOVED_TEMPORARILY) {
      return RequestBuilder.copy(request).setUri(uri).build();
    }
    return new HttpGet(uri);
  }
}
 
Example 7
Source File: MyRedirectHandler.java    From android-project-wo2b with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isRedirectRequested(
        final HttpResponse response,
        final HttpContext context) {
    if (!enableRedirects) {
        return false;
    }
    if (response == null) {
        throw new IllegalArgumentException("HTTP response may not be null");
    }
    int statusCode = response.getStatusLine().getStatusCode();
    switch (statusCode) {
        case HttpStatus.SC_MOVED_TEMPORARILY:
        case HttpStatus.SC_MOVED_PERMANENTLY:
        case HttpStatus.SC_SEE_OTHER:
        case HttpStatus.SC_TEMPORARY_REDIRECT:
            return true;
        default:
            return false;
    } //end of switch
}
 
Example 8
Source File: HttpClientTests.java    From vividus with Apache License 2.0 6 votes vote down vote up
@Test
void testDoHttpHead() throws Exception
{
    CloseableHttpResponse closeableHttpResponse = mock(CloseableHttpResponse.class);
    HttpContext context = null;
    when(closeableHttpResponse.getEntity()).thenReturn(null);
    StatusLine statusLine = mock(StatusLine.class);
    int statusCode = HttpStatus.SC_MOVED_PERMANENTLY;
    Header[] headers = { header };
    when(closeableHttpResponse.getAllHeaders()).thenReturn(headers);
    when(statusLine.getStatusCode()).thenReturn(statusCode);
    when(closeableHttpResponse.getStatusLine()).thenReturn(statusLine);
    when(closeableHttpClient.execute(isA(HttpHead.class), eq(context)))
            .thenAnswer(getAnswerWithSleep(closeableHttpResponse));
    HttpResponse httpResponse = httpClient.doHttpHead(URI_TO_GO);
    assertEquals(HEAD, httpResponse.getMethod());
    assertEquals(URI_TO_GO, httpResponse.getFrom());
    assertEquals(statusCode, httpResponse.getStatusCode());
    assertThat(httpResponse.getResponseTimeInMs(), greaterThan(0L));
    assertThat(httpResponse.getResponseHeaders(), is(equalTo(headers)));
}
 
Example 9
Source File: MockUrlLengthenerFetcher.java    From flink-crawler with Apache License 2.0 6 votes vote down vote up
@Override
public FetchedResult get(String originalUrl, Payload payload) throws BaseFetchException {

    final int responseRate = 1000;
    final String mimeType = "text/plain";

    Headers headers = new Headers();
    int statusCode = HttpStatus.SC_MOVED_PERMANENTLY;
    String redirectedUrl = _redirections.get(originalUrl);
    if (redirectedUrl == null) {
        redirectedUrl = originalUrl;
        statusCode = HttpStatus.SC_NOT_FOUND;
    } else {
        headers.add(Headers.LOCATION, redirectedUrl);
    }

    // With max redirects set to 0, we don't get the redirected URL in the "actually fetched"
    // field of the FetchedResult (it's in the headers Location:xxx entry).
    FetchedResult result = new FetchedResult(originalUrl, originalUrl, 0, headers, new byte[0],
            mimeType, responseRate, payload, originalUrl, 0, "192.168.1.1", statusCode, null);
    return result;
}
 
Example 10
Source File: DcResponse.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * Engineとして許容しないレスポンスコードかどうかを判定する.
 * @param oStatus レスポンスコード(Number型)
 * @return true:Engineとして許容しないレスポンスコードである false:Engineとして許容できるレスポンスコードである
 */
private static boolean isInvalidResCode(Number oStatus) {
    // 以下のレスポンスコードは許容しない
    // ・3桁ではない
    // ・0番台
    // ・100番台(クライアントの挙動が不安定になるため)
    if (!String.valueOf(Context.toString(oStatus)).matches("^[2-9]\\d{2}$")) {
        return true;
    }
    // 301、303、307はサーブレットコンテナでエラーになるため許容しない
    if (oStatus.intValue() == HttpStatus.SC_MOVED_PERMANENTLY
            || oStatus.intValue() == HttpStatus.SC_SEE_OTHER
            || oStatus.intValue() == HttpStatus.SC_TEMPORARY_REDIRECT) {
        return true;
    }
    return false;
}
 
Example 11
Source File: HttpClientTests.java    From vividus with Apache License 2.0 6 votes vote down vote up
@Test
void testDoHttpHeadContext() throws Exception
{
    CloseableHttpResponse closeableHttpResponse = mock(CloseableHttpResponse.class);
    HttpContext context = mock(HttpContext.class);
    when(closeableHttpResponse.getEntity()).thenReturn(null);
    StatusLine statusLine = mock(StatusLine.class);
    int statusCode = HttpStatus.SC_MOVED_PERMANENTLY;
    Header[] headers = { header };
    when(closeableHttpResponse.getAllHeaders()).thenReturn(headers);
    when(statusLine.getStatusCode()).thenReturn(statusCode);
    when(closeableHttpResponse.getStatusLine()).thenReturn(statusLine);
    when(closeableHttpClient.execute(isA(HttpHead.class), eq(context)))
            .thenAnswer(getAnswerWithSleep(closeableHttpResponse));
    HttpResponse httpResponse = httpClient.doHttpHead(URI_TO_GO, context);
    assertEquals(HEAD, httpResponse.getMethod());
    assertEquals(URI_TO_GO, httpResponse.getFrom());
    assertEquals(statusCode, httpResponse.getStatusCode());
    assertThat(httpResponse.getResponseTimeInMs(), greaterThan(0L));
    assertThat(httpResponse.getResponseHeaders(), is(equalTo(headers)));
}
 
Example 12
Source File: HttpClientTools.java    From lavaplayer with Apache License 2.0 5 votes vote down vote up
private static boolean isRedirectStatus(int statusCode) {
  switch (statusCode) {
    case HttpStatus.SC_MOVED_PERMANENTLY:
    case HttpStatus.SC_MOVED_TEMPORARILY:
    case HttpStatus.SC_SEE_OTHER:
    case HttpStatus.SC_TEMPORARY_REDIRECT:
      return true;
    default:
      return false;
  }
}
 
Example 13
Source File: MCRDNBURNRestClient.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Updates all URLS to a given URN.
 * <br><br>
 * 204 No Content: URN was updated successfully<br>
 * 301 Moved Permanently: URN has a newer version<br>
 * 303 See other: URL is registered for another URN<br>
 *
 * @return the status code of the request
 */

private Optional<Date> update(MCRPIRegistrationInfo urn) {
    MCREpicurLite elp = epicurProvider.apply(urn);
    String elpXML = elp.asXMLString();
    String updateURL = getUpdateURL(urn);
    CloseableHttpResponse response = MCRHttpsClient.post(updateURL, APPLICATION_XML.toString(), elpXML);
    StatusLine statusLine = response.getStatusLine();

    if (statusLine == null) {
        LOGGER.warn("POST request for {} returns no status line.", updateURL);
        return Optional.empty();
    }

    int postStatus = statusLine.getStatusCode();

    String identifier = urn.getIdentifier();
    switch (postStatus) {
        case HttpStatus.SC_NO_CONTENT:
            LOGGER.info("URN {} updated to {}.", identifier, elp.getUrl());
            return Optional.ofNullable(response.getFirstHeader("Last-Modified"))
                .map(Header::getValue)
                .map(DateTimeFormatter.RFC_1123_DATE_TIME::parse)
                .map(Instant::from)
                .map(Date::from);
        case HttpStatus.SC_MOVED_PERMANENTLY:
            LOGGER.warn("URN {} has a newer version.", identifier);
            break;
        case HttpStatus.SC_SEE_OTHER:
            LOGGER.warn("URL {} is registered for another URN.", elp.getUrl());
            break;
        default:
            LOGGER.warn("URN {} could not be updated. Status {}.", identifier, postStatus);
            break;
    }

    return Optional.empty();
}
 
Example 14
Source File: MCRDNBURNRestClient.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Please see list of status codes and their meaning:
 * <br><br>
 * 204 No Content: URN is in database. No further information asked.<br>
 * 301 Moved Permanently: The given URN is replaced with a newer version.
 * This newer version should be used instead.<br>
 * 404 Not Found: The given URN is not registered in system.<br>
 * 410 Gone: The given URN is registered in system but marked inactive.<br>
 *
 * @return the status code of the request
 */
public Optional<Date> register(MCRPIRegistrationInfo urn) {
    String url = getBaseServiceURL(urn);
    CloseableHttpResponse response = MCRHttpsClient.head(url);

    StatusLine statusLine = response.getStatusLine();

    if (statusLine == null) {
        LOGGER.warn("HEAD request for {} returns no status line.", url);
        return Optional.empty();
    }

    int headStatus = statusLine.getStatusCode();

    String identifier = urn.getIdentifier();
    switch (headStatus) {
        case HttpStatus.SC_NO_CONTENT:
            LOGGER.info("URN {} is in database. No further information asked.", identifier);
            LOGGER.info("Performing update of url.");
            return update(urn);
        case HttpStatus.SC_NOT_FOUND:
            LOGGER.info("The given URN {} is not registered in system.", identifier);
            return registerNew(urn);
        case HttpStatus.SC_MOVED_PERMANENTLY:
            LOGGER.warn("The given URN {} is replaced with a newer version. \n "
                + "This newer version should be used instead.", identifier);
            break;
        case HttpStatus.SC_GONE:
            LOGGER.warn("The given URN {} is registered in system but marked inactive.", identifier);
            break;
        default:
            LOGGER.warn("Could not handle request for urnInfo {} Status code {}.", identifier, headStatus);
            break;
    }

    return Optional.empty();
}
 
Example 15
Source File: WebProxyCall.java    From aliyun-cupid-sdk with Apache License 2.0 5 votes vote down vote up
public void callWebProxy(String url) {
    String resultCode = "";
    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {
        HttpGet httpget = new HttpGet(url);
        HttpResponse response = httpclient.execute(httpget);

        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode == HttpStatus.SC_OK) {
            HttpEntity entity = response.getEntity();
            resultCode = ResponseCode.CALLRESPONSEERROR;
            if (entity != null) {
                String responseString = EntityUtils.toString(entity);
                if (responseString.contains("Spark Jobs") && responseString.contains("Stages")
                        && responseString.contains("Storage") && responseString.contains("Environment")
                        && responseString.contains("Executors")) {
                    resultCode = ResponseCode.CALLSUCCESS;
                }
            }
        } else if (statusCode == HttpStatus.SC_MOVED_TEMPORARILY
                || statusCode == HttpStatus.SC_MOVED_PERMANENTLY) {
            resultCode = ResponseCode.CALLFORBIDDEN;
        } else {
            resultCode = ResponseCode.OTHER_RESPONSE + String.valueOf(statusCode);
        }
    } catch (Exception e) {
        LOG.warn("WebProxyCall exception " + e.getMessage());
        resultCode = ResponseCode.CALLEXCEPTION;
    } finally {
        httpclient.close();
    }
    LOG.info("WebProxyCall result " + resultCode);
    if (!resultCode.equals(ResponseCode.CALLSUCCESS)) {
        System.exit(1);
    }
}
 
Example 16
Source File: WebdavFileSystem.java    From xenon with Apache License 2.0 5 votes vote down vote up
@Override
public void rename(Path source, Path target) throws XenonException {

    LOGGER.debug("move source = {} to target = {}", source, target);

    Path absSource = toAbsolutePath(source);
    Path absTarget = toAbsolutePath(target);

    assertPathExists(absSource);

    if (areSamePaths(absSource, absTarget)) {
        return;
    }

    assertParentDirectoryExists(absTarget);
    assertPathNotExists(absTarget);

    PathAttributes a = getAttributes(absSource);

    try {
        if (a.isDirectory()) {
            client.move(getDirectoryPath(absSource), getDirectoryPath(absTarget), false);
        } else {
            client.move(getFilePath(absSource), getFilePath(absTarget), false);
        }
    } catch (SardineException e) {
        if (e.getStatusCode() == HttpStatus.SC_MOVED_PERMANENTLY) {
            return;
        }
        throw new XenonException(ADAPTOR_NAME, "Failed to move from " + absSource + " to " + absTarget, e);
    } catch (Exception e1) {
        throw new XenonException(ADAPTOR_NAME, "Failed to move from " + absSource + " to " + absTarget, e1);
    }
}
 
Example 17
Source File: SimpleUrlLengthener.java    From flink-crawler with Apache License 2.0 4 votes vote down vote up
@Override
public RawUrl lengthen(RawUrl url) {
    
    if (_lengthenedUrlMap.containsKey(url)) {
        return _lengthenedUrlMap.get(url);
    }

    // If the domain is a link shortener, lengthen it.

    String urlString = url.getUrl();
    
    Matcher m = HOSTNAME_PATTERN.matcher(urlString);
    if (!m.find()) {
        return url;
    }

    String hostname = m.group(1);
    if (!_urlShorteners.contains(hostname)) {
        // FUTURE - see if this looks like a shortened URL
        return url;
    }

    String redirectedUrl = urlString;
    LOGGER.trace("Checking redirection of '{}'", urlString);

    try {
        FetchedResult fr = _fetcher.get(urlString);
        int statusCode = fr.getStatusCode();
        if (statusCode == HttpStatus.SC_OK) {
            // This will happen if we're using a fetcher configured to
            // follow redirects (rather than one configured to immediately
            // return a 301). This isn't very nice, since we're fetching
            // content from the target site without checking its robot rules,
            // but the caller knows best.
            redirectedUrl = fr.getFetchedUrl();
            LOGGER.trace("Normal redirection of '{}' to '{}'", urlString, redirectedUrl);
        } else if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY) {
            redirectedUrl = extractRedirectUrl(fr, urlString);
            LOGGER.trace("Redirecting '{}' to '{}'", urlString, redirectedUrl);
        } else {
            LOGGER.trace("Status code {} processing redirect for '{}'", statusCode, urlString);
        }
    } catch (BaseFetchException e) {
        // The site doesn't seem to like the way we're forcing it to redirect,
        // so just emit the same URL for downstream fetching.
        LOGGER.error(String.format("Exception processing redirect for '{}': {}", urlString, e.getMessage()),
                e);
    }

    RawUrl result = new RawUrl(redirectedUrl, url.getScore());
    _lengthenedUrlMap.put(url, result);
    return result;
}
 
Example 18
Source File: ExceptionUtils.java    From flink-crawler with Apache License 2.0 4 votes vote down vote up
public static FetchStatus mapHttpStatusToFetchStatus(int httpStatus) {
    switch (httpStatus) {
        case HttpStatus.SC_OK:
            return FetchStatus.FETCHED;

        case HttpStatus.SC_FORBIDDEN:
            return FetchStatus.HTTP_FORBIDDEN;

        case HttpStatus.SC_UNAUTHORIZED:
        case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED:
            return FetchStatus.HTTP_UNAUTHORIZED;

        case HttpStatus.SC_NOT_FOUND:
            return FetchStatus.HTTP_NOT_FOUND;

        case HttpStatus.SC_GONE:
            return FetchStatus.HTTP_GONE;

        case HttpStatus.SC_TEMPORARY_REDIRECT:
        case HttpStatus.SC_MOVED_TEMPORARILY:
        case HttpStatus.SC_SEE_OTHER:
            return FetchStatus.HTTP_TOO_MANY_REDIRECTS;

        case HttpStatus.SC_MOVED_PERMANENTLY:
            return FetchStatus.HTTP_MOVED_PERMANENTLY;

        default:
            if (httpStatus < 300) {
                LOGGER.warn("Invalid HTTP status for exception: " + httpStatus);
                return FetchStatus.HTTP_SERVER_ERROR;
            } else if (httpStatus < 400) {
                return FetchStatus.HTTP_REDIRECTION_ERROR;
            } else if (httpStatus < 500) {
                return FetchStatus.HTTP_CLIENT_ERROR;
            } else if (httpStatus < 600) {
                return FetchStatus.HTTP_SERVER_ERROR;
            } else {
                LOGGER.warn("Unknown status: " + httpStatus);
                return FetchStatus.HTTP_SERVER_ERROR;
            }
    }
}
 
Example 19
Source File: HttpRequestHandler.java    From Asqatasun with GNU Affero General Public License v3.0 4 votes vote down vote up
private int computeStatus(int status) {
    switch (status) { 
        case HttpStatus.SC_FORBIDDEN:
        case HttpStatus.SC_METHOD_NOT_ALLOWED:
        case HttpStatus.SC_BAD_REQUEST:
        case HttpStatus.SC_UNAUTHORIZED:
        case HttpStatus.SC_PAYMENT_REQUIRED:
        case HttpStatus.SC_NOT_FOUND:
        case HttpStatus.SC_NOT_ACCEPTABLE:
        case HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED:
        case HttpStatus.SC_REQUEST_TIMEOUT:
        case HttpStatus.SC_CONFLICT:
        case HttpStatus.SC_GONE:
        case HttpStatus.SC_LENGTH_REQUIRED:
        case HttpStatus.SC_PRECONDITION_FAILED:
        case HttpStatus.SC_REQUEST_TOO_LONG:
        case HttpStatus.SC_REQUEST_URI_TOO_LONG:
        case HttpStatus.SC_UNSUPPORTED_MEDIA_TYPE:
        case HttpStatus.SC_REQUESTED_RANGE_NOT_SATISFIABLE:
        case HttpStatus.SC_EXPECTATION_FAILED:
        case HttpStatus.SC_INSUFFICIENT_SPACE_ON_RESOURCE:
        case HttpStatus.SC_METHOD_FAILURE:
        case HttpStatus.SC_UNPROCESSABLE_ENTITY:
        case HttpStatus.SC_LOCKED:
        case HttpStatus.SC_FAILED_DEPENDENCY:
        case HttpStatus.SC_INTERNAL_SERVER_ERROR:
        case HttpStatus.SC_NOT_IMPLEMENTED:
        case HttpStatus.SC_BAD_GATEWAY:
        case HttpStatus.SC_SERVICE_UNAVAILABLE:
        case HttpStatus.SC_GATEWAY_TIMEOUT:
        case HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED:
        case HttpStatus.SC_INSUFFICIENT_STORAGE:
            return 0;
        case HttpStatus.SC_CONTINUE:
        case HttpStatus.SC_SWITCHING_PROTOCOLS:
        case HttpStatus.SC_PROCESSING:
        case HttpStatus.SC_OK:
        case HttpStatus.SC_CREATED:
        case HttpStatus.SC_ACCEPTED:
        case HttpStatus.SC_NON_AUTHORITATIVE_INFORMATION:
        case HttpStatus.SC_NO_CONTENT:
        case HttpStatus.SC_RESET_CONTENT:
        case HttpStatus.SC_PARTIAL_CONTENT:
        case HttpStatus.SC_MULTI_STATUS:
        case HttpStatus.SC_MULTIPLE_CHOICES:
        case HttpStatus.SC_MOVED_PERMANENTLY:
        case HttpStatus.SC_MOVED_TEMPORARILY:
        case HttpStatus.SC_SEE_OTHER:
        case HttpStatus.SC_NOT_MODIFIED:
        case HttpStatus.SC_USE_PROXY:
        case HttpStatus.SC_TEMPORARY_REDIRECT:
            return 1;
        default : 
            return 1;
    }
}
 
Example 20
Source File: ResponseCachingPolicy.java    From apigee-android-sdk with Apache License 2.0 4 votes vote down vote up
/**
 * Determines if an HttpResponse can be cached.
 * 
 * @param httpMethod
 *            What type of request was this, a GET, PUT, other?
 * @param response
 *            The origin response
 * @return <code>true</code> if response is cacheable
 */
public boolean isResponseCacheable(String httpMethod, HttpResponse response) {
	boolean cacheable = false;

	if (!HeaderConstants.GET_METHOD.equals(httpMethod)) {
		log.debug("Response was not cacheable.");
		return false;
	}

	switch (response.getStatusLine().getStatusCode()) {
	case HttpStatus.SC_OK:
	case HttpStatus.SC_NON_AUTHORITATIVE_INFORMATION:
	case HttpStatus.SC_MULTIPLE_CHOICES:
	case HttpStatus.SC_MOVED_PERMANENTLY:
	case HttpStatus.SC_GONE:
		// these response codes MAY be cached
		cacheable = true;
		log.debug("Response was cacheable");
		break;
	case HttpStatus.SC_PARTIAL_CONTENT:
		// we don't implement Range requests and hence are not
		// allowed to cache partial content
		log.debug("Response was not cacheable (Partial Content)");
		return cacheable;

	default:
		// If the status code is not one of the recognized
		// available codes in HttpStatus Don't Cache
		log.debug("Response was not cacheable (Unknown Status code)");
		return cacheable;
	}

	Header contentLength = response.getFirstHeader(HTTP.CONTENT_LEN);
	if (contentLength != null) {
		int contentLengthValue = Integer.parseInt(contentLength.getValue());
		if (contentLengthValue > this.maxObjectSizeBytes)
			return false;
	}

	Header[] ageHeaders = response.getHeaders(HeaderConstants.AGE);

	if (ageHeaders.length > 1)
		return false;

	Header[] expiresHeaders = response.getHeaders(HeaderConstants.EXPIRES);

	if (expiresHeaders.length > 1)
		return false;

	Header[] dateHeaders = response.getHeaders(HTTP.DATE_HEADER);

	if (dateHeaders.length != 1)
		return false;

	try {
		DateUtils.parseDate(dateHeaders[0].getValue());
	} catch (DateParseException dpe) {
		return false;
	}

	for (Header varyHdr : response.getHeaders(HeaderConstants.VARY)) {
		for (HeaderElement elem : varyHdr.getElements()) {
			if ("*".equals(elem.getName())) {
				return false;
			}
		}
	}

	if (isExplicitlyNonCacheable(response))
		return false;

	return (cacheable || isExplicitlyCacheable(response));
}