public static String post(String url, List<NameValuePair> params) {
   String body = null;
   try {
     HttpClient hc = new DefaultHttpClient();
     // Post请求
     HttpPost httppost = new HttpPost(url);
     // 设置参数
     httppost.setEntity(new UrlEncodedFormEntity(params));
     // 设置请求和传输超时时间
     RequestConfig requestConfig =
         RequestConfig.custom().setSocketTimeout(4000).setConnectTimeout(4000).build();
     httppost.setConfig(requestConfig);
     // 发送请求
     HttpResponse httpresponse = hc.execute(httppost);
     int statusCode = httpresponse.getStatusLine().getStatusCode();
     if (statusCode != HttpStatus.SC_OK) {
       logger.error("HTTP" + "  " + "HttpMethod failed: " + httpresponse.getStatusLine());
     }
     // 获取返回数据
     HttpEntity entity = httpresponse.getEntity();
     body = EntityUtils.toString(entity);
     if (entity != null) {
       entity.consumeContent();
     }
   } catch (Exception e) {
     logger.error("http的post方法" + e.toString());
     throw new RuntimeException(e);
   }
   return body;
 }
예제 #2
0
 public static String uploadMaterial(
     String url, Map<String, String> params, String formDataName, File file) {
   client = getHttpClientInstance();
   URI uri = generateURLParams(url, params);
   HttpPost post = new HttpPost(uri);
   post.setConfig(requestConfig);
   ContentBody contentBody = new FileBody(file);
   HttpEntity reqestEntity =
       MultipartEntityBuilder.create().addPart(formDataName, contentBody).build();
   post.setEntity(reqestEntity);
   String responseStr = null;
   CloseableHttpResponse httpResponse = null;
   try {
     httpResponse = client.execute(post);
     responseStr = generateHttpResponse(httpResponse);
   } catch (IOException e) {
   } finally {
     if (null != httpResponse) {
       try {
         httpResponse.close();
       } catch (IOException e) {
         e.printStackTrace();
       }
     }
   }
   return responseStr;
 }
예제 #3
0
 public static String doPost(String url, Map<String, String> params, String jsonStr) {
   logger.info(jsonStr);
   client = getHttpClientInstance();
   URI uri = generateURLParams(url, params);
   HttpPost post = new HttpPost(uri);
   post.setConfig(requestConfig);
   String responseStr = null;
   CloseableHttpResponse httpResponse = null;
   try {
     HttpEntity entity = new StringEntity(jsonStr, DEFAULT_CHARSET);
     post.setEntity(entity);
     post.setHeader("Content-Type", "application/json");
     httpResponse = client.execute(post);
     responseStr = generateHttpResponse(httpResponse);
   } catch (IOException e) {
     e.printStackTrace();
   } finally {
     if (null != httpResponse) {
       try {
         httpResponse.close();
       } catch (IOException e) {
         e.printStackTrace();
       }
     }
   }
   return responseStr;
 }
  public HttpPost createPostMethod(String url, Object postData) {
    try {
      byte[] rawData;

      if (postData instanceof byte[]) {
        rawData = (byte[]) postData;
      } else {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(postData);
        oos.close();
        rawData = baos.toByteArray();
      }

      InputStream is = new ByteArrayInputStream(rawData);

      HttpPost httpPost = new HttpPost(url);
      BasicHttpEntity entity = new BasicHttpEntity();
      entity.setContentType("application/octet-stream");
      entity.setContentLength(rawData.length);
      entity.setContent(is);
      httpPost.setEntity(entity);

      RequestConfig requestConfig =
          RequestConfig.copy(HAZELCAST_REQUEST_CONFIG)
              .setSocketTimeout(TIMEOUT)
              .setConnectTimeout(TIMEOUT)
              .setConnectionRequestTimeout(TIMEOUT)
              .build();
      httpPost.setConfig(requestConfig);
      return httpPost;
    } catch (IOException e) {
      throw new RuntimeException("Error POST-ing data", e);
    }
  }
예제 #5
0
  /**
   * @param url
   * @param formData 提交的数
   * @throws IOException
   */
  public static void post(String url, Map<String, Object> formData) throws Exception {
    HttpClient client = HttpClients.createDefault();
    HttpPost post = new HttpPost(url);
    List<NameValuePair> formparams = new ArrayList<NameValuePair>();
    if (formData != null) {
      Iterator<Map.Entry<String, Object>> iterator = formData.entrySet().iterator();
      while (iterator.hasNext()) {
        Map.Entry<String, Object> next = iterator.next();
        String key = next.getKey();
        Object value = next.getValue();
        formparams.add(new BasicNameValuePair(key, value.toString()));
      }
    }

    HttpEntity reqEntity = new UrlEncodedFormEntity(formparams, "utf-8");

    RequestConfig requestConfig =
        RequestConfig.custom()
            .setSocketTimeout(50000)
            .setConnectTimeout(50000)
            .setConnectionRequestTimeout(50000)
            .build();
    post.setEntity(reqEntity);
    post.setConfig(requestConfig);
    HttpResponse response = client.execute(post);
    if (response.getStatusLine().getStatusCode() != 200) {
      throw new Exception("请求失败");
    }
  }
예제 #6
0
 public String post() throws Exception {
   HttpClient client = new DefaultHttpClient();
   HttpPost request = new HttpPost(url);
   HttpEntity entity = new ByteArrayEntity(postContent.getBytes("UTF-8"));
   request.setConfig(requestConfig);
   request.setEntity(entity);
   HttpResponse response = client.execute(request);
   String result = EntityUtils.toString(response.getEntity());
   return result;
 }
예제 #7
0
  public static HttpResponse post(
      String url, List<NameValuePair> params, RequestConfig config, CookieStore cookieStore)
      throws IOException {
    CloseableHttpClient client =
        HttpClients.custom().setUserAgent(Constants.DEFAULT_USER_AGENT).build();
    HttpContext localContext = new BasicHttpContext();
    localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);

    HttpPost post = new HttpPost(url);
    post.setEntity(new UrlEncodedFormEntity(params));
    post.setConfig(config);
    HttpResponse response = client.execute(post, localContext);
    return response;
  }
예제 #8
0
  /**
   * Get file
   *
   * @param url
   * @return
   * @throws Exception
   */
  public static String basicHttpAccess(String url) throws Exception {
    RequestConfig config =
        RequestConfig.custom()
            .setConnectionRequestTimeout(30 * 1000) // 30 sec
            .setConnectTimeout(30 * 1000) // 30sec
            .build();
    HttpClient httpclient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
    HttpPost httppost = new HttpPost(url);
    httppost.setConfig(config);
    try {
      // Execute and get the response.
      HttpResponse response = httpclient.execute(httppost);

      if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { // check HttpStatus
        HttpEntity entity = response.getEntity();
        if (entity != null) {

          @SuppressWarnings("deprecation")
          String origCharset = EntityUtils.getContentCharSet(entity); // get charset
          // get content from entity
          InputStream instream = entity.getContent();
          String content = MyCharset.convertCharset(instream, origCharset, MyContext.Charset);

          // clear resouce
          instream.close();

          // return
          return content;
        }
      } else {
        throw new Exception(
            "url:"
                + url
                + " http request fetch failed. http response status code is "
                + response.getStatusLine().getStatusCode());
      }
    } catch (Exception ex) {
      ex.printStackTrace();
      throw ex;
    } finally {
      // clear resouce
      httppost = null;
      httpclient = null;
    }
    return null;
  }
예제 #9
0
파일: HttpUtil.java 프로젝트: sumekey/sms
  public static String post(String url, Map<String, String> paramsMap) throws ApiException {
    // reuse httpclient to keepalive to the server
    // keepalive in https will save time on tcp handshaking.
    if (null == httpClient) httpClient = createSSLClientDefault();

    CloseableHttpClient client = httpClient;
    String responseText = null;
    HttpPost method = new HttpPost(url);
    RequestConfig requestConfig =
        RequestConfig.custom()
            .setConnectTimeout(CONNECTION_TIMEOUT)
            .setSocketTimeout(SOCKETCOOECTION_TIMEOUT)
            .build(); // 设置请求超时时间
    method.setConfig(requestConfig);
    HttpEntity entity = null;
    CloseableHttpResponse response = null;
    try {
      if (paramsMap != null) {
        List<NameValuePair> paramList = new ArrayList<NameValuePair>();
        for (Map.Entry<String, String> param : paramsMap.entrySet()) {
          org.apache.http.NameValuePair pair =
              new BasicNameValuePair(param.getKey(), param.getValue());
          paramList.add(pair);
        }
        method.setEntity(new UrlEncodedFormEntity(paramList, ApiConfig.getEncode()));
      }
      response = client.execute(method);
      entity = response.getEntity();
      if (entity != null) {
        responseText = EntityUtils.toString(entity);
      }

      if (response != null) {
        response.close();
      }
      if (response.getStatusLine().getStatusCode() != 200) throw new ApiException(responseText);

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

    } catch (IOException e) {
      e.printStackTrace();
    }
    return responseText;
  }
  @Override
  public String execute(
      CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String postEntity)
      throws WxErrorException, ClientProtocolException, IOException {
    HttpPost httpPost = new HttpPost(uri);
    if (httpProxy != null) {
      RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build();
      httpPost.setConfig(config);
    }

    if (postEntity != null) {
      StringEntity entity = new StringEntity(postEntity, Consts.UTF_8);
      httpPost.setEntity(entity);
    }

    CloseableHttpResponse response = httpclient.execute(httpPost);
    String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response);
    return responseContent;
  }
예제 #11
0
  @SuppressWarnings({"rawtypes", "unchecked"})
  public static IMHttpResponse post(String url, Map params) {
    IMHttpResponse response = new IMHttpResponse();
    if (StringUtils.isEmpty(url)) {
      response.setStatusCode(404);
      return response;
    }

    CloseableHttpClient httpClient = null;
    HttpPost httpPost = null;
    try {
      httpPost = new HttpPost(url); // HTTP Get请求(POST雷同)
      List<NameValuePair> postData = new ArrayList<NameValuePair>();
      Iterator<Entry> piter = params.entrySet().iterator();
      while (piter.hasNext()) {
        Entry entry = piter.next();
        postData.add(
            new BasicNameValuePair(
                String.valueOf(entry.getKey()), String.valueOf(entry.getValue())));
      }
      StringEntity entity = new StringEntity(URLEncodedUtils.format(postData, "UTF-8"));
      httpPost.setEntity(entity);
      RequestConfig requestConfig =
          RequestConfig.custom()
              .setSocketTimeout(2000)
              .setConnectTimeout(2000)
              .build(); // 设置请求和传输超时时间
      httpPost.setConfig(requestConfig);
      httpClient = HttpClients.createDefault();
      CloseableHttpResponse hresp = httpClient.execute(httpPost); // 执行请求
      String responseString = EntityUtils.toString(hresp.getEntity());
      response.setStatusCode(hresp.getStatusLine().getStatusCode());
      response.setResponseBody(responseString);
      return response;
    } catch (Exception e) {
      logger.error("url: " + url, e);
    } finally {
      if (httpPost != null) {
        httpPost.releaseConnection();
      }
    }
    return response;
  }
  public static void submit(String submissionIdentifier, String pipeline, int startingProcessIndex)
      throws IOException {
    setup();

    ObjectNode userOb = objectMapper.createObjectNode();

    userOb.put("priority", properties.getProperty("biosamples.conan.priority"));
    userOb.put("pipelineName", pipeline);
    userOb.put("startingProcessIndex", startingProcessIndex);
    userOb.put("restApiKey", properties.getProperty("biosamples.conan.apikey"));
    ObjectNode inputParameters = userOb.putObject("inputParameters");
    inputParameters.put("SampleTab Accession", submissionIdentifier);

    log.trace(userOb.toString());

    // Send data
    HttpPost postRequest =
        new HttpPost(properties.getProperty("biosamples.conan.url") + "/api/submissions/");
    postRequest.setConfig(
        RequestConfig.custom()
            .setSocketTimeout(0)
            .setConnectTimeout(0)
            .setConnectionRequestTimeout(0)
            .build());
    StringEntity input = new StringEntity(userOb.toString());
    input.setContentType("application/json");
    postRequest.setEntity(input);

    // get response
    try (CloseableHttpResponse response = httpClient.execute(postRequest)) {
      try (BufferedReader br =
          new BufferedReader(new InputStreamReader((response.getEntity().getContent())))) {
        // TODO parse response and raise exception if submit failed
        String line;
        while ((line = br.readLine()) != null) {
          log.info(line);
        }
      }
      EntityUtils.consume(response.getEntity());
    }
  }
예제 #13
0
  protected void sendStatus(String label, GitHubStatus status) {
    logger.info("  ** " + label + " : " + status + "  ** ");

    HttpPost httpPostRequest = new HttpPost(githubEndpoint);

    try {
      String payload =
          String.format(
              "{\"state\": \"%s\", \"target_url\": \"%s\", \"description\": \"%s\", \"context\": \"%s\"}",
              status, "http://github.com", "This is a meaningful description", label);

      StringEntity params = new StringEntity(payload);

      httpPostRequest.addHeader("content-type", "application/json");
      httpPostRequest.addHeader("Authorization", "token " + githubToken);

      RequestConfig requestConfig =
          RequestConfig.custom()
              .setSocketTimeout(5000)
              .setConnectTimeout(5000)
              .setConnectionRequestTimeout(5000)
              .build();
      httpPostRequest.setConfig(requestConfig);

      httpPostRequest.setEntity(params);

      HttpResponse response = httpClient.execute(httpPostRequest);
      if (response.getStatusLine().getStatusCode() >= 300) {
        logger.error(response.getStatusLine().toString());
      }
      logger.info(response.getStatusLine().toString());
    } catch (Exception e) {
      logger.error(e.getMessage());
      e.printStackTrace();
    } finally {
      httpPostRequest.releaseConnection();
    }

    // post.s

  }
예제 #14
0
  /** header由調用者set Create by : 2015年9月14日 上午11:54:59 */
  private String doPost(HttpPost httpPost) throws Exception {
    try {
      httpPost.setConfig(config);
      /**
       * setHeader(name, value):如果Header中没有定义则添加,如果已定义则用新的value覆盖原用value值。 addHeader(name,
       * value):如果Header中没有定义则添加,如果已定义则保持原有value不改变。
       */
      //			httpPost.addHeader( HTTP.CONTENT_TYPE, "application/json" ) ;
      //			httpPost.addHeader( "Accept-Charset", "gbk,GB2312,utf-8;q=0.7,*;q=0.7" ) ;
      //			httpPost.addHeader( "Accept-Language", "zh-cn,zh;q=0.5" ) ;
      //			httpPost.addHeader( HTTP.CONN_DIRECTIVE, "keep-alive" ) ;
      //			httpPost.addHeader( "refer", "localhost" ) ;
      //			httpPost.addHeader( HTTP.USER_AGENT, "Mozilla/5.0 (Windows NT 6.1; rv:6.0.2)
      // Gecko/20100101 Firefox/6.0.2" ) ;

      CloseableHttpResponse response = httpClient.execute(httpPost, HttpClientContext.create());
      try {
        HttpEntity entity = response.getEntity();
        logger.debug("{}", entity);

        return EntityUtils.toString(entity);
        //				return EntityUtils.toString( entity, "utf-8" );
      } finally {
        if (null != response) response.close();
      }
    } catch (ClientProtocolException e) {
      logger.error("HttpClientUtil error : [{}].", e.getMessage(), e);
      throw e;
    } catch (ParseException e) {
      logger.error("HttpClientUtil error : [{}].", e.getMessage(), e);
      throw e;
    } catch (IOException e) {
      logger.error("HttpClientUtil error : [{}].", e.getMessage(), e);
      throw e;
    } catch (Exception e) {
      logger.error("HttpClientUtil error : [{}].", e.getMessage(), e);
      throw e;
    }
  }
예제 #15
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;
      }
    }
  }
예제 #16
0
  public static void search() {
    long startTimeMillis = System.currentTimeMillis(); // 开始时间
    // 创建HttpClientBuilder
    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    // HttpClient
    CloseableHttpClient closeableHttpClient = httpClientBuilder.build();

    HttpPost httpPost = new HttpPost("http://122.224.5.41:8080/api.aspx");
    RequestConfig requestConfig =
        RequestConfig.custom()
            .setConnectionRequestTimeout(3000)
            .setConnectTimeout(3000)
            .setSocketTimeout(3000)
            .build();
    httpPost.setConfig(requestConfig);

    try {
      String account = "zhiqi";
      String action = "getReports";
      String signKey = "4b27d66b8c91435f81a8852e0a9f247c";
      String count = "2";
      String v = "1.1";

      StringBuffer signData = new StringBuffer();
      signData.append("account=");
      signData.append(account);
      signData.append("&count=");
      signData.append(count);
      signData.append("&key=");
      signData.append(signKey);

      String sign = DigestUtils.md5Hex(signData.toString());

      // 创建参数队列
      List<NameValuePair> formparams = new ArrayList<NameValuePair>();
      formparams.add(new BasicNameValuePair("v", v));
      formparams.add(new BasicNameValuePair("action", action));
      formparams.add(new BasicNameValuePair("account", account));
      formparams.add(new BasicNameValuePair("count", count));
      formparams.add(new BasicNameValuePair("sign", sign));

      UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
      httpPost.setEntity(entity);

      HttpResponse httpResponse;
      // post请求
      httpResponse = closeableHttpClient.execute(httpPost);

      // getEntity()
      HttpEntity httpEntity = httpResponse.getEntity();
      if (httpEntity != null) {
        // 打印响应内容

        String restr = EntityUtils.toString(httpEntity, "UTF-8");
        System.out.println("response:" + restr);

        Gson gson = new Gson();
        Map map = gson.fromJson(restr, Map.class);
        if ("0".equals(map.get("Code"))) {}
      }
      //					closeableHttpClient.close();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        // 释放资源
        closeableHttpClient.close();
      } catch (IOException e) {
      }
    }
  }
예제 #17
0
  public Boolean sendMsg(Consume consume) throws Exception {
    // 创建HttpClientBuilder
    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    // HttpClient
    CloseableHttpClient closeableHttpClient = httpClientBuilder.build();

    HttpPost httpPost = new HttpPost(httpUrl);
    RequestConfig requestConfig =
        RequestConfig.custom()
            .setConnectionRequestTimeout(3000)
            .setConnectTimeout(3000)
            .setSocketTimeout(3000)
            .build();
    httpPost.setConfig(requestConfig);
    try {

      int opsummoney = consume.getConsumePrice().intValue() * opmoney;
      // 创建参数队列
      List<NameValuePair> formparams = new ArrayList<NameValuePair>();
      formparams.add(new BasicNameValuePair("platformname", platformname));
      formparams.add(new BasicNameValuePair("opstreamid", consume.getConsumeId()));
      formparams.add(new BasicNameValuePair("agtphone", agtphone));
      formparams.add(
          new BasicNameValuePair("agtpasswd", DigestUtils.md5Hex(agtpasswd).toUpperCase()));
      formparams.add(new BasicNameValuePair("optype", "HFCZ")); // 话费充值
      formparams.add(new BasicNameValuePair("custphone", consume.getConsumePhone()));
      formparams.add(new BasicNameValuePair("opmoney", opmoney + ""));
      formparams.add(new BasicNameValuePair("opcount", consume.getConsumePrice().intValue() + ""));
      formparams.add(new BasicNameValuePair("opsummoney", opsummoney + ""));
      formparams.add(new BasicNameValuePair("busitype", "01")); // 01手机
      formparams.add(new BasicNameValuePair("isp", this.ispTransformation(consume.getIsp())));
      formparams.add(new BasicNameValuePair("rechargetype", ""));
      formparams.add(new BasicNameValuePair("servicecode", ""));
      formparams.add(new BasicNameValuePair("servicesubcode", ""));
      formparams.add(new BasicNameValuePair("clientip", ""));
      formparams.add(new BasicNameValuePair("storageid", ""));
      formparams.add(new BasicNameValuePair("ip", ""));
      formparams.add(new BasicNameValuePair("notifyurl", notifyurl));

      StringBuffer sb = new StringBuffer();
      for (NameValuePair nameValuePair : formparams) {
        sb.append(nameValuePair.getName());
        sb.append("=");
        sb.append(nameValuePair.getValue());
        sb.append("&");
      }
      sb.append(key);
      //			System.out.println(sb.toString());
      formparams.add(new BasicNameValuePair("sign", DigestUtils.md5Hex(sb.toString())));

      UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
      httpPost.setEntity(entity);

      HttpResponse httpResponse;
      // post请求
      httpResponse = closeableHttpClient.execute(httpPost);

      // getEntity()
      HttpEntity httpEntity = httpResponse.getEntity();
      if (httpEntity != null) {
        // 打印响应内容

        String restr = EntityUtils.toString(httpEntity, "UTF-8");
        //				System.out.println("response:" + restr);
        Gson gson = new Gson();
        Map<String, Object> requestMap = gson.fromJson(restr, Map.class);
        if (requestMap != null && "10000000".equals(requestMap.get("retcode"))) {
          return true;
        } else {
          consume.setRemark(restr);
        }
      }

      //					closeableHttpClient.close();
    } catch (Exception e) {
      throw new Exception(e);
    } finally {
      try {
        // 释放资源
        closeableHttpClient.close();
      } catch (IOException e) {
      }
    }
    return false;
  }