Ejemplo n.º 1
0
  /*
   * Send a request to a locally running Cougar container via SOAP as per the
   * passed parameters.
   *
   * @param message
   * @param serviceName
   * @param version
   * @param httpBean
   * @return
   */
  public HttpResponseBean makeCougarSOAPCall(
      SOAPMessage message, String serviceName, String version, HttpCallBean httpBean) {
    try {

      // Debugging code
      ByteArrayOutputStream outStream = new ByteArrayOutputStream();
      message.writeTo(outStream);
      SOAPConnectionFactory connectionFactory = SOAPConnectionFactory.newInstance();
      SOAPConnection connection = connectionFactory.createConnection();

      // this can either be a SOAPException or SOAPMessage
      HttpResponseBean responseBean = new HttpResponseBean();
      Object response;

      String host = httpBean.getHost();
      String port = httpBean.getPort();

      String endPoint = "http://" + host + ":" + port + "/" + serviceName + "Service/" + version;

      try {

        response = connection.call(message, endPoint);

      } catch (SOAPException e) {
        response = e;
      } finally {
        connection.close();
      }

      responseBean.setResponseObject(handleResponse(response, responseBean));

      return responseBean;

    } catch (SOAPException | IOException | ParserConfigurationException | TransformerException e) {
      throw new RuntimeException(SOAP_CALL_TEXT + e, e);
    }
  }
Ejemplo n.º 2
0
  /*
   * Create and return a HttpMethodBase for a Rest request based on the passed
   * HttpCallBean and CougarMessageProtocolRequestTypeEnum.
   *
   * @param httpCallBean
   * @param protocolRequestType
   * @return
   */
  public HttpUriRequest getRestMethod(
      HttpCallBean httpCallBean, CougarMessageProtocolRequestTypeEnum protocolRequestType) {

    Object postQueryObject = httpCallBean.getPostQueryObjectsByEnum(protocolRequestType);
    String postQuery;

    if (postQueryObject == null) {
      postQuery = null;
    } else {
      postQuery = (String) postQueryObject;
    }

    String serviceExtension = httpCallBean.getServiceExtension();
    String version = httpCallBean.getVersion();
    Map<String, String> queryParams = httpCallBean.getQueryParams();

    String host = httpCallBean.getHost();
    String port = httpCallBean.getPort();
    String path = httpCallBean.getPath();
    String altURL =
        httpCallBean
            .getAlternativeURL(); // Will be "" for standard URL, or "/www" for alternative URL
    boolean batchedQuery = httpCallBean.getJSONRPC();
    String fullPath = httpCallBean.getFullPath();

    String methodURL = "";

    if (batchedQuery) { // If request is a batched JSON request set the URL to the appropriate
                        // baseline URL
      postQuery =
          createBatchedPostString(
              httpCallBean
                  .getBatchedRequests()); // Build the post string out of the list of requests to
                                          // batch
      methodURL = "http://" + host + ":" + port + "/json-rpc";
    } else { // else build the request URL

      String queryParamString = "";
      if (queryParams != null) {
        int counter = 0;
        StringBuilder queryBuff = new StringBuilder();
        for (Map.Entry<String, String> entry : queryParams.entrySet()) {
          String value = entry.getValue();
          String key = entry.getKey();
          if (counter == 0) {
            String firstQuery = "?" + key + "=" + value;
            queryBuff.append(firstQuery);
          } else {
            String nextQuery = "&" + key + "=" + value;
            queryBuff.append(nextQuery);
          }
          counter++;
        }
        queryParamString = queryBuff.toString();
      }

      if (fullPath != null) {
        methodURL = "http://" + host + ":" + port + fullPath + queryParamString;
      } else {
        methodURL =
            "http://"
                + host
                + ":"
                + port
                + altURL
                + "/"
                + serviceExtension
                + "/"
                + version
                + "/"
                + path
                + queryParamString;
      }
    }

    //        if (logger.isDebugEnabled()) {
    //        }

    if ((postQuery != null) && (!postQuery.equalsIgnoreCase(""))) {
      HttpPost method = new HttpPost(methodURL);
      try {
        method.setEntity(new StringEntity(postQuery, null, UTF8));
        return method;
      } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
      }
    } else {
      return new HttpGet(methodURL);
    }
  }