2  * Copyright 2016 [ZTE] and others.
\r 
   4  * Licensed under the Apache License, Version 2.0 (the "License");
\r 
   5  * you may not use this file except in compliance with the License.
\r 
   6  * You may obtain a copy of the License at
\r 
   8  *     http://www.apache.org/licenses/LICENSE-2.0
\r 
  10  * Unless required by applicable law or agreed to in writing, software
\r 
  11  * distributed under the License is distributed on an "AS IS" BASIS,
\r 
  12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r 
  13  * See the License for the specific language governing permissions and
\r 
  14  * limitations under the License.
\r 
  17 package org.openo.commontosca.catalog.model.plan.wso2;
\r 
  19 import com.google.gson.Gson;
\r 
  21 import com.eclipsesource.jaxrs.consumer.ConsumerFactory;
\r 
  23 import org.apache.http.HttpEntity;
\r 
  24 import org.apache.http.entity.ContentType;
\r 
  25 import org.apache.http.entity.mime.MultipartEntityBuilder;
\r 
  26 import org.glassfish.jersey.client.ClientConfig;
\r 
  27 import org.openo.commontosca.catalog.common.Config;
\r 
  28 import org.openo.commontosca.catalog.db.exception.CatalogResourceException;
\r 
  29 import org.openo.commontosca.catalog.model.plan.wso2.entity.DeletePackageResponse;
\r 
  30 import org.openo.commontosca.catalog.model.plan.wso2.entity.DeployPackageResponse;
\r 
  31 import org.openo.commontosca.catalog.model.plan.wso2.entity.StartProcessRequest;
\r 
  32 import org.openo.commontosca.catalog.model.plan.wso2.entity.StartProcessResponse;
\r 
  33 import org.slf4j.Logger;
\r 
  34 import org.slf4j.LoggerFactory;
\r 
  36 import java.io.BufferedInputStream;
\r 
  37 import java.io.File;
\r 
  38 import java.io.FileInputStream;
\r 
  39 import java.io.FileNotFoundException;
\r 
  40 import java.io.IOException;
\r 
  41 import java.io.InputStream;
\r 
  42 import java.util.Map;
\r 
  43 import java.util.zip.ZipEntry;
\r 
  44 import java.util.zip.ZipFile;
\r 
  45 import java.util.zip.ZipInputStream;
\r 
  49 public class Wso2ServiceConsumer {
\r 
  50   public static final String WSO2_APP_URL = "/openoapi/wso2bpel/v1/package";
\r 
  51   private static final Logger LOGGER = LoggerFactory.getLogger(Wso2ServiceConsumer.class);
\r 
  55    * @param zipFileLocation zip file location
\r 
  56    * @param planFilePath plan file path
\r 
  57    * @return DeployPackageResponse
\r 
  58    * @throws CatalogResourceException e1
\r 
  60   public static DeployPackageResponse deployPackage(String zipFileLocation, String planFilePath)
\r 
  61       throws CatalogResourceException {
\r 
  62     InputStream ins = null;
\r 
  64       ins = getInputStream(zipFileLocation, planFilePath);
\r 
  65       RestResponse res = RestfulClient.post(Config.getConfigration().getWso2HostIp(),
\r 
  66           Integer.parseInt(Config.getConfigration().getWso2HostPort()), WSO2_APP_URL,
\r 
  67           buildRequest(ins, planFilePath));
\r 
  69       if (200 == res.getStatusCode() || 201 == res.getStatusCode()) {
\r 
  70         DeployPackageResponse response =
\r 
  71             new Gson().fromJson(res.getResult(), DeployPackageResponse.class);
\r 
  72         if (response.isSuccess()) {
\r 
  77       throw new CatalogResourceException(
\r 
  78           "Deploy Package return fail. Response = " + res.getResult());
\r 
  79     } catch (FileNotFoundException e1) {
\r 
  80       throw new CatalogResourceException("Deploy Package failed.", e1);
\r 
  85         } catch (IOException e1) {
\r 
  86           LOGGER.error("inputStream close failed !");
\r 
  92   private static HttpEntity buildRequest(InputStream inputStream, String filePath)
\r 
  93       throws FileNotFoundException {
\r 
  94     MultipartEntityBuilder builder = MultipartEntityBuilder.create();
\r 
  95     builder.seContentType(ContentType.MULTIPART_FORM_DATA);
\r 
  96     builder.addBinaryBody("file", inputStream, ContentType.APPLICATION_OCTET_STREAM,
\r 
  97         new File(filePath).getName());
\r 
  98     return builder.build();
\r 
 101   @SuppressWarnings("resource")
\r 
 102   private static InputStream getInputStream(String zipFileLocation, String planFilePath)
\r 
 103       throws CatalogResourceException {
\r 
 104     ZipInputStream zin = null;
\r 
 106       InputStream in = new BufferedInputStream(new FileInputStream(zipFileLocation));
\r 
 107       zin = new ZipInputStream(in);
\r 
 109       while ((ze = zin.getNextEntry()) != null) {
\r 
 110         if (planFilePath.equals(ze.getName())) {
\r 
 111           ZipFile zf = new ZipFile(zipFileLocation);
\r 
 112           return zf.getInputStream(ze);
\r 
 115     } catch (IOException e1) {
\r 
 116       throw new CatalogResourceException("Get InputStream failed. planFilePath = " + planFilePath,
\r 
 122     throw new CatalogResourceException("Get InputStream failed. planFilePath = " + planFilePath);
\r 
 125   private static void closeStream(ZipInputStream zin) {
\r 
 130       } catch (IOException e1) {
\r 
 131         LOGGER.error("zip inputStream close failed !");
\r 
 138    * @param packageName package to delete according packageName
\r 
 139    * @return DeletePackageResponse
\r 
 140    * @throws CatalogResourceException e1
\r 
 142   public static DeletePackageResponse deletePackage(String packageName)
\r 
 143       throws CatalogResourceException {
\r 
 145       ClientConfig config = new ClientConfig();
\r 
 146       Iwso2RestService wso2Proxy = ConsumerFactory.createConsumer(
\r 
 147           Config.getConfigration().getWso2BaseUrl(), config, Iwso2RestService.class);
\r 
 148       DeletePackageResponse response = wso2Proxy.deletePackage(packageName);
\r 
 149       if (response.isSuccess()) {
\r 
 152       throw new CatalogResourceException(response.getException());
\r 
 153     } catch (Exception e1) {
\r 
 154       throw new CatalogResourceException(
\r 
 155           "Call Delete Package api failed. packageName = " + packageName, e1);
\r 
 162    * @param processId process id
\r 
 163    * @param params params
\r 
 164    * @return StartProcessResponse
\r 
 165    * @throws CatalogResourceException e1
\r 
 167   public static StartProcessResponse startProcess(String processId, Map<String, Object> params)
\r 
 168       throws CatalogResourceException {
\r 
 170       ClientConfig config = new ClientConfig();
\r 
 171       Iwso2RestService wso2Proxy = ConsumerFactory.createConsumer(
\r 
 172           Config.getConfigration().getWso2BaseUrl(), config, Iwso2RestService.class);
\r 
 173       StartProcessResponse response =
\r 
 174           wso2Proxy.startProcess(new StartProcessRequest(processId, params));
\r 
 175       if (response.isSuccess()) {
\r 
 178       throw new CatalogResourceException(response.getException());
\r 
 179     } catch (Exception e1) {
\r 
 180       throw new CatalogResourceException("Call Start Process api failed.", e1);
\r