// *****************************************************************************
  // Delete Contact using HTTP DELETE method with singleContactUrl.
  // *****************************************************************************
  public Integer DeleteContact(Contact deleteContact) {
    Integer statusCode = 0;
    HttpResponse response;

    try {
      boolean isValid = true;

      // Data validation goes here

      if (isValid) {
        HttpDelete request = new HttpDelete(singleContactUrl + deleteContact.getId());
        request.setHeader("User-Agent", "dev.ronlemire.contactClient");
        request.setHeader("Accept", "application/json");
        request.setHeader("Content-type", "application/json");

        // Send request to WCF service
        DefaultHttpClient httpClient = new DefaultHttpClient();
        response = httpClient.execute(request);

        Log.d("WebInvoke", "Saving : " + response.getStatusLine().getStatusCode());

        statusCode = response.getStatusLine().getStatusCode();
      }

    } catch (Exception e) {
      e.printStackTrace();
    }

    return statusCode;
  }
  @Override
  public void deleteItem(final ProxyRepository repository, final ResourceStoreRequest request)
      throws ItemNotFoundException, UnsupportedStorageOperationException, RemoteStorageException {
    final URL remoteUrl =
        appendQueryString(getAbsoluteUrlFromBase(repository, request), repository);

    final HttpDelete method = new HttpDelete(remoteUrl.toExternalForm());

    final HttpResponse httpResponse = executeRequestAndRelease(repository, request, method);
    final int statusCode = httpResponse.getStatusLine().getStatusCode();

    if (statusCode != HttpStatus.SC_OK
        && statusCode != HttpStatus.SC_NO_CONTENT
        && statusCode != HttpStatus.SC_ACCEPTED) {
      throw new RemoteStorageException(
          "The response to HTTP "
              + method.getMethod()
              + " was unexpected HTTP Code "
              + statusCode
              + " : "
              + httpResponse.getStatusLine().getReasonPhrase()
              + " [repositoryId=\""
              + repository.getId()
              + "\", requestPath=\""
              + request.getRequestPath()
              + "\", remoteUrl=\""
              + remoteUrl.toString()
              + "\"]");
    }
  }
Exemple #3
0
  public static String delete(String url, Map<String, String> data)
      throws IllegalStateException, ClientProtocolException, HttpResponseException, IOException {
    HttpDelete request = null;

    if (data == null || data.isEmpty()) {
      request = new HttpDelete(url);
    } else {
      String paramStr = "";
      for (Map.Entry<String, String> entry : data.entrySet()) {
        paramStr += "&" + entry.getKey() + "=" + entry.getValue();
      }

      if (url.indexOf("?") == -1) {
        url += paramStr.replaceFirst("&", "?");
      } else {
        url += paramStr;
      }
      url += "&";
      url = encodeSpecCharacters(url);
      L.e(url);
      request = new HttpDelete(url);
    }
    request.addHeader("Content-Type", "application/x-www-form-urlencoded");
    // support Gzip
    supportGzip(request);
    return processResponse(httpClient.execute(request));
  }
  /**
   * Synchroniseert verwijderde polygonen met de server
   *
   * @param group groupid om polygonen uit te syncen
   * @throws SyncException
   */
  private void deletePolygons(int group) throws SyncException {
    Cursor c = db.getRemovedPolygons(group);
    int polygonid = 0;

    if (!c.moveToFirst()) {
      return; // Niks te syncen, dus gelijk klaar!
    }

    do {
      polygonid = c.getInt(0);

      HttpDelete httpd = new HttpDelete(serverUrl + "polygon/id/" + polygonid);
      UsernamePasswordCredentials creds = new UsernamePasswordCredentials(mappUser, mappPass);

      try {
        httpd.addHeader(new BasicScheme().authenticate(creds, httpd));
      } catch (AuthenticationException e1) {
        Log.e(Mapp.TAG, e1.getStackTrace().toString());
        throw new SyncException("Authentication failed");
      }

      HttpResponse response;

      try {
        response = httpclient.execute(httpd);

        if (response.getStatusLine().getStatusCode() == 418) {
          throw new SyncException("Unable to synchronize because the server is a teapot.");
        } else if (response.getStatusLine().getStatusCode() == 401) {
          throw new SyncException("Not authorized");
        } else if (response.getStatusLine().getStatusCode() != 200) {
          // Er is iets mis gegaan.
          // Lees de uitvoer
          InputStream is = response.getEntity().getContent();
          BufferedReader r = new BufferedReader(new InputStreamReader(is));
          StringBuilder total = new StringBuilder();
          String line;
          while ((line = r.readLine()) != null) {
            total.append(line);
          }

          JSONObject result = null;
          result = new JSONObject(total.toString());
          Log.e(Mapp.TAG, "Sync error: " + result.getString("message"));
          throw new SyncException(result.getString("message"));
        } else {
          db.removeRemovedPolygon(polygonid);
        }
      } catch (ClientProtocolException e) {
        Log.e(Mapp.TAG, e.getMessage());
        throw new SyncException("Epic HTTP failure");
      } catch (IOException e) {
        Log.e(Mapp.TAG, e.getMessage());
        throw new SyncException("Exception during server synchronisation");
      } catch (JSONException e) {
        Log.e(Mapp.TAG, "Sync failed. Getting status message from JSON response failed.");
        throw new SyncException("Invalid server response");
      }
    } while (c.moveToNext());
  }
  public DrvLicRepresentation cancel() {
    try {
      HttpDelete request = new HttpDelete(link.getHref());
      request.addHeader("Accept", DrvLicRepresentation.DRVLIC_MEDIA_TYPE);

      log.debug("calling {} {}", request.getMethod(), request.getURI());
      HttpResponse response = httpClient.execute(request);
      result = HttpResult.getResult(Void.class, null, response);
      if (result.status >= 200 && result.status <= 299) {
        return new DrvLicRepresentation(); // no links
      } else {
        log.warn(
            String.format(
                "error calling %s %s, %d:%s",
                request.getMethod(), link, result.status, result.errorMsg));
        return null;
      }
    } catch (UnsupportedEncodingException ex) {
      throw new RuntimeException(ex);
    } catch (IllegalStateException ex) {
      ex.printStackTrace();
      throw new RuntimeException("State error reading stream", ex);
    } catch (IOException ex) {
      ex.printStackTrace();
      throw new RuntimeException("IO error reading stream", ex);
    } catch (JAXBException ex) {
      ex.printStackTrace();
      throw new RuntimeException("JAXB error demarshalling result", ex);
    } finally {
    }
  }
  /**
   * 通过delete方式发送请求 删除农场
   *
   * @param url URL地址
   * @param params 参数 user_serial, password, farm_serial
   * @return
   * @throws Exception
   */
  public String httpdeleteFarm(String url, String tokenAuth) throws Exception {
    // 拼接请求URL
    int timeoutConnection = YunTongXun.httpclienttime;
    int timeoutSocket = YunTongXun.httpclienttime;
    HttpParams httpParameters =
        new BasicHttpParams(); // Set the timeout in milliseconds until a connection is established.
    HttpConnectionParams.setConnectionTimeout(
        httpParameters,
        timeoutConnection); // Set the default socket timeout (SO_TIMEOUT) // in milliseconds which
                            // is the timeout for waiting for data.
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

    // 构造HttpClient的实例
    HttpClient httpClient = new DefaultHttpClient(httpParameters);
    // 创建GET方法的实例
    HttpDelete httpexit = new HttpDelete(url);
    httpexit.setHeader("Authorization", tokenAuth);
    try {
      HttpResponse httpResponse = httpClient.execute(httpexit);
      int statusCode = httpResponse.getStatusLine().getStatusCode();
      return "{status_code:" + statusCode + "}";

    } catch (Exception e) {
      return null;
    }
  }
 @Test
 public void testDeleteVipCustomer() throws Exception {
   HttpDelete delete =
       new HttpDelete(
           "http://localhost:" + PORT_PATH + "/rest/customerservice/customers/vip/gold/123");
   delete.addHeader("Accept", "text/xml");
   HttpResponse response = httpclient.execute(delete);
   assertEquals(200, response.getStatusLine().getStatusCode());
 }
 /*.................................................................................................................*/
 public void deleteJob(HttpClient httpclient, String URL) {
   HttpDelete httpdelete = new HttpDelete(URL);
   httpdelete.addHeader("cipres-appkey", CIPRESkey);
   try {
     HttpResponse response = httpclient.execute(httpdelete);
   } catch (IOException e) {
     Debugg.printStackTrace(e);
   }
 }
Exemple #9
0
  // TODO: throw all exceptions in the Execute method (or equivalent)
  public void Execute(int method) throws UnsupportedEncodingException {
    last_method = method;
    switch (method) {
      case HTTP_GET:
        {
          HttpGet request = new HttpGet(url + getCombinedParams());
          // add headers
          for (NameValuePair h : headers) {
            request.addHeader(h.getName(), h.getValue());
          }
          executeRequest(request, url);
          break;
        }
      case HTTP_POST:
        {
          HttpPost request = new HttpPost(url);
          // add headers
          for (NameValuePair h : headers) {
            request.addHeader(h.getName(), h.getValue());
          }

          if (!params.isEmpty()) {
            request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
          }

          executeRequest(request, url);
          break;
        }
      case HTTP_PUT:
        {
          HttpPut request = new HttpPut(url);
          // add headers
          for (NameValuePair h : headers) {
            request.addHeader(h.getName(), h.getValue());
          }

          if (!params.isEmpty()) {
            request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
          }

          executeRequest(request, url);
          break;
        }
      case HTTP_DELETE:
        {
          HttpDelete request = new HttpDelete(url + getCombinedParams());
          // add headers
          for (NameValuePair h : headers) {
            request.addHeader(h.getName(), h.getValue());
          }
          executeRequest(request, url);
          break;
        }
    }
  }
 /**
  * Perform a HTTP DELETE request.
  *
  * @param context the Android Context which initiated the request.
  * @param url the URL to send the request to.
  * @param headers set one-time headers for this request
  * @param params additional DELETE parameters or files to send along with request
  * @param responseHandler the response handler instance that should handle the response.
  */
 public RequestHandle delete(
     Context context,
     String url,
     Header[] headers,
     RequestParams params,
     AsyncHttpResponseHandler responseHandler) {
   HttpDelete httpDelete =
       new HttpDelete(getUrlWithQueryString(isUrlEncodingEnabled, url, params));
   if (headers != null) httpDelete.setHeaders(headers);
   return sendRequest(httpClient, httpContext, httpDelete, null, responseHandler, context);
 }
  public HttpResponse doDelete(URI resourcePath) throws Exception {
    HttpDelete httpDelete = null;
    try {
      httpDelete = new HttpDelete(resourcePath);
      httpDelete.addHeader("Content-Type", "application/json");

      return httpClient.execute(httpDelete, new HttpResponseHandler());
    } finally {
      releaseConnection(httpDelete);
    }
  }
Exemple #12
0
  /**
   * Sends a POST request and returns the response.
   *
   * @param endpoint The endpoint to send the request to.
   * @param responseClass The class to deserialise the Json response to. Can be null if no response
   *     message is expected.
   * @param headers Any additional headers to send with this request. You can use {@link
   *     org.apache.http.HttpHeaders} constants for header names.
   * @param <T> The type to deserialise the response to.
   * @return A {@link Response} containing the deserialised body, if any.
   * @throws IOException If an error occurs.
   */
  public <T> Response<T> delete(Endpoint endpoint, Class<T> responseClass, NameValuePair... headers)
      throws IOException {

    // Create the request
    HttpDelete delete = new HttpDelete(endpoint.url());
    delete.setHeaders(combineHeaders(headers));

    // Send the request and process the response
    try (CloseableHttpResponse response = httpClient().execute(delete)) {
      T body = deserialiseResponseMessage(response, responseClass);
      return new Response<>(response.getStatusLine(), body);
    }
  }
 @SuppressWarnings("unused")
 private synchronized void doUnRegister(String service) {
   String url = registryURL;
   try {
     HttpDelete method = new HttpDelete(url);
     method.addHeader("service", service);
     ResponseHandler<String> handler = new BasicResponseHandler();
     String responseBody = httpClient.execute(method, handler);
     LOG.debug("DELETE to " + url + " got a " + responseBody);
   } catch (Exception e) {
     LOG.debug("DELETE to " + url + " failed with: " + e);
   }
 }
Exemple #14
0
  public static void deleteMessage(String login, String id) {
    HttpClient m_ClientHttp = new DefaultHttpClient();
    try {

      URI uri = new URI("https", WEB.URL, WEB.DELETE_MESSAGE(login, id), null, null);
      HttpDelete requeteDelete = new HttpDelete(uri);
      requeteDelete.addHeader("Content-Type", "application/json");

      String body = m_ClientHttp.execute(requeteDelete, new BasicResponseHandler());
    } catch (Exception e) {
      throw new IllegalArgumentException(e.getMessage());
    }
  }
  protected String getData(
      String url, String method, String contentType, String content, ContinuationToken cTokens) {
    try {
      HttpClient client = new DefaultHttpClient();
      HttpResponse res = null;
      url = url.replaceAll(" ", "%20");
      if (method == "GET") {
        HttpGet get = new HttpGet(url);
        get.addHeader("Authorization", token);
        get.addHeader("Content-Type", contentType);
        get.addHeader("Content-Length", content.length() + "");
        res = client.execute(get);
      } else if (method == "POST") {
        HttpPost post = new HttpPost(url);
        post.addHeader("Authorization", token);
        post.addHeader("Content-Type", contentType);
        StringEntity entity = new StringEntity(content, HTTP.UTF_8);
        post.setEntity(entity);
        res = client.execute(post);
      } else if (method == "DELETE") {
        HttpDelete del = new HttpDelete(url);
        del.addHeader("Authorization", token);
        del.addHeader("Content-Type", contentType);
        res = client.execute(del);
      } else if (method == "PUT") {
        HttpPut put = new HttpPut(url);
        put.addHeader("Authorization", token);
        put.addHeader("Content-Type", contentType);
        StringEntity entity = new StringEntity(content, HTTP.UTF_8);
        put.setEntity(entity);
        res = client.execute(put);
      }
      StatusLine sl = res.getStatusLine();
      String json = "";
      if (sl.getStatusCode() == HttpStatus.SC_OK) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        res.getEntity().writeTo(out);
        out.close();
        json = out.toString();
        Log.i("result", json);
        return json;
      } else {
        Log.i("error", sl.getReasonPhrase());
      }

    } catch (IOException ex) {
      ex.printStackTrace();
    }
    return null;
  }
  private static void delete(final String graph, boolean check)
      throws URISyntaxException, ClientProtocolException, IOException {
    URI uri = uri(graph);

    HttpDelete httpdelete = new HttpDelete(uri);
    httpdelete.setHeader("Accept", "text/plain");

    HttpResponse response = httpclient.execute(httpdelete);

    if (check) {
      assertEquals(204, response.getStatusLine().getStatusCode());
    }

    httpdelete.abort();
  }
  public boolean deleteRequest(String path) {
    log.info("deleteRequest(" + path + ")");
    // http://msdn.microsoft.com/en-us/library/windowsazure/dn151676.aspx
    HttpDelete httpDelete = new HttpDelete(getAPIEndPoint(path));
    httpDelete.addHeader("Authorization", this.getToken());
    httpDelete.addHeader("Content-Type", "application/json");
    httpDelete.addHeader("DataServiceVersion", "3.0;NetFx");
    httpDelete.addHeader("MaxDataServiceVersion", "3.0;NetFx");

    HttpClient httpClient = HttpClientBuilder.create().build();

    try {
      HttpResponse response = httpClient.execute(httpDelete);
      HttpEntity entity = response.getEntity();

      if (response.getStatusLine().getStatusCode() != 204) {
        log.error("An error occured when deleting an object in Office 365");
        this.invalidateToken();
        StringBuffer sb = new StringBuffer();
        if (entity != null && entity.getContent() != null) {
          BufferedReader in = new BufferedReader(new InputStreamReader(entity.getContent()));
          String s = null;

          log.info("Response :{0}", response.getStatusLine().toString());

          while ((s = in.readLine()) != null) {
            sb.append(s);
            log.info(s);
          }
        }
        throw new ConnectorException(
            "Delete Object failed to "
                + path
                + ". Error code was "
                + response.getStatusLine().getStatusCode()
                + ". Received the following response "
                + sb.toString());
      } else {
        return true;
      }
    } catch (ClientProtocolException cpe) {
      log.error(cpe, "Error doing deleteRequest to path {0}", path);
      throw new ConnectorException("Exception whilst doing DELETE to " + path);
    } catch (IOException ioe) {
      log.error(ioe, "IOE Error doing deleteRequest to path {0}", path);
      throw new ConnectorException("Exception whilst doing DELETE to " + path);
    }
  }
Exemple #18
0
  /**
   * Deletes OAuth Client from Authorization Server.
   *
   * @param consumerKey consumer key of the OAuth Client.
   * @throws APIManagementException
   */
  @Override
  public void deleteApplication(String consumerKey) throws APIManagementException {

    LOGGER.log(
        Level.INFO, "OAuthTwoClient - deleteApplication: " + consumerKey + " will be deleted.");

    Long id = nameIdMapping.get(consumerKey);

    String configURL = configuration.getParameter(OAuthTwoConstants.CLIENT_REG_ENDPOINT);
    String configURLsAccessToken =
        configuration.getParameter(OAuthTwoConstants.REGISTRAION_ACCESS_TOKEN);
    HttpClient client = getHttpClient();

    try {

      // Deletion has to be called providing the ID. If we don't have the
      // ID we can't proceed with Delete.
      if (id != null) {
        configURL += "/" + id.toString();
        HttpDelete httpDelete = new HttpDelete(configURL);

        LOGGER.log(
            Level.INFO, "OAuthTwoClient - deleteApplication: id:" + id + " URL:" + configURL);

        // Set Authorization Header
        httpDelete.addHeader(
            OAuthTwoConstants.AUTHORIZATION, OAuthTwoConstants.BEARER + configURLsAccessToken);
        HttpResponse response = client.execute(httpDelete);
        int responseCode = response.getStatusLine().getStatusCode();
        LOGGER.log(Level.INFO, "Delete application response code :  " + responseCode);
        if (responseCode == HttpStatus.SC_OK || responseCode == HttpStatus.SC_NO_CONTENT) {
          LOGGER.log(
              Level.INFO,
              "OAuth Client for consumer Id " + consumerKey + " has been successfully deleted");
          nameIdMapping.remove(consumerKey);
        } else {
          handleException("Problem occurred while deleting client for Consumer Key " + consumerKey);
        }
      }

    } catch (IOException e) {
      handleException("Error while reading response body from Server ", e);
    } finally {
      client.getConnectionManager().shutdown();
    }
  }
Exemple #19
0
 public HttpResponse delete(
     String cType,
     String accountSid,
     String authToken,
     String timestamp,
     String url,
     DefaultHttpClient httpclient,
     EncryptUtil encryptUtil)
     throws Exception {
   HttpDelete httpDelete = new HttpDelete(url);
   httpDelete.setHeader("Accept", cType);
   httpDelete.setHeader("Content-Type", cType + ";charset=utf-8");
   String src = accountSid + ":" + timestamp;
   String auth = encryptUtil.base64Encoder(src);
   httpDelete.setHeader("Authorization", auth);
   HttpResponse response = httpclient.execute(httpDelete);
   return response;
 }
  /**
   * Takes the extracted base URI and parameters from the {@link RequestConfig} consolidated
   * parameter type and creates a {@link HttpRequestBase} of the designated method type.
   *
   * @param uri the {@link URI} whose parameters should be populated
   * @param annotatedParams the list of {@link Param}s and the parameter objects which were
   *     annotated with them; <b>Complex objects should supply a formatted version of their String
   *     representation via {@link Object#toString()}</b>
   * @param staticParams the list of {@link Request.Param}s and the parameter objects which were
   *     annotated with them <br>
   *     <br>
   * @return the created {@link HttpRequestBase} which is an instance of {@link HttpPost} <br>
   *     <br>
   * @throws Exception when the {@link HttpRequestBase} could not be created due to an exception
   *     <br>
   *     <br>
   * @since 1.1.3
   */
  private static HttpRequestBase populateDeleteParameters(
      URI uri, Map<Object, Param> annotatedParams, List<Request.Param> staticParams)
      throws Exception {

    HttpParams httpParams = new BasicHttpParams();

    for (Request.Param param : staticParams) httpParams.setParameter(param.name(), param.value());

    Set<Entry<Object, Param>> methodParams = annotatedParams.entrySet();

    for (Entry<Object, Param> entry : methodParams)
      httpParams.setParameter(entry.getValue().value(), entry.getKey().toString());

    HttpDelete httpDelete = new HttpDelete(uri);
    httpDelete.setParams(httpParams);

    return httpDelete;
  }
 /**
  * This class deletes the interest. The Http Client sends an HTTP DEL request to the API to delete
  * the interest. The API sends information which is stored in Android via JSON to the webserver.
  *
  * @throws JSONException
  * @throws ClientProtocolException
  * @throws IOException
  */
 public void deleteInterest() throws JSONException, ClientProtocolException, IOException {
   HttpClient client = new DefaultHttpClient();
   HttpDelete del =
       new HttpDelete(
           "http://myapp-gosuninjas.dotcloud.com/api/v1/createinterest/"
               + UserProfile.interestpoint
               + "/");
   del.setHeader("Content-type", "application/json");
   del.setHeader("Content-Length", "0");
   try {
     HttpResponse response = client.execute(del);
   } catch (ClientProtocolException e1) {
     // TODO Auto-generated catch block
     e1.printStackTrace();
   } catch (IOException e1) {
     // TODO Auto-generated catch block
     e1.printStackTrace();
   }
 }
  @Override
  public String delete(String token, String said) throws ServiceNotAvailableException {
    HttpDelete httpDelete = new HttpDelete(serviceEnpoint + "?said=" + said);
    String encode = Base64.encodeBase64String((said + ":" + token).getBytes());
    httpDelete.addHeader("Authorization", "Basic " + encode.replace("\r\n", ""));

    HttpResponse response;
    try {
      response = httpClient.execute(httpDelete);

      HttpEntity entity = response.getEntity();
      if (entity != null) {
        String jsonResponse = IOUtils.toString(entity.getContent());
        logger.debug("Register response: {}", jsonResponse);
        return jsonResponse;
      }
    } catch (Exception e) {
      throw new ServiceNotAvailableException("Unable to register");
    }
    return null;
  }
Exemple #23
0
  @Override
  public String deleteGet(String url) {
    int responseCode = -1;
    HttpClient httpClient = new DefaultHttpClient();
    StringBuffer responseOutput = new StringBuffer();
    try {
      HttpDelete request = new HttpDelete(url);
      request.addHeader("content-type", "application/json");
      HttpResponse response = httpClient.execute(request);
      responseCode = response.getStatusLine().getStatusCode();

      if (responseCode == 200 || responseCode == 204) {
        if (response.getEntity() != null) {
          BufferedReader br =
              new BufferedReader(new InputStreamReader((response.getEntity().getContent())));

          String output;
          while ((output = br.readLine()) != null) {
            responseOutput.append(output);
          }
        }
      } else {
        logger.error("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
        throw new RuntimeException(
            "Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
      }

    } catch (Exception ex) {
      logger.error("exception for url = " + url);
      logger.error(elog.toStringException(ex));
    } finally {
      httpClient.getConnectionManager().shutdown();
    }

    if (responseCode == -1) {
      return "Exception";
    }
    return responseOutput.toString();
  }
  /* Delete the file used to test savePhoto() after the testcase has run */
  public void tearDown() throws Exception {

    /* Delete the story added to online */
    @SuppressWarnings("resource")
    HttpClient httpclient = new DefaultHttpClient();
    HttpDelete httpDelete =
        new HttpDelete("http://cmput301.softwareprocess.es:8080/cmput301f13t08/stories/" + 100);
    httpDelete.addHeader("Accept", "application/json");

    HttpResponse response = httpclient.execute(httpDelete);

    String status = response.getStatusLine().toString();
    System.out.println(status);

    HttpEntity entity = response.getEntity();
    BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
    String output;
    System.err.println("Output from Server -> ");
    while ((output = br.readLine()) != null) {
      System.err.println(output);
    }
    testActivity.getApplicationContext().deleteFile("Download15");
  }
Exemple #25
0
  public String delete(String url, Map<String, String> headers)
      throws ClientProtocolException, IOException, AuthorizationDeniedException {
    // create request
    HttpDelete request = new HttpDelete(url);

    if (logger.isDebugEnabled()) {
      logger.debug("getting url: " + url);
    }

    // add headers to request
    if (headers != null && headers.size() > 0) {
      for (String header : headers.keySet()) {
        String value = headers.get(header);
        request.addHeader(header, value);

        if (logger.isDebugEnabled()) {
          logger.debug("adding header: " + header + " = " + value);
        }
      }
    }

    return process(request);
  }
  /**
   * This method is used to delete all Instagram subscription.<br>
   * It used when we call to stop method.
   */
  public void deleteSubscription() {

    DefaultHttpClient client = new DefaultHttpClient();
    String url =
        "https://api.instagram.com/v1/subscriptions?object=all&client_id="
            + this.clientId
            + "&client_secret="
            + this.clientSecret;

    HttpDelete delete = new HttpDelete(url);

    try {

      log.debug("Delete  : " + delete + "Header = " + delete.getAllHeaders());
      HttpResponse response = client.execute(delete);
      log.info("-----> Delete  response  :" + response);

    } catch (ClientProtocolException e) {
      log.error(" Error in client Protocol, error type : " + e);
    } catch (IOException e) {
      log.error(" Error IO, error type :" + e);
    }
  }
 public void doDelete(String url) throws ClientProtocolException, IOException {
   HttpClient httpclient = new DefaultHttpClient();
   HttpDelete delete = new HttpDelete(url);
   delete.addHeader("accept", "application/json");
   httpclient.execute(delete);
 }
 /**
  * Perform a HTTP DELETE request.
  *
  * @param context the Android Context which initiated the request.
  * @param url the URL to send the request to.
  * @param headers set one-time headers for this request
  * @param responseHandler the response handler instance that should handle the response.
  */
 public void delete(
     Context context, String url, Header[] headers, AsyncHttpResponseHandler responseHandler) {
   final HttpDelete delete = new HttpDelete(url);
   if (headers != null) delete.setHeaders(headers);
   sendRequest(httpClient, httpContext, delete, null, responseHandler, context);
 }
Exemple #29
0
 public static void setParameter(HttpDelete delete) {
   delete.setParams(HTTPBuilder.getHttpParams());
 }
Exemple #30
0
  @Override
  public void onTrigger(final ProcessContext context, final ProcessSession session) {
    final boolean sendAsFlowFile = context.getProperty(SEND_AS_FLOWFILE).asBoolean();
    final int compressionLevel = context.getProperty(COMPRESSION_LEVEL).asInteger();
    final String userAgent = context.getProperty(USER_AGENT).getValue();

    final RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
    requestConfigBuilder.setConnectionRequestTimeout(
        context.getProperty(DATA_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue());
    requestConfigBuilder.setConnectTimeout(
        context.getProperty(CONNECTION_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue());
    requestConfigBuilder.setRedirectsEnabled(false);
    requestConfigBuilder.setSocketTimeout(
        context.getProperty(DATA_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue());
    final RequestConfig requestConfig = requestConfigBuilder.build();

    final StreamThrottler throttler = throttlerRef.get();
    final ProcessorLog logger = getLogger();

    final Double maxBatchBytes = context.getProperty(MAX_BATCH_SIZE).asDataSize(DataUnit.B);
    String lastUrl = null;
    long bytesToSend = 0L;

    final List<FlowFile> toSend = new ArrayList<>();
    DestinationAccepts destinationAccepts = null;
    CloseableHttpClient client = null;
    final String transactionId = UUID.randomUUID().toString();

    final ObjectHolder<String> dnHolder = new ObjectHolder<>("none");
    while (true) {
      FlowFile flowFile = session.get();
      if (flowFile == null) {
        break;
      }

      final String url = context.getProperty(URL).evaluateAttributeExpressions(flowFile).getValue();
      try {
        new java.net.URL(url);
      } catch (final MalformedURLException e) {
        logger.error(
            "After substituting attribute values for {}, URL is {}; this is not a valid URL, so routing to failure",
            new Object[] {flowFile, url});
        flowFile = session.penalize(flowFile);
        session.transfer(flowFile, REL_FAILURE);
        continue;
      }

      // If this FlowFile doesn't have the same url, throw it back on the queue and stop grabbing
      // FlowFiles
      if (lastUrl != null && !lastUrl.equals(url)) {
        session.transfer(flowFile);
        break;
      }

      lastUrl = url;
      toSend.add(flowFile);

      if (client == null || destinationAccepts == null) {
        final Config config = getConfig(url, context);
        final HttpClientConnectionManager conMan = config.getConnectionManager();

        final HttpClientBuilder clientBuilder = HttpClientBuilder.create();
        clientBuilder.setConnectionManager(conMan);
        clientBuilder.setUserAgent(userAgent);
        clientBuilder.addInterceptorFirst(
            new HttpResponseInterceptor() {
              @Override
              public void process(final HttpResponse response, final HttpContext httpContext)
                  throws HttpException, IOException {
                HttpCoreContext coreContext = HttpCoreContext.adapt(httpContext);
                ManagedHttpClientConnection conn =
                    coreContext.getConnection(ManagedHttpClientConnection.class);
                if (!conn.isOpen()) {
                  return;
                }

                SSLSession sslSession = conn.getSSLSession();

                if (sslSession != null) {
                  final X509Certificate[] certChain = sslSession.getPeerCertificateChain();
                  if (certChain == null || certChain.length == 0) {
                    throw new SSLPeerUnverifiedException("No certificates found");
                  }

                  final X509Certificate cert = certChain[0];
                  dnHolder.set(cert.getSubjectDN().getName().trim());
                }
              }
            });

        clientBuilder.disableAutomaticRetries();
        clientBuilder.disableContentCompression();

        final String username = context.getProperty(USERNAME).getValue();
        final String password = context.getProperty(PASSWORD).getValue();
        // set the credentials if appropriate
        if (username != null) {
          final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
          if (password == null) {
            credentialsProvider.setCredentials(
                AuthScope.ANY, new UsernamePasswordCredentials(username));
          } else {
            credentialsProvider.setCredentials(
                AuthScope.ANY, new UsernamePasswordCredentials(username, password));
          }
          clientBuilder.setDefaultCredentialsProvider(credentialsProvider);
        }
        client = clientBuilder.build();

        // determine whether or not destination accepts flowfile/gzip
        destinationAccepts = config.getDestinationAccepts();
        if (destinationAccepts == null) {
          try {
            if (sendAsFlowFile) {
              destinationAccepts =
                  getDestinationAcceptance(client, url, getLogger(), transactionId);
            } else {
              destinationAccepts = new DestinationAccepts(false, false, false, false, null);
            }

            config.setDestinationAccepts(destinationAccepts);
          } catch (IOException e) {
            flowFile = session.penalize(flowFile);
            session.transfer(flowFile, REL_FAILURE);
            logger.error(
                "Unable to communicate with destination {} to determine whether or not it can accept "
                    + "flowfiles/gzip; routing {} to failure due to {}",
                new Object[] {url, flowFile, e});
            context.yield();
            return;
          }
        }
      }

      // if we are not sending as flowfile, or if the destination doesn't accept V3 or V2
      // (streaming) format,
      // then only use a single FlowFile
      if (!sendAsFlowFile
          || (!destinationAccepts.isFlowFileV3Accepted()
              && !destinationAccepts.isFlowFileV2Accepted())) {
        break;
      }

      bytesToSend += flowFile.getSize();
      if (bytesToSend > maxBatchBytes.longValue()) {
        break;
      }
    }

    if (toSend.isEmpty()) {
      return;
    }

    final String url = lastUrl;
    final HttpPost post = new HttpPost(url);
    final List<FlowFile> flowFileList = toSend;
    final DestinationAccepts accepts = destinationAccepts;
    final boolean isDestinationLegacyNiFi = accepts.getProtocolVersion() == null;

    final EntityTemplate entity =
        new EntityTemplate(
            new ContentProducer() {
              @Override
              public void writeTo(final OutputStream rawOut) throws IOException {
                final OutputStream throttled =
                    (throttler == null) ? rawOut : throttler.newThrottledOutputStream(rawOut);
                OutputStream wrappedOut = new BufferedOutputStream(throttled);
                if (compressionLevel > 0 && accepts.isGzipAccepted()) {
                  wrappedOut = new GZIPOutputStream(wrappedOut, compressionLevel);
                }

                try (final OutputStream out = wrappedOut) {
                  for (final FlowFile flowFile : flowFileList) {
                    session.read(
                        flowFile,
                        new InputStreamCallback() {
                          @Override
                          public void process(final InputStream rawIn) throws IOException {
                            try (final InputStream in = new BufferedInputStream(rawIn)) {

                              FlowFilePackager packager = null;
                              if (!sendAsFlowFile) {
                                packager = null;
                              } else if (accepts.isFlowFileV3Accepted()) {
                                packager = new FlowFilePackagerV3();
                              } else if (accepts.isFlowFileV2Accepted()) {
                                packager = new FlowFilePackagerV2();
                              } else if (accepts.isFlowFileV1Accepted()) {
                                packager = new FlowFilePackagerV1();
                              }

                              // if none of the above conditions is met, we should never get here,
                              // because
                              // we will have already verified that at least 1 of the FlowFile
                              // packaging
                              // formats is acceptable if sending as FlowFile.
                              if (packager == null) {
                                StreamUtils.copy(in, out);
                              } else {
                                final Map<String, String> flowFileAttributes;
                                if (isDestinationLegacyNiFi) {
                                  // Old versions of NiFi expect nf.file.name and nf.file.path to
                                  // indicate filename & path;
                                  // in order to maintain backward compatibility, we copy the
                                  // filename & path to those attribute keys.
                                  flowFileAttributes = new HashMap<>(flowFile.getAttributes());
                                  flowFileAttributes.put(
                                      "nf.file.name",
                                      flowFile.getAttribute(CoreAttributes.FILENAME.key()));
                                  flowFileAttributes.put(
                                      "nf.file.path",
                                      flowFile.getAttribute(CoreAttributes.PATH.key()));
                                } else {
                                  flowFileAttributes = flowFile.getAttributes();
                                }

                                packager.packageFlowFile(
                                    in, out, flowFileAttributes, flowFile.getSize());
                              }
                            }
                          }
                        });
                  }

                  out.flush();
                }
              }
            });

    entity.setChunked(context.getProperty(CHUNKED_ENCODING).asBoolean());
    post.setEntity(entity);
    post.setConfig(requestConfig);

    final String contentType;
    if (sendAsFlowFile) {
      if (accepts.isFlowFileV3Accepted()) {
        contentType = APPLICATION_FLOW_FILE_V3;
      } else if (accepts.isFlowFileV2Accepted()) {
        contentType = APPLICATION_FLOW_FILE_V2;
      } else if (accepts.isFlowFileV1Accepted()) {
        contentType = APPLICATION_FLOW_FILE_V1;
      } else {
        logger.error(
            "Cannot send data to {} because the destination does not accept FlowFiles and this processor is "
                + "configured to deliver FlowFiles; rolling back session",
            new Object[] {url});
        session.rollback();
        context.yield();
        return;
      }
    } else {
      final String attributeValue = toSend.get(0).getAttribute(CoreAttributes.MIME_TYPE.key());
      contentType = (attributeValue == null) ? DEFAULT_CONTENT_TYPE : attributeValue;
    }

    final String attributeHeaderRegex = context.getProperty(ATTRIBUTES_AS_HEADERS_REGEX).getValue();
    if (attributeHeaderRegex != null && !sendAsFlowFile && flowFileList.size() == 1) {
      final Pattern pattern = Pattern.compile(attributeHeaderRegex);

      final Map<String, String> attributes = flowFileList.get(0).getAttributes();
      for (final Map.Entry<String, String> entry : attributes.entrySet()) {
        final String key = entry.getKey();
        if (pattern.matcher(key).matches()) {
          post.setHeader(entry.getKey(), entry.getValue());
        }
      }
    }

    post.setHeader(CONTENT_TYPE, contentType);
    post.setHeader(FLOWFILE_CONFIRMATION_HEADER, "true");
    post.setHeader(PROTOCOL_VERSION_HEADER, PROTOCOL_VERSION);
    post.setHeader(TRANSACTION_ID_HEADER, transactionId);
    if (compressionLevel > 0 && accepts.isGzipAccepted()) {
      post.setHeader(GZIPPED_HEADER, "true");
    }

    // Do the actual POST
    final String flowFileDescription =
        toSend.size() <= 10 ? toSend.toString() : toSend.size() + " FlowFiles";

    final String uploadDataRate;
    final long uploadMillis;
    CloseableHttpResponse response = null;
    try {
      final StopWatch stopWatch = new StopWatch(true);
      response = client.execute(post);

      // consume input stream entirely, ignoring its contents. If we
      // don't do this, the Connection will not be returned to the pool
      EntityUtils.consume(response.getEntity());
      stopWatch.stop();
      uploadDataRate = stopWatch.calculateDataRate(bytesToSend);
      uploadMillis = stopWatch.getDuration(TimeUnit.MILLISECONDS);
    } catch (final IOException e) {
      logger.error(
          "Failed to Post {} due to {}; transferring to failure",
          new Object[] {flowFileDescription, e});
      context.yield();
      for (FlowFile flowFile : toSend) {
        flowFile = session.penalize(flowFile);
        session.transfer(flowFile, REL_FAILURE);
      }
      return;
    } finally {
      if (response != null) {
        try {
          response.close();
        } catch (IOException e) {
          getLogger().warn("Failed to close HTTP Response due to {}", new Object[] {e});
        }
      }
    }

    // If we get a 'SEE OTHER' status code and an HTTP header that indicates that the intent
    // of the Location URI is a flowfile hold, we will store this holdUri. This prevents us
    // from posting to some other webservice and then attempting to delete some resource to which
    // we are redirected
    final int responseCode = response.getStatusLine().getStatusCode();
    final String responseReason = response.getStatusLine().getReasonPhrase();
    String holdUri = null;
    if (responseCode == HttpServletResponse.SC_SEE_OTHER) {
      final Header locationUriHeader = response.getFirstHeader(LOCATION_URI_INTENT_NAME);
      if (locationUriHeader != null) {
        if (LOCATION_URI_INTENT_VALUE.equals(locationUriHeader.getValue())) {
          final Header holdUriHeader = response.getFirstHeader(LOCATION_HEADER_NAME);
          if (holdUriHeader != null) {
            holdUri = holdUriHeader.getValue();
          }
        }
      }

      if (holdUri == null) {
        for (FlowFile flowFile : toSend) {
          flowFile = session.penalize(flowFile);
          logger.error(
              "Failed to Post {} to {}: sent content and received status code {}:{} but no Hold URI",
              new Object[] {flowFile, url, responseCode, responseReason});
          session.transfer(flowFile, REL_FAILURE);
        }
        return;
      }
    }

    if (holdUri == null) {
      if (responseCode == HttpServletResponse.SC_SERVICE_UNAVAILABLE) {
        for (FlowFile flowFile : toSend) {
          flowFile = session.penalize(flowFile);
          logger.error(
              "Failed to Post {} to {}: response code was {}:{}; will yield processing, "
                  + "since the destination is temporarily unavailable",
              new Object[] {flowFile, url, responseCode, responseReason});
          session.transfer(flowFile, REL_FAILURE);
        }
        context.yield();
        return;
      }

      if (responseCode >= 300) {
        for (FlowFile flowFile : toSend) {
          flowFile = session.penalize(flowFile);
          logger.error(
              "Failed to Post {} to {}: response code was {}:{}",
              new Object[] {flowFile, url, responseCode, responseReason});
          session.transfer(flowFile, REL_FAILURE);
        }
        return;
      }

      logger.info(
          "Successfully Posted {} to {} in {} at a rate of {}",
          new Object[] {
            flowFileDescription,
            url,
            FormatUtils.formatMinutesSeconds(uploadMillis, TimeUnit.MILLISECONDS),
            uploadDataRate
          });

      for (final FlowFile flowFile : toSend) {
        session
            .getProvenanceReporter()
            .send(flowFile, url, "Remote DN=" + dnHolder.get(), uploadMillis, true);
        session.transfer(flowFile, REL_SUCCESS);
      }
      return;
    }

    //
    // the response indicated a Hold URI; delete the Hold.
    //
    // determine the full URI of the Flow File's Hold; Unfortunately, the responses that are
    // returned have
    // changed over the past, so we have to take into account a few different possibilities.
    String fullHoldUri = holdUri;
    if (holdUri.startsWith("/contentListener")) {
      // If the Hold URI that we get starts with /contentListener, it may not really be
      // /contentListener,
      // as this really indicates that it should be whatever we posted to -- if posting directly to
      // the
      // ListenHTTP component, it will be /contentListener, but if posting to a proxy/load balancer,
      // we may
      // be posting to some other URL.
      fullHoldUri = url + holdUri.substring(16);
    } else if (holdUri.startsWith("/")) {
      // URL indicates the full path but not hostname or port; use the same hostname & port that we
      // posted
      // to but use the full path indicated by the response.
      int firstSlash = url.indexOf("/", 8);
      if (firstSlash < 0) {
        firstSlash = url.length();
      }
      final String beforeSlash = url.substring(0, firstSlash);
      fullHoldUri = beforeSlash + holdUri;
    } else if (!holdUri.startsWith("http")) {
      // Absolute URL
      fullHoldUri = url + (url.endsWith("/") ? "" : "/") + holdUri;
    }

    final HttpDelete delete = new HttpDelete(fullHoldUri);
    delete.setHeader(TRANSACTION_ID_HEADER, transactionId);

    while (true) {
      try {
        final HttpResponse holdResponse = client.execute(delete);
        EntityUtils.consume(holdResponse.getEntity());
        final int holdStatusCode = holdResponse.getStatusLine().getStatusCode();
        final String holdReason = holdResponse.getStatusLine().getReasonPhrase();
        if (holdStatusCode >= 300) {
          logger.error(
              "Failed to delete Hold that destination placed on {}: got response code {}:{}; routing to failure",
              new Object[] {flowFileDescription, holdStatusCode, holdReason});

          for (FlowFile flowFile : toSend) {
            flowFile = session.penalize(flowFile);
            session.transfer(flowFile, REL_FAILURE);
          }
          return;
        }

        logger.info(
            "Successfully Posted {} to {} in {} milliseconds at a rate of {}",
            new Object[] {flowFileDescription, url, uploadMillis, uploadDataRate});

        for (FlowFile flowFile : toSend) {
          session.getProvenanceReporter().send(flowFile, url);
          session.transfer(flowFile, REL_SUCCESS);
        }
        return;
      } catch (final IOException e) {
        logger.warn(
            "Failed to delete Hold that destination placed on {} due to {}",
            new Object[] {flowFileDescription, e});
      }

      if (!isScheduled()) {
        context.yield();
        logger.warn(
            "Failed to delete Hold that destination placed on {}; Processor has been stopped so routing FlowFile(s) to failure",
            new Object[] {flowFileDescription});
        for (FlowFile flowFile : toSend) {
          flowFile = session.penalize(flowFile);
          session.transfer(flowFile, REL_FAILURE);
        }
        return;
      }
    }
  }