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

The following examples show how to use org.apache.http.HttpStatus#SC_TEMPORARY_REDIRECT . 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: 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 2
Source File: MyRedirectHandler.java    From Mobike 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 3
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 4
Source File: MyRedirectHandler.java    From AndroidWear-OpenWear with MIT License 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-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 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: FragmentRedirectStrategy.java    From esigate with Apache License 2.0 6 votes vote down vote up
/**
 * @see DefaultRedirectStrategy#getRedirect(HttpRequest, HttpResponse, HttpContext)
 */
@Override
public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context)
        throws ProtocolException {

    // Code from DefaultRedirectStrategy
    // To ensure usage of the local getLocationURI implementation.

    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) {
            return RequestBuilder.copy(request).setUri(uri).build();
        } else {
            return new HttpGet(uri);
        }
    }
}
 
Example 8
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 9
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 10
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 11
Source File: InterceptorManager.java    From azure-libraries-for-java with MIT License 5 votes vote down vote up
private Response record(Interceptor.Chain chain) throws IOException {
    Request request = chain.request();
    NetworkCallRecord networkCallRecord = new NetworkCallRecord();

    networkCallRecord.Headers = new HashMap<>();

    if (request.header("Content-Type") != null) {
        networkCallRecord.Headers.put("Content-Type", request.header("Content-Type"));
    }
    if (request.header("x-ms-version") != null) {
        networkCallRecord.Headers.put("x-ms-version", request.header("x-ms-version"));
    }
    if (request.header("User-Agent") != null) {
        networkCallRecord.Headers.put("User-Agent", request.header("User-Agent"));
    }

    networkCallRecord.Method = request.method();
    networkCallRecord.Uri = applyReplacementRule(request.url().toString().replaceAll("\\?$", ""));

    Response response = chain.proceed(request);

    networkCallRecord.Response = new HashMap<>();
    networkCallRecord.Response.put("StatusCode", Integer.toString(response.code()));
    extractResponseData(networkCallRecord.Response, response);

    // remove pre-added header if this is a waiting or redirection
    if (networkCallRecord.Response.containsKey("Body") && networkCallRecord.Response.get("Body").contains("<Status>InProgress</Status>")
            || Integer.parseInt(networkCallRecord.Response.get("StatusCode")) == HttpStatus.SC_TEMPORARY_REDIRECT) {
        // Do nothing
    } else {
        synchronized (recordedData.getNetworkCallRecords()) {
            recordedData.getNetworkCallRecords().add(networkCallRecord);
        }
    }

    return response;
}
 
Example 12
Source File: GatewayBasicFuncTest.java    From knox with Apache License 2.0 5 votes vote down vote up
private String updateFileNN( String user, String password, String file, String resource, int status ) throws IOException {
  if( status == HttpStatus.SC_TEMPORARY_REDIRECT ) {
    driver.getMock( "WEBHDFS" )
        .expect()
        .method( "PUT" )
        .pathInfo( "/v1" + file )
        .queryParam( "op", "CREATE" )
        .queryParam( "user.name", user )
        .queryParam( "overwrite", "true" )
        .respond()
        .status( status )
        .header( "Location", driver.getRealUrl("DATANODE") + file + "?op=CREATE&user.name="+user );
  } else {
    driver.getMock( "WEBHDFS" )
        .expect()
        .method( "PUT" )
        .pathInfo( "v1" + file )
        .queryParam( "user.name", user )
        .queryParam( "op", "CREATE" )
        .respond()
        .status( status );
  }
  Response response = given()
      //.log().all()
      .auth().preemptive().basic( user, password )
      .header( "X-XSRF-Header", "jksdhfkhdsf" )
      .queryParam( "op", "CREATE" )
      .queryParam( "overwrite", "true" )
      .body( driver.getResourceBytes( resource ) )
      .then()
      //.log().all()
      .statusCode( status )
      .when().put( driver.getUrl("WEBHDFS") + "/v1" + file + ( driver.isUseGateway() ? "" : "?user.name=" + user ) );
  String location = response.getHeader( "Location" );
  log.trace( "Redirect location: " + response.getHeader( "Location" ) );
  return location;
}
 
Example 13
Source File: GatewayBasicFuncTest.java    From knox with Apache License 2.0 5 votes vote down vote up
private String createFileNN( String user, String password, String file, String permsOctal, int status ) throws IOException {
  if( status == HttpStatus.SC_TEMPORARY_REDIRECT ) {
    driver.getMock( "WEBHDFS" )
        .expect()
        .method( "PUT" )
        .pathInfo( "/v1" + file )
        .queryParam( "user.name", user )
        .queryParam( "op", "CREATE" )
        .respond()
        .status( status )
        .header( "Location", driver.getRealUrl("DATANODE") + file + "?op=CREATE&user.name="+user );
  } else {
    driver.getMock( "WEBHDFS" )
        .expect()
        .method( "PUT" )
        .pathInfo( "/v1" + file )
        .queryParam( "user.name", user )
        .queryParam( "op", "CREATE" )
        .respond()
        .status( status );
  }
  Response response = given()
      //.log().headers()
      //.log().parameters()
      .auth().preemptive().basic( user, password )
      .header( "X-XSRF-Header", "jksdhfkhdsf" )
      .queryParam( "op", "CREATE" )
      .queryParam( "permission", permsOctal )
      .then()
      //.log().all()
      .statusCode( status )
      .when().put( driver.getUrl( "WEBHDFS" ) + "/v1" + file + ( driver.isUseGateway() ? "" : "?user.name=" + user ) );
  String location = response.getHeader( "Location" );
  log.trace( "Redirect location: " + response.getHeader( "Location" ) );
  return location;
}
 
Example 14
Source File: CustomRedirectStrategy.java    From neembuu-uploader with GNU General Public License v3.0 5 votes vote down vote up
@Override
public HttpUriRequest getRedirect(
        final HttpRequest request,
        final HttpResponse response,
        final HttpContext context) throws ProtocolException {
    URI uri = null;
    try {
        uri = getLocationURI(request, response, context);
    } catch (URISyntaxException ex) {
        Logger.getLogger(CustomRedirectStrategy.class.getName()).log(Level.SEVERE, null, ex);
    }
    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 {
        int status = response.getStatusLine().getStatusCode();
        if (status == HttpStatus.SC_TEMPORARY_REDIRECT) {
            if (method.equalsIgnoreCase(HttpPost.METHOD_NAME)) {
                return copyEntity(new HttpPost(uri), request);
            } else if (method.equalsIgnoreCase(HttpPut.METHOD_NAME)) {
                return copyEntity(new HttpPut(uri), request);
            } else if (method.equalsIgnoreCase(HttpDelete.METHOD_NAME)) {
                return new HttpDelete(uri);
            } else if (method.equalsIgnoreCase(HttpTrace.METHOD_NAME)) {
                return new HttpTrace(uri);
            } else if (method.equalsIgnoreCase(HttpOptions.METHOD_NAME)) {
                return new HttpOptions(uri);
            } else if (method.equalsIgnoreCase(HttpPatch.METHOD_NAME)) {
                return copyEntity(new HttpPatch(uri), request);
            }
        }
        return new HttpGet(uri);
    }
}
 
Example 15
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 16
Source File: HttpClientAdapter.java    From davmail with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Check if status is a redirect (various 30x values).
 *
 * @param status Http status
 * @return true if status is a redirect
 */
public static boolean isRedirect(int status) {
    return status == HttpStatus.SC_MOVED_PERMANENTLY
            || status == HttpStatus.SC_MOVED_TEMPORARILY
            || status == HttpStatus.SC_SEE_OTHER
            || status == HttpStatus.SC_TEMPORARY_REDIRECT;
}
 
Example 17
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 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: AmazonHttpClient.java    From ibm-cos-sdk-java with Apache License 2.0 4 votes vote down vote up
private static boolean isTemporaryRedirect(org.apache.http.HttpResponse response) {
    int status = response.getStatusLine().getStatusCode();
    return status == HttpStatus.SC_TEMPORARY_REDIRECT && response.getHeaders("Location") != null
           && response.getHeaders("Location").length > 0;
}
 
Example 20
Source File: AlwaysRedirectStrategy.java    From caravan with Apache License 2.0 4 votes vote down vote up
protected boolean isRedirected(int statusCode) {
    return statusCode == HttpStatus.SC_TEMPORARY_REDIRECT;
}