本文整理匯總了Java中org.apache.http.ProtocolException類的典型用法代碼示例。如果您正苦於以下問題:Java ProtocolException類的具體用法?Java ProtocolException怎麽用?Java ProtocolException使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
ProtocolException類屬於org.apache.http包,在下文中一共展示了ProtocolException類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getRedirect
import org.apache.http.ProtocolException; //導入依賴的package包/類
public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException {
URI uri = this.getLocationURI(request, response, context);
String method = request.getRequestLine().getMethod();
if (method.equalsIgnoreCase(HttpHead.METHOD_NAME)) {
return new HttpHead(uri);
} else if (method.equalsIgnoreCase(HttpPost.METHOD_NAME)) {
return this.copyEntity(new HttpPost(uri), request);
} else if (method.equalsIgnoreCase(HttpPut.METHOD_NAME)) {
return this.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 this.copyEntity(new HttpPatch(uri), request);
} else {
return new HttpGet(uri);
}
}
示例2: isRedirected
import org.apache.http.ProtocolException; //導入依賴的package包/類
public boolean isRedirected(
final HttpRequest request,
final HttpResponse response,
final HttpContext context) throws ProtocolException {
if (request == null) {
throw new IllegalArgumentException("HTTP request may not be null");
}
if (response == null) {
throw new IllegalArgumentException("HTTP response may not be null");
}
int statusCode = response.getStatusLine().getStatusCode();
String method = request.getRequestLine().getMethod();
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:
return isRedirectable(method);
case HttpStatus.SC_SEE_OTHER:
return true;
default:
return false;
} //end of switch
}
示例3: RequestWrapper
import org.apache.http.ProtocolException; //導入依賴的package包/類
public RequestWrapper(final HttpRequest request) throws ProtocolException {
super();
if (request == null) {
throw new IllegalArgumentException("HTTP request may not be null");
}
this.original = request;
setParams(request.getParams());
setHeaders(request.getAllHeaders());
// Make a copy of the original URI
if (request instanceof HttpUriRequest) {
this.uri = ((HttpUriRequest) request).getURI();
this.method = ((HttpUriRequest) request).getMethod();
this.version = null;
} else {
RequestLine requestLine = request.getRequestLine();
try {
this.uri = new URI(requestLine.getUri());
} catch (URISyntaxException ex) {
throw new ProtocolException("Invalid request URI: "
+ requestLine.getUri(), ex);
}
this.method = requestLine.getMethod();
this.version = request.getProtocolVersion();
}
this.execCount = 0;
}
示例4: handleException
import org.apache.http.ProtocolException; //導入依賴的package包/類
/**
* Handles the given exception and generates an HTTP response to be sent
* back to the client to inform about the exceptional condition encountered
* in the course of the request processing.
*
* @param ex the exception.
* @param response the HTTP response.
*/
protected void handleException(final HttpException ex, final HttpResponse response) {
if (ex instanceof MethodNotSupportedException) {
response.setStatusCode(HttpStatus.SC_NOT_IMPLEMENTED);
} else if (ex instanceof UnsupportedHttpVersionException) {
response.setStatusCode(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED);
} else if (ex instanceof ProtocolException) {
response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
} else {
response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
}
String message = ex.getMessage();
if (message == null) {
message = ex.toString();
}
byte[] msg = EncodingUtils.getAsciiBytes(message);
ByteArrayEntity entity = new ByteArrayEntity(msg);
entity.setContentType("text/plain; charset=US-ASCII");
response.setEntity(entity);
}
示例5: RequestEntityRestStorageService
import org.apache.http.ProtocolException; //導入依賴的package包/類
public RequestEntityRestStorageService(final S3Session session,
final Jets3tProperties properties,
final HttpClientBuilder configuration) {
super(session.getHost().getCredentials().isAnonymousLogin() ? null :
new AWSCredentials(null, null) {
@Override
public String getAccessKey() {
return session.getHost().getCredentials().getUsername();
}
@Override
public String getSecretKey() {
return session.getHost().getCredentials().getPassword();
}
},
new PreferencesUseragentProvider().get(), null, properties);
this.session = session;
configuration.disableContentCompression();
configuration.setRetryHandler(new S3HttpRequestRetryHandler(this, preferences.getInteger("http.connections.retry")));
configuration.setRedirectStrategy(new DefaultRedirectStrategy() {
@Override
public HttpUriRequest getRedirect(final HttpRequest request, final HttpResponse response, final HttpContext context) throws ProtocolException {
if(response.containsHeader("x-amz-bucket-region")) {
final String host = ((HttpUriRequest) request).getURI().getHost();
if(!StringUtils.equals(session.getHost().getHostname(), host)) {
regionEndpointCache.putRegionForBucketName(
StringUtils.split(StringUtils.removeEnd(((HttpUriRequest) request).getURI().getHost(), session.getHost().getHostname()), ".")[0],
response.getFirstHeader("x-amz-bucket-region").getValue());
}
}
return super.getRedirect(request, response, context);
}
});
this.setHttpClient(configuration.build());
}
示例6: createLocationURI
import org.apache.http.ProtocolException; //導入依賴的package包/類
@Override
protected URI createLocationURI(String location) throws ProtocolException
{
try
{
final URIBuilder b = new URIBuilder(new URI(encode(location)).normalize());
final String host = b.getHost();
if (host != null)
{
b.setHost(host.toLowerCase(Locale.ROOT));
}
final String path = b.getPath();
if (TextUtils.isEmpty(path))
{
b.setPath("/");
}
return b.build();
}
catch (final URISyntaxException ex)
{
throw new ProtocolException("Invalid redirect URI: " + location, ex);
}
}
示例7: getRedirect
import org.apache.http.ProtocolException; //導入依賴的package包/類
@Override
public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException {
URI uri = getLocationURI(request, response, context);
String method = request.getRequestLine().getMethod();
if ("post".equalsIgnoreCase(method)) {
try {
HttpRequestWrapper httpRequestWrapper = (HttpRequestWrapper) request;
httpRequestWrapper.setURI(uri);
httpRequestWrapper.removeHeaders("Content-Length");
return httpRequestWrapper;
} catch (Exception e) {
logger.error("強轉為HttpRequestWrapper出錯");
}
return new HttpPost(uri);
} else {
return new HttpGet(uri);
}
}
示例8: rewriteRequestURI
import org.apache.http.ProtocolException; //導入依賴的package包/類
static void rewriteRequestURI(
final HttpRequestWrapper request,
final HttpRoute route) throws ProtocolException {
try {
URI uri = request.getURI();
if (uri != null) {
// Make sure the request URI is relative
if (uri.isAbsolute()) {
uri = URIUtilsHC4.rewriteURI(uri, null, true);
} else {
uri = URIUtilsHC4.rewriteURI(uri);
}
request.setURI(uri);
}
} catch (final URISyntaxException ex) {
throw new ProtocolException("Invalid URI: " + request.getRequestLine().getUri(), ex);
}
}
示例9: isRedirected
import org.apache.http.ProtocolException; //導入依賴的package包/類
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:
return isRedirectable(method);
case HttpStatus.SC_SEE_OTHER:
return true;
default:
return false;
} //end of switch
}
示例10: createLocationURI
import org.apache.http.ProtocolException; //導入依賴的package包/類
/**
* @since 4.1
*/
protected URI createLocationURI(final String location) throws ProtocolException {
try {
final URIBuilder b = new URIBuilder(new URI(location).normalize());
final String host = b.getHost();
if (host != null) {
b.setHost(host.toLowerCase(Locale.ENGLISH));
}
final String path = b.getPath();
if (TextUtils.isEmpty(path)) {
b.setPath("/");
}
return b.build();
} catch (final URISyntaxException ex) {
throw new ProtocolException("Invalid redirect URI: " + location, ex);
}
}
示例11: getRedirect
import org.apache.http.ProtocolException; //導入依賴的package包/類
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(HttpHeadHC4.METHOD_NAME)) {
return new HttpHeadHC4(uri);
} else if (method.equalsIgnoreCase(HttpGetHC4.METHOD_NAME)) {
return new HttpGetHC4(uri);
} else {
final int status = response.getStatusLine().getStatusCode();
if (status == HttpStatus.SC_TEMPORARY_REDIRECT) {
return RequestBuilder.copy(request).setUri(uri).build();
} else {
return new HttpGetHC4(uri);
}
}
}
示例12: determineRoute
import org.apache.http.ProtocolException; //導入依賴的package包/類
public HttpRoute determineRoute(
final HttpHost host,
final HttpRequest request,
final HttpContext context) throws HttpException {
Args.notNull(request, "Request");
if (host == null) {
throw new ProtocolException("Target host is not specified");
}
final HttpClientContext clientContext = HttpClientContext.adapt(context);
final RequestConfig config = clientContext.getRequestConfig();
final InetAddress local = config.getLocalAddress();
HttpHost proxy = config.getProxy();
if (proxy == null) {
proxy = determineProxy(host, request, context);
}
final HttpHost target;
if (host.getPort() <= 0) {
try {
target = new HttpHost(
host.getHostName(),
this.schemePortResolver.resolve(host),
host.getSchemeName());
} catch (final UnsupportedSchemeException ex) {
throw new HttpException(ex.getMessage());
}
} else {
target = host;
}
final boolean secure = target.getSchemeName().equalsIgnoreCase("https");
if (proxy == null) {
return new HttpRoute(target, local, secure);
} else {
return new HttpRoute(target, local, proxy, secure);
}
}
示例13: revalidateCacheEntry
import org.apache.http.ProtocolException; //導入依賴的package包/類
private HttpResponse revalidateCacheEntry(final HttpHost target,
final HttpRequestWrapper request, final HttpContext context, final HttpCacheEntry entry,
final Date now) throws ClientProtocolException {
try {
if (asynchRevalidator != null
&& !staleResponseNotAllowed(request, entry, now)
&& validityPolicy.mayReturnStaleWhileRevalidating(entry, now)) {
log.trace("Serving stale with asynchronous revalidation");
final HttpResponse resp = generateCachedResponse(request, context, entry, now);
asynchRevalidator.revalidateCacheEntry(target, request, context, entry);
return resp;
}
return revalidateCacheEntry(target, request, context, entry);
} catch (final IOException ioex) {
return handleRevalidationFailure(request, context, entry, now);
} catch (final ProtocolException e) {
throw new ClientProtocolException(e);
}
}
示例14: testRunGracefullyHandlesProtocolException
import org.apache.http.ProtocolException; //導入依賴的package包/類
@Test
public void testRunGracefullyHandlesProtocolException() throws Exception {
final String identifier = "foo";
final AsynchronousValidationRequest impl = new AsynchronousValidationRequest(
mockParent, mockClient, route, request, context, mockExecAware, mockCacheEntry,
identifier, 0);
when(
mockClient.revalidateCacheEntry(
route, request, context, mockExecAware, mockCacheEntry)).thenThrow(
new ProtocolException());
impl.run();
verify(mockClient).revalidateCacheEntry(
route, request, context, mockExecAware, mockCacheEntry);
verify(mockParent).markComplete(identifier);
verify(mockParent).jobFailed(identifier);
}
示例15: rewriteRequestURI
import org.apache.http.ProtocolException; //導入依賴的package包/類
static void rewriteRequestURI(
final HttpRequestWrapper request,
final HttpRoute route) throws ProtocolException {
try {
URI uri = request.getURI();
if (uri != null) {
// Make sure the request URI is relative
if (uri.isAbsolute()) {
uri = URIUtils.rewriteURI(uri, null, true);
} else {
uri = URIUtils.rewriteURI(uri);
}
request.setURI(uri);
}
} catch (final URISyntaxException ex) {
throw new ProtocolException("Invalid URI: " + request.getRequestLine().getUri(), ex);
}
}