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 (res.getStatusCode() == null || res.getResult() == null) {
\r
70 throw new CatalogResourceException(
\r
71 "Deploy Package return null. Response = " + res);
\r
74 if (200 == res.getStatusCode() || 201 == res.getStatusCode()) {
\r
75 DeployPackageResponse response =
\r
76 new Gson().fromJson(res.getResult(), DeployPackageResponse.class);
\r
77 if (response.isSuccess()) {
\r
82 throw new CatalogResourceException(
\r
83 "Deploy Package return fail. Response = " + res.getResult());
\r
84 } catch (FileNotFoundException e1) {
\r
85 throw new CatalogResourceException("Deploy Package failed.", e1);
\r
90 } catch (IOException e1) {
\r
91 LOGGER.error("inputStream close failed !");
\r
97 private static HttpEntity buildRequest(InputStream inputStream, String filePath)
\r
98 throws FileNotFoundException {
\r
99 MultipartEntityBuilder builder = MultipartEntityBuilder.create();
\r
100 builder.seContentType(ContentType.MULTIPART_FORM_DATA);
\r
101 builder.addBinaryBody("file", inputStream, ContentType.APPLICATION_OCTET_STREAM,
\r
102 new File(filePath).getName());
\r
103 return builder.build();
\r
106 @SuppressWarnings("resource")
\r
107 private static InputStream getInputStream(String zipFileLocation, String planFilePath)
\r
108 throws CatalogResourceException {
\r
109 ZipInputStream zin = null;
\r
111 InputStream in = new BufferedInputStream(new FileInputStream(zipFileLocation));
\r
112 zin = new ZipInputStream(in);
\r
114 while ((ze = zin.getNextEntry()) != null) {
\r
115 if (planFilePath.equals(ze.getName())) {
\r
116 ZipFile zf = new ZipFile(zipFileLocation);
\r
117 return zf.getInputStream(ze);
\r
120 } catch (IOException e1) {
\r
121 throw new CatalogResourceException("Get InputStream failed. planFilePath = " + planFilePath,
\r
127 throw new CatalogResourceException("Get InputStream failed. planFilePath = " + planFilePath);
\r
130 private static void closeStream(ZipInputStream zin) {
\r
135 } catch (IOException e1) {
\r
136 LOGGER.error("zip inputStream close failed !");
\r
143 * @param packageName package to delete according packageName
\r
144 * @return DeletePackageResponse
\r
145 * @throws CatalogResourceException e1
\r
147 public static DeletePackageResponse deletePackage(String packageName)
\r
148 throws CatalogResourceException {
\r
150 ClientConfig config = new ClientConfig();
\r
151 Iwso2RestService wso2Proxy = ConsumerFactory.createConsumer(
\r
152 Config.getConfigration().getWso2BaseUrl(), config, Iwso2RestService.class);
\r
153 DeletePackageResponse response = wso2Proxy.deletePackage(packageName);
\r
154 if (response.isSuccess()) {
\r
157 throw new CatalogResourceException(response.getException());
\r
158 } catch (Exception e1) {
\r
159 throw new CatalogResourceException(
\r
160 "Call Delete Package api failed. packageName = " + packageName, e1);
\r
167 * @param processId process id
\r
168 * @param params params
\r
169 * @return StartProcessResponse
\r
170 * @throws CatalogResourceException e1
\r
172 public static StartProcessResponse startProcess(String processId, Map<String, Object> params)
\r
173 throws CatalogResourceException {
\r
175 ClientConfig config = new ClientConfig();
\r
176 Iwso2RestService wso2Proxy = ConsumerFactory.createConsumer(
\r
177 Config.getConfigration().getWso2BaseUrl(), config, Iwso2RestService.class);
\r
178 StartProcessResponse response =
\r
179 wso2Proxy.startProcess(new StartProcessRequest(processId, params));
\r
180 if (response.isSuccess()) {
\r
183 throw new CatalogResourceException(response.getException());
\r
184 } catch (Exception e1) {
\r
185 throw new CatalogResourceException("Call Start Process api failed.", e1);
\r