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