/**
  * Ensures that the response has a "success" status code {@code 2xx}.
  *
  * @throws HttpResponseException if the response does not have a {@code 2xx} status code
  * @throws IOException if the response entity parsing has failed
  */
 public void ensure2xxStatus() throws HttpResponseException, IOException {
   if (response.getStatus() / 100 != 2) {
     final String message;
     if (MediaType.TEXT_PLAIN_TYPE.equals(response.getMediaType())) {
       message = response.readEntity(String.class);
     } else if (MediaType.TEXT_XML_TYPE.equals(response.getMediaType())
         || MediaType.APPLICATION_XML_TYPE.equals(response.getMediaType())
         || MediaType.APPLICATION_JSON_TYPE.equals(response.getMediaType())) {
       message = response.readEntity(AcknowlegementType.class).getMessage();
     } else {
       message = response.toString();
     }
     throw new HttpResponseException(response.getStatus(), message);
   }
 }
  /**
   * Save CSV or XML file with the storage component
   *
   * @expects "GEMO portal wide params" in portal-ext.properties:
   *     gemo.storage.upload.url=http://?????????/service/storage/upload
   *     gemo.storage.search.query.url=http://?????????/service/storage/ search?query=
   * @param csvXmlFile the file
   * @return the url
   * @throws SystemException the system exception
   * @throws URISyntaxException the uRI syntax exception
   * @throws IOException
   * @throws PortalException
   */
  public static String uploadToStorage(UploadedFile csvXmlFile)
      throws SystemException, URISyntaxException, IOException, PortalException,
          NullPointerException {
    // grab props from constants
    final String storage_uri =
        PrefsPropsUtil.getString(PortalProperties.GEMO_STORAGE_URL, "http://localhost/storage");
    final String upload_uri =
        PrefsPropsUtil.getString(
            PortalProperties.GEMO_STORAGE_UPLOAD_URL, "http://localhost/storage/upload");
    final String upload_base64_uri =
        PrefsPropsUtil.getString(
            PortalProperties.GEMO_STORAGE_UPLOADBASE64_URL,
            "http://localhost:8080/storage/uploadbase64");
    final String query_uri =
        PrefsPropsUtil.getString(
            PortalProperties.GEMO_STORAGE_SEARCH_QUERY_URL,
            "http://localhost/storage/search?query=");

    final String tableName = NameUtils.UsersUsableUniqueNamefromFile(csvXmlFile);

    try {
      byte[] bytes = csvXmlFile.getBytes();
      String theBase64Bytes = Base64.encodeBytes(bytes);

      ResteasyClient client = new ResteasyClientBuilder().build();
      ResteasyWebTarget target = client.target(upload_base64_uri);

      MultipartFormDataOutput mdo = new MultipartFormDataOutput();
      mdo.addFormData("tableName", tableName, MediaType.TEXT_PLAIN_TYPE);
      mdo.addFormData("fileName", csvXmlFile.getName(), MediaType.TEXT_PLAIN_TYPE);
      mdo.addFormData("fileBase64", theBase64Bytes, MediaType.TEXT_PLAIN_TYPE.withCharset("utf-8"));

      GenericEntity<MultipartFormDataOutput> entity =
          new GenericEntity<MultipartFormDataOutput>(mdo) {};
      Response r = target.request().post(Entity.entity(entity, MediaType.MULTIPART_FORM_DATA_TYPE));
      String obj = r.readEntity(String.class);
      LOG.debug("Storage returned: " + obj);
    } catch (ClientProtocolException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      // httpclient.getConnectionManager().shutdown();
    }
    return query_uri + "select * from " + tableName + " LIMIT 10";
  }