/** * {@link Beta} <br/> * The call back method that will be invoked on a server error or an I/O * exception during resumable upload inside {@link #upload}. * * <p> * This method changes the current request to query the current status of * the upload to find how many bytes were successfully uploaded before the * server error occurred. * </p> */ @Beta void serverErrorCallback() throws IOException { Preconditions.checkNotNull(currentRequest, "The current request should not be null"); // Query the current status of the upload by issuing an empty PUT // request on the upload URI. currentRequest.setContent(new EmptyContent()); currentRequest .getHeaders() .setContentRange( "bytes */" + (isMediaLengthKnown() ? getMediaContentLength() : "*")); }
public static boolean addChildToGroup(HttpRequestFactory pHttpRequestFactory, AlfrescoConfig pConfig, String pStrGroupId, String pStrChildId) { mLog.debug("START addChildToGroup(String, String)"); boolean lRet = false; GroupsUrl lUrl = new GroupsUrl(pConfig.getHost()); lUrl.addChild(pStrGroupId, pStrChildId); try { // FIXME (Alessio): bisognerebbe trasferire altrove questo codice ripetitivo HttpHeaders lRequestHeaders = new HttpHeaders().setContentType("application/json"); HttpRequest lRequest = pHttpRequestFactory.buildPostRequest(lUrl, new EmptyContent()).setHeaders(lRequestHeaders); // TODO (Alessio): la risposta è completamente ignorata! Vanno gestiti i diversi casi // possibili. // L'implementazione dovrebbe prima controllare se l'utente è già parte del gruppo, // quindi eventualmente effettuare l'aggiunta. int lStatusCode = lRequest.execute().getStatusCode(); if (200 <= lStatusCode && lStatusCode < 300) { lRet = true; } } catch (Exception e) { // TODO (Alessio): gestione decente delle eccezioni // mLog.error("Unexpected failure", e); lRet = false; } mLog.debug("END addChildToGroup(String, String)"); return lRet; }
/** * This method sends a POST request with empty content to get the unique * upload URL. * * @param initiationRequestUrl * The request URL where the initiation request will be sent */ private HttpResponse executeUploadInitiation(GenericUrl initiationRequestUrl) throws IOException { updateStateAndNotifyListener(UploadState.INITIATION_STARTED); initiationRequestUrl.put("uploadType", "resumable"); HttpContent content = metadata == null ? new EmptyContent() : metadata; HttpRequest request = requestFactory.buildRequest( initiationRequestMethod, initiationRequestUrl, content); initiationHeaders.set(CONTENT_TYPE_HEADER, mediaContent.getType()); if (isMediaLengthKnown()) { initiationHeaders.set(CONTENT_LENGTH_HEADER, getMediaContentLength()); } request.getHeaders().putAll(initiationHeaders); HttpResponse response = executeCurrentRequest(request); boolean notificationCompleted = false; try { updateStateAndNotifyListener(UploadState.INITIATION_COMPLETE); notificationCompleted = true; } finally { if (!notificationCompleted) { response.disconnect(); } } return response; }
/** * Executes the current request with some common code that includes * exponential backoff and GZip encoding. * * @param request * current request * @return HTTP response */ private HttpResponse executeCurrentRequest(HttpRequest request) throws IOException { // enable GZip encoding if necessary if (!disableGZipContent && !(request.getContent() instanceof EmptyContent)) { request.setEncoding(new GZipEncoding()); } // execute request HttpResponse response = executeCurrentRequestWithoutGZip(request); return response; }
private HttpRequest buildHttpRequest() throws IOException { final HttpRequest httpRequest = getGracenote().getRequestFactory().buildRequest(HttpMethods.POST, buildHttpRequestUrl(), httpContent); httpRequest.setParser(getGracenote().getObjectParser()); if (httpContent == null) { httpRequest.setContent(new EmptyContent()); } return httpRequest; }
/** * Initiates the resumable upload by sending a request to Google Cloud Storage. * * @param batchJobUploadUrl the {@code uploadUrl} of a {@code BatchJob} * @return the URI for the initiated resumable upload */ private URI initiateResumableUpload(URI batchJobUploadUrl) throws BatchJobException { // This follows the Google Cloud Storage guidelines for initiating resumable uploads: // https://cloud.google.com/storage/docs/resumable-uploads-xml HttpRequestFactory requestFactory = httpTransport.createRequestFactory(new HttpRequestInitializer() { @Override public void initialize(HttpRequest request) throws IOException { HttpHeaders headers = createHttpHeaders(); headers.setContentLength(0L); headers.set("x-goog-resumable", "start"); request.setHeaders(headers); request.setLoggingEnabled(true); } }); try { HttpRequest httpRequest = requestFactory.buildPostRequest(new GenericUrl(batchJobUploadUrl), new EmptyContent()); HttpResponse response = httpRequest.execute(); if (response.getHeaders() == null || response.getHeaders().getLocation() == null) { throw new BatchJobException( "Initiate upload failed. Resumable upload URI was not in the response."); } return URI.create(response.getHeaders().getLocation()); } catch (IOException e) { throw new BatchJobException("Failed to initiate upload", e); } }