PackageManagerJsonCall.java

  1. /*
  2.  * #%L
  3.  * wcm.io
  4.  * %%
  5.  * Copyright (C) 2014 wcm.io
  6.  * %%
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *      http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  * #L%
  19.  */
  20. package io.wcm.tooling.commons.packmgr.httpaction;

  21. import java.io.IOException;

  22. import org.apache.http.HttpStatus;
  23. import org.apache.http.client.methods.CloseableHttpResponse;
  24. import org.apache.http.client.methods.HttpRequestBase;
  25. import org.apache.http.client.protocol.HttpClientContext;
  26. import org.apache.http.impl.client.CloseableHttpClient;
  27. import org.apache.http.util.EntityUtils;
  28. import org.json.JSONException;
  29. import org.json.JSONObject;
  30. import org.slf4j.Logger;
  31. import org.slf4j.LoggerFactory;

  32. import io.wcm.tooling.commons.packmgr.PackageManagerHttpActionException;

  33. /**
  34.  * Call to package manager HTTP JSON interface.
  35.  */
  36. public final class PackageManagerJsonCall implements HttpCall<JSONObject> {

  37.   private final CloseableHttpClient httpClient;
  38.   private final HttpClientContext context;
  39.   private final HttpRequestBase method;

  40.   private static final Logger log = LoggerFactory.getLogger(PackageManagerJsonCall.class);

  41.   /**
  42.    * @param httpClient HTTP client
  43.    * @param context HTTP client context
  44.    * @param method HTTP method
  45.    */
  46.   public PackageManagerJsonCall(CloseableHttpClient httpClient, HttpClientContext context, HttpRequestBase method) {
  47.     this.httpClient = httpClient;
  48.     this.context = context;
  49.     this.method = method;
  50.   }

  51.   @Override
  52.   public JSONObject execute() {
  53.     log.debug("Call URL: {}", method.getURI());

  54.     try (CloseableHttpResponse response = httpClient.execute(method, context)) {
  55.       JSONObject jsonResponse = null;

  56.       String responseString = EntityUtils.toString(response.getEntity());
  57.       if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

  58.         // get response JSON
  59.         if (responseString != null) {
  60.           try {
  61.             jsonResponse = new JSONObject(responseString);
  62.           }
  63.           catch (JSONException ex) {
  64.             throw PackageManagerHttpActionException.forJSONException(method.getURI().toString(), responseString, ex);
  65.           }
  66.         }
  67.         if (jsonResponse == null) {
  68.           jsonResponse = new JSONObject();
  69.           jsonResponse.put("success", false);
  70.           jsonResponse.put("msg", "Invalid response (null).");
  71.         }

  72.       }
  73.       else {
  74.         throw PackageManagerHttpActionException.forHttpError(method.getURI().toString(), response.getStatusLine(), responseString);
  75.       }

  76.       return jsonResponse;
  77.     }
  78.     catch (IOException ex) {
  79.       throw PackageManagerHttpActionException.forIOException(method.getURI().toString(), ex);
  80.     }
  81.   }

  82. }