List of usage examples for org.apache.http.util EntityUtils updateEntity
public static void updateEntity(HttpResponse httpResponse, HttpEntity httpEntity) throws IOException
From source file:org.springframework.cloud.netflix.ribbon.apache.HttpClientStatusCodeException.java
public HttpClientStatusCodeException(String serviceId, HttpResponse response, HttpEntity entity, URI uri) throws IOException { super(serviceId, response.getStatusLine().getStatusCode(), response, uri); this.response = new BasicHttpResponse(response.getStatusLine()); this.response.setLocale(response.getLocale()); this.response.setStatusCode(response.getStatusLine().getStatusCode()); this.response.setReasonPhrase(response.getStatusLine().getReasonPhrase()); this.response.setHeaders(response.getAllHeaders()); EntityUtils.updateEntity(this.response, entity); }
From source file:ch.cyberduck.core.b2.B2ErrorResponseInterceptor.java
@Override public boolean retryRequest(final HttpResponse response, final int executionCount, final HttpContext context) { switch (response.getStatusLine().getStatusCode()) { case HttpStatus.SC_UNAUTHORIZED: if (executionCount <= MAX_RETRIES) { final B2ApiException failure; try { EntityUtils.updateEntity(response, new BufferedHttpEntity(response.getEntity())); failure = new B2ApiException(EntityUtils.toString(response.getEntity()), new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase())); if (new B2ExceptionMappingService().map(failure) instanceof ExpiredTokenException) { // The authorization token is valid for at most 24 hours. try { authorizationToken = session.getClient().authenticate(accountId, applicationKey) .getAuthorizationToken(); return true; } catch (B2ApiException | IOException e) { log.warn(String.format("Attempt to renew expired auth token failed. %s", e.getMessage())); return false; }/*w w w . j av a 2 s . co m*/ } } catch (IOException e) { log.warn(String.format("Failure parsing response entity from %s", response)); return false; } } } return false; }
From source file:ch.cyberduck.core.sds.SDSErrorResponseInterceptor.java
@Override public boolean retryRequest(final HttpResponse response, final int executionCount, final HttpContext context) { switch (response.getStatusLine().getStatusCode()) { case HttpStatus.SC_UNAUTHORIZED: if (executionCount <= MAX_RETRIES) { final ApiException failure; try { EntityUtils.updateEntity(response, new BufferedHttpEntity(response.getEntity())); failure = new ApiException(response.getStatusLine().getStatusCode(), Collections.emptyMap(), EntityUtils.toString(response.getEntity())); if (new SDSExceptionMappingService().map(failure) instanceof ExpiredTokenException) { // The provided token is valid for two hours, every usage resets this period to two full hours again. Logging off invalidates the token. try { token = new AuthApi(session.getClient()).login( new LoginRequest().authType(session.getHost().getProtocol().getAuthorization()) .login(user).password(password)) .getToken(); return true; } catch (ApiException e) { // {"code":401,"message":"Unauthorized","debugInfo":"Wrong username or password","errorCode":-10011} log.warn(String.format("Attempt to renew expired auth token failed. %s", e.getMessage())); return false; }/* w ww . ja v a2s. c o m*/ } } catch (IOException e) { log.warn(String.format("Failure parsing response entity from %s", response)); return false; } } } return false; }
From source file:ch.cyberduck.core.sds.SDSWriteFeature.java
@Override public HttpResponseOutputStream<VersionId> write(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { final CreateFileUploadRequest body = new CreateFileUploadRequest(); body.setParentId(Long.parseLong( new SDSNodeIdProvider(session).getFileid(file.getParent(), new DisabledListProgressListener()))); body.setName(file.getName());/*from ww w. j a va 2 s. co m*/ body.classification(DEFAULT_CLASSIFICATION); try { final CreateFileUploadResponse response = new NodesApi(session.getClient()) .createFileUpload(StringUtils.EMPTY, body); final String uploadId = response.getUploadId(); final DelayedHttpMultipartEntity entity = new DelayedHttpMultipartEntity(file.getName(), status); final DelayedHttpEntityCallable<VersionId> command = new DelayedHttpEntityCallable<VersionId>() { @Override public VersionId call(final AbstractHttpEntity entity) throws BackgroundException { try { final SDSApiClient client = session.getClient(); final HttpPost request = new HttpPost( String.format("%s/nodes/files/uploads/%s", client.getBasePath(), uploadId)); request.setEntity(entity); request.setHeader(SDSSession.SDS_AUTH_TOKEN_HEADER, StringUtils.EMPTY); request.setHeader(HTTP.CONTENT_TYPE, String.format("multipart/form-data; boundary=%s", DelayedHttpMultipartEntity.DEFAULT_BOUNDARY)); final HttpResponse response = client.getClient().execute(request); try { // Validate response switch (response.getStatusLine().getStatusCode()) { case HttpStatus.SC_CREATED: // Upload complete break; default: EntityUtils.updateEntity(response, new BufferedHttpEntity(response.getEntity())); throw new SDSExceptionMappingService() .map(new ApiException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase(), Collections.emptyMap(), EntityUtils.toString(response.getEntity()))); } } finally { EntityUtils.consume(response.getEntity()); } final CompleteUploadRequest body = new CompleteUploadRequest(); if (status.isExists()) { body.setResolutionStrategy(CompleteUploadRequest.ResolutionStrategyEnum.OVERWRITE); } if (status.getFilekey() != null) { final ObjectReader reader = session.getClient().getJSON().getContext(null) .readerFor(FileKey.class); final FileKey fileKey = reader.readValue(status.getFilekey().array()); final EncryptedFileKey encryptFileKey = Crypto.encryptFileKey( TripleCryptConverter.toCryptoPlainFileKey(fileKey), TripleCryptConverter .toCryptoUserPublicKey(session.keyPair().getPublicKeyContainer())); body.setFileKey(TripleCryptConverter.toSwaggerFileKey(encryptFileKey)); } final Node upload = new NodesApi(client).completeFileUpload(StringUtils.EMPTY, uploadId, null, body); return new VersionId(String.valueOf(upload.getId())); } catch (IOException e) { throw new HttpExceptionMappingService().map("Upload {0} failed", e, file); } catch (ApiException e) { throw new SDSExceptionMappingService().map("Upload {0} failed", e, file); } catch (CryptoSystemException | InvalidFileKeyException | InvalidKeyPairException e) { throw new CryptoExceptionMappingService().map("Upload {0} failed", e, file); } } @Override public long getContentLength() { return entity.getContentLength(); } }; return this.write(file, status, command, entity); } catch (ApiException e) { throw new SDSExceptionMappingService().map("Upload {0} failed", e, file); } }