summaryrefslogtreecommitdiffstats
path: root/wso2bpel-ext/wso2bpel-core/wso2bpel-mgr/src/main/java/org/openo/carbon/bpel/resources/BpsPackage.java
blob: e11c1ebfbb1491aea302e850bc5191257a103fd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
/**
 * Copyright 2016-2017 ZTE Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openo.carbon.bpel.resources;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.QueryParam;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.xml.namespace.QName;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.axis2.transport.http.HttpTransportProperties;
import org.apache.axis2.transport.http.HttpTransportProperties.Authenticator;
import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.glassfish.jersey.media.multipart.FormDataParam;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
import org.wso2.carbon.bpel.stub.upload.types.UploadedFileItem;
import org.openo.carbon.bpel.common.Config;
import org.openo.carbon.bpel.util.JsonUtil;
import org.openo.carbon.bpel.util.Xml2JsonUtil;

import com.codahale.metrics.annotation.Timed;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Path("/")
@Api(tags = {"wso2 bpel api"})
public class BpsPackage {

  private static final Log log = LogFactory.getLog(BpsPackage.class);

  public static final int STATUS_SUCCESS = 1;
  public static final int STATUS_FAIL = 0;
  /**
   * 无错误
   */
  public static final int ERROR_CODE_NOERROR = 0;
  /**
   * 不支持的文件类型
   */
  public static final int ERROR_CODE_PACKAGE_UNSUPPORED_FILE = 10001;
  /**
   * 同名的包正在操作中
   */
  public static final int ERROR_CODE_PACKAGE_STATUS_BUSY = 10002;
  /**
   * 包名重复
   */
  public static final int ERROR_CODE_PACKAGE_DUPLICATED_NAME = 10003;
  /**
   * 未获取到包部署的信息
   */
  public static final int ERROR_CODE_PACKAGE_DEPLOY_INFORMATION_IS_LOST = 10004;
  /**
   * 卸载包失败
   */
  public static final int ERROR_CODE_PACKAGE_UNDEPLOY_FAILED = 10005;
  /**
   * 包不存在
   */
  public static final int ERROR_CODE_PACKAGE_NOTEXISTS = 10006;
  /**
   * 服务运行时异常
   */
  public static final int ERROR_CODE_RUNTIME_EXCEPTION = 20001;
  /**
   * Axis运行时异常
   */
  public static final int ERROR_CODE_RUNTIME_EXCEPTION_AXIS = 20002;
  /**
   * IO运行时异常
   */
  public static final int ERROR_CODE_RUNTIME_EXCEPTION_IO = 20003;

  public static Set<String> packageNameSet = new HashSet<String>();

  private Map<String, String> configMap = null;

  @SuppressWarnings("unused")
  private final AtomicLong counter;

  public BpsPackage() {
    this.counter = new AtomicLong();
  }

  private synchronized String getConfig(String key) {
    if (configMap == null) {
      configMap = new HashMap<String, String>();
      String uploadFilePath = Config.getConfigration().getWso2UploadFilePath();
      String jksFile = Config.getConfigration().getWso2SslJksFile();
      String trustStorePassword = Config.getConfigration().getWso2SslJksPassword();
      String httpUsername = Config.getConfigration().getWso2AuthUserName();
      String httpPassword = Config.getConfigration().getWso2AuthPassword();
      String host = Config.getConfigration().getWso2Host();
      String port = Config.getConfigration().getWso2HostPort();
      configMap.put("uploadFilePath", uploadFilePath);
      configMap.put("jksFile", jksFile);
      configMap.put("trustStorePassword", trustStorePassword);
      configMap.put("httpUsername", httpUsername);
      configMap.put("httpPassword", httpPassword);
      configMap.put("host", host);
      configMap.put("port", port);
    }
    if (configMap.containsKey(key)) {
      return configMap.get(key);
    } else {
      return "";
    }
  }

  public static boolean lockPackageName(String packageName) {
    boolean succeed = false;
    synchronized (packageNameSet) {
      if (!packageNameSet.contains(packageName)) {
        packageNameSet.add(packageName);
        succeed = true;
      }
    }
    return succeed;
  }

  public static boolean unlockPackageName(String packageName) {
    boolean succeed = false;
    synchronized (packageNameSet) {
      if (packageNameSet.contains(packageName)) {
        packageNameSet.remove(packageName);
        succeed = true;
      }
    }
    return succeed;
  }

  @POST
  @Path(value = "package")
  @Consumes(MediaType.MULTIPART_FORM_DATA)
  @Produces(value = MediaType.APPLICATION_JSON)
  @ApiOperation(value = "package process", response = Map.class)
  @Timed
  public Map<String, Object> uploadFile(@FormDataParam("filename") String filename,
      @FormDataParam("file") InputStream fileInputStream,
      @FormDataParam("file") FormDataContentDisposition fileDetail,
      @Context HttpServletRequest request, @Context HttpServletResponse response)
          throws IOException {
    Map<String, Object> map = new LinkedHashMap<String, Object>();
    String errorMessage = "unkown";
    int errorCode = ERROR_CODE_NOERROR;

    String fileName = fileDetail.getFileName();
    String fullName = getConfig("uploadFilePath") + "/" + fileName;
    File file = new File(fullName);
    String packageName = null;
    try {
      if (fileName.endsWith(".zip")) {
        packageName = fileName.substring(0, fileName.length() - 4);
      } else {
    	  errorCode = ERROR_CODE_PACKAGE_UNSUPPORED_FILE;
        throw new Exception("Only support *.zip file.");
      }
      if (!lockPackageName(packageName)) {
    	  errorCode = ERROR_CODE_PACKAGE_STATUS_BUSY;
        throw new Exception("Package " + packageName + " is operating.");
      }
      FileUtils.copyInputStreamToFile(fileInputStream, file);
      System.setProperty("javax.net.ssl.trustStore", "*.keystore");
      System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
      System.setProperty("javax.net.ssl.trustStore", getConfig("jksFile"));
      System.setProperty("javax.net.ssl.trustStorePassword", getConfig("trustStorePassword"));

      HttpTransportProperties.Authenticator authenticator =
          new HttpTransportProperties.Authenticator();
      List<String> auth = new ArrayList<String>();
      auth.add(Authenticator.BASIC);
      authenticator.setAuthSchemes(auth);
      authenticator.setUsername(getConfig("httpUsername"));
      authenticator.setPassword(getConfig("httpPassword"));
      authenticator.setPreemptiveAuthentication(true);

      map.putAll(bpsDeployPackage(fileName, fullName, packageName, authenticator));
      map.put("status", STATUS_SUCCESS);
      map.put("message", "success");
      return map;
    } catch (AxisFault e) {
      errorMessage = e.getLocalizedMessage();
      errorCode = ERROR_CODE_RUNTIME_EXCEPTION_AXIS;
      log.error(e.getMessage(), e);
      e.printStackTrace();
    } catch (IOException e) {
      errorMessage = e.getLocalizedMessage();
      errorCode = ERROR_CODE_RUNTIME_EXCEPTION_IO;
      log.error(e.getMessage(), e);
      e.printStackTrace();
    } catch (Throwable e) {
      errorMessage = e.getLocalizedMessage();
		if (e instanceof BpsServiceException) {
			errorCode = ((BpsServiceException) e).getErrorCode();
		} else {
			errorCode = ERROR_CODE_RUNTIME_EXCEPTION;
		}
      log.error(e.getMessage(), e);
      e.printStackTrace();
    } finally {
      if (packageName != null) {
        unlockPackageName(packageName);
      }
    }
    map.put("errorCode", errorCode);
    map.put("status", STATUS_FAIL);
    map.put("message", errorMessage);
    return map;
  }

  @SuppressWarnings("rawtypes")
  private Map<String, Object> bpsDeployPackage(String fileName, String fullName, String packageName,
      HttpTransportProperties.Authenticator authenticator)
          throws JsonParseException, JsonMappingException, IOException, AxisFault, Exception {
    Map<String, Object> map = new HashMap<String, Object>();
    Map deployedPackageInfoMap = getDeployedPackageInfo(authenticator, packageName, "");
    if (deployedPackageInfoMap.get("packageName") != null) {
      throw new BpsServiceException(ERROR_CODE_PACKAGE_DUPLICATED_NAME,"Package " + deployedPackageInfoMap.get("packageName")
          + " exist, please undeploy it first.");
    }
    OMElement element = deployPackage(fileName, fullName, authenticator);
    System.out.println(Xml2JsonUtil.xml2JSON(element.toString()));

    long timeout = 60 * 1000L;
    long timeStart = System.currentTimeMillis();
    while (System.currentTimeMillis() - timeStart < timeout) {
      deployedPackageInfoMap = getDeployedPackageInfo(authenticator, packageName, "");
      if (deployedPackageInfoMap.get("packageName") == null) {
        Thread.sleep(2000);
      } else {
        break;
      }
    }
    if (deployedPackageInfoMap.get("packageName") == null) {
      throw new BpsServiceException(ERROR_CODE_PACKAGE_DEPLOY_INFORMATION_IS_LOST,
          "Package " + packageName + " deploy failed or deploy information is lost.");
    }
    map.put("packageName", deployedPackageInfoMap.get("packageName"));
    map.put("processId", deployedPackageInfoMap.get("pid"));
    return map;
  }

  private OMElement deployPackage(String fileName, String fullName,
      HttpTransportProperties.Authenticator authenticator) throws AxisFault {
    String url =
        "https://" + getConfig("host") + ":" + getConfig("port") + "/services/BPELUploader?wsdl";
    RPCServiceClient serviceClient = new RPCServiceClient();
    EndpointReference targetEPR = new EndpointReference(url);
    Options options = serviceClient.getOptions();
    options.setTo(targetEPR);
    options.setAction("urn:uploadService");
    options.setProperty(HTTPConstants.SO_TIMEOUT, new Integer(300000));
    options.setProperty(HTTPConstants.AUTHENTICATE, authenticator);
    serviceClient.setOptions(options);

    QName qname = new QName("http://services.deployer.bpel.carbon.wso2.org", "uploadService");
    UploadedFileItem fileItem = new UploadedFileItem();
    fileItem.setFileName(fileName);
    fileItem.setFileType("zip");

    DataSource dataSource = new FileDataSource(fullName);
    fileItem.setDataHandler(new DataHandler(dataSource));
    UploadedFileItem[] parameters = new UploadedFileItem[] {fileItem};
    OMElement element = serviceClient.invokeBlocking(qname, parameters);
    return element;
  }

  @Path(value = "package/{packageName}")
  @DELETE
  @Produces(value = MediaType.APPLICATION_JSON)
  @ApiOperation(value = "delete", response = Map.class)
  @Timed
  public Map<String, Object> delete(@PathParam("packageName") String packageName,
      @Context HttpServletRequest request) {
    Map<String, Object> map = new LinkedHashMap<String, Object>();
    String errorMessage = "unkown";
    int errorCode = ERROR_CODE_NOERROR;
    try {
      if (!lockPackageName(packageName)) {
        throw new Exception("Package " + packageName + " is operating.");
      }
      System.setProperty("javax.net.ssl.trustStore", "*.keystore");
      System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
      System.setProperty("javax.net.ssl.trustStore", getConfig("jksFile"));
      System.setProperty("javax.net.ssl.trustStorePassword", getConfig("trustStorePassword"));

      HttpTransportProperties.Authenticator authenticator =
          new HttpTransportProperties.Authenticator();
      List<String> auth = new ArrayList<String>();
      auth.add(Authenticator.BASIC);
      authenticator.setAuthSchemes(auth);
      authenticator.setUsername(getConfig("httpUsername"));
      authenticator.setPassword(getConfig("httpPassword"));
      authenticator.setPreemptiveAuthentication(true);

      map.putAll(bpsUndeployPackage(packageName, authenticator));

      return map;
    } catch (AxisFault e) {
      errorMessage = e.getLocalizedMessage();
      errorCode = ERROR_CODE_RUNTIME_EXCEPTION_AXIS;
      log.error(e.getMessage(), e);
      e.printStackTrace();
    } catch (Throwable e) {
      errorMessage = e.getLocalizedMessage();
		if (e instanceof BpsServiceException) {
			errorCode = ((BpsServiceException) e).getErrorCode();
		} else {
			errorCode = ERROR_CODE_RUNTIME_EXCEPTION;
		}
      log.error(e.getMessage(), e);
      e.printStackTrace();
    } finally {
      if (packageName != null) {
        unlockPackageName(packageName);
      }
    }
    map.put("errorCode", errorCode);
    map.put("status", STATUS_FAIL);
    map.put("message", errorMessage);
    return map;
  }

  @SuppressWarnings({"rawtypes", "unused"})
  private Map<String, Object> bpsUndeployPackage(String packageName,
      HttpTransportProperties.Authenticator authenticator)
          throws JsonParseException, JsonMappingException, IOException, AxisFault, Exception {
    Map<String, Object> map = new HashMap<String, Object>();
    Map deployedPackageInfoMap = getDeployedPackageInfo(authenticator, packageName, "");
    if (deployedPackageInfoMap.get("packageName") == null) {
      throw new BpsServiceException(ERROR_CODE_PACKAGE_NOTEXISTS,"Package " + deployedPackageInfoMap.get("packageName")
          + " does not exist, please deploy it first.");
    }
    OMElement element = undeployPackage(authenticator, packageName);
    deployedPackageInfoMap = getDeployedPackageInfo(authenticator, packageName, "");
    if (deployedPackageInfoMap.get("packageName") != null) {
      throw new BpsServiceException(ERROR_CODE_PACKAGE_UNDEPLOY_FAILED,
          "Package " + deployedPackageInfoMap.get("packageName") + " undeploy failed.");
    }
    map.put("status", STATUS_SUCCESS);
    map.put("message", "success");

    return map;
  }

  private OMElement undeployPackage(HttpTransportProperties.Authenticator authenticator,
      String packageName) throws AxisFault {
    String url = "https://" + getConfig("host") + ":" + getConfig("port")
        + "/services/BPELPackageManagementService?wsdl";
    RPCServiceClient serviceClient = new RPCServiceClient();
    EndpointReference targetEPR = new EndpointReference(url);
    Options options = serviceClient.getOptions();
    options.setTo(targetEPR);
    options.setAction("sch:undeployBPELPackage");
    options.setProperty(HTTPConstants.SO_TIMEOUT, new Integer(300000));
    options.setProperty(HTTPConstants.AUTHENTICATE, authenticator);
    serviceClient.setOptions(options);
    OMFactory fac = OMAbstractFactory.getOMFactory();
    OMNamespace omNs = fac.createOMNamespace("http://wso2.org/bps/management/schema", "sch");
    OMElement method = fac.createOMElement("undeployBPELPackage", omNs);
    OMElement content = fac.createOMElement("package", omNs);
    content.addChild(fac.createOMText(content, packageName));
    method.addChild(content);
    method.build();
    OMElement res = serviceClient.sendReceive(method);
    return res;
  }
  
  @SuppressWarnings({"rawtypes", "unchecked"})
  private Map getFullDeployedPackageInfo(HttpTransportProperties.Authenticator authenticator,
	      String packageName, String page) throws Exception {
	    String result;
	    String jsonTemplate =
	        "{'listDeployedPackagesPaginated':{'page':'${page}','packageSearchString':'${searchString}'}}";
	    Map jsonParamMap = new HashMap();
	    jsonParamMap.put("page", page);
	    jsonParamMap.put("searchString", packageName);
	    Object params = getParams(jsonTemplate, jsonParamMap);
	    result = BpsProcess.invokeWsdl("https://" + getConfig("host") + ":" + getConfig("port")
	        + "/services/BPELPackageManagementService?wsdl", params, authenticator);
	    Map<String, Object> processMap = JsonUtil.json2Bean(result, Map.class);
	    return processMap;
	  }

  @SuppressWarnings({"rawtypes", "unchecked"})
  private Map getDeployedPackageInfo(HttpTransportProperties.Authenticator authenticator,
	      String packageName, String page) throws Exception {
	    Map resultMap = new HashMap();
	    Map<String, Object> processMap = getFullDeployedPackageInfo(authenticator, packageName, page);
	    Object packages = ((Map) processMap.get("deployedPackagesPaginated")).get("package");
	    Map deployedPackage = null;
	    if (packages instanceof List) {
	      for (Iterator iter = ((List) packages).iterator(); iter.hasNext();) {
	        Map packageMap = (Map) iter.next();
	        String deployedPackageName = (String) packageMap.get("name");
	        if (deployedPackageName.equals(packageName)) {
	          deployedPackage = packageMap;
	          break;
	        }
	      }
	    } else if (packages instanceof Map) {
	      String deployedPackageName = (String) ((Map) packages).get("name");
	      if (deployedPackageName.equals(packageName)) {
	        deployedPackage = (Map) packages;
	      }
	    }
	    if (deployedPackage != null) {
	      String fullPackageName = null;
	      String pid = null;
	      Object versions = ((Map) deployedPackage.get("versions")).get("version");
	      Map lastestVersion = null;
	      if (versions instanceof List) {
	        for (Iterator iter = ((List) versions).iterator(); iter.hasNext();) {
	          Map version = (Map) iter.next();
	          if (version.get("isLatest").equals("true")) {
	            lastestVersion = version;
	          }
	        }
	      } else if (versions instanceof Map) {
	        lastestVersion = (Map) versions;
	      }
	      fullPackageName = (String) ((Map) ((Map) lastestVersion.get("processes")).get("process"))
	          .get("packageName");
	      pid = (String) ((Map) ((Map) lastestVersion.get("processes")).get("process")).get("pid");
	      resultMap.put("packageName", fullPackageName);
	      resultMap.put("pid", pid);
	    }
	    return resultMap;
}


  @SuppressWarnings({"rawtypes", "unchecked"})
  public static Map<String, Object> getParams(String jsonTemplate, Map<String, Object> paramMap)
      throws JsonParseException, JsonMappingException, IOException {
    String json = jsonTemplate.replaceAll("'", "\"");
		for (Iterator iter = paramMap.keySet().iterator(); iter.hasNext();) {
			String key = (String) iter.next();
			String value = "";
			if (paramMap.get(key) != null) {
				value = paramMap.get(key).toString().replaceAll("\"", "\\\\\"");
			}
			json = json.replaceAll("\\$\\{" + key + "\\}", value);
		}
    return JsonUtil.json2Bean(json, Map.class);
  }
  
  @GET
  @Path(value = "listPackages")
  @Produces(value = MediaType.APPLICATION_JSON)
  @ApiOperation(value = "list packages", response = Map.class)
  @Timed
  public Map<String, Object> listPackages(@QueryParam("page") String page,
	      @QueryParam("searchString") String searchString, @Context HttpServletRequest request)
          throws IOException {
    Map<String, Object> map = new LinkedHashMap<String, Object>();
    String errorMessage = "unkown";
    int errorCode = ERROR_CODE_NOERROR;
    String packageName = null;
    try {
      System.setProperty("javax.net.ssl.trustStore", "*.keystore");
      System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
      System.setProperty("javax.net.ssl.trustStore", getConfig("jksFile"));
      System.setProperty("javax.net.ssl.trustStorePassword", getConfig("trustStorePassword"));

      HttpTransportProperties.Authenticator authenticator =
          new HttpTransportProperties.Authenticator();
      List<String> auth = new ArrayList<String>();
      auth.add(Authenticator.BASIC);
      authenticator.setAuthSchemes(auth);
      authenticator.setUsername(getConfig("httpUsername"));
      authenticator.setPassword(getConfig("httpPassword"));
      authenticator.setPreemptiveAuthentication(true);

      map.putAll(getFullDeployedPackageInfo(authenticator,searchString,page));

      map.put("status", STATUS_SUCCESS);
      map.put("message", "success");
      return map;
    } catch (AxisFault e) {
      errorMessage = e.getLocalizedMessage();
      errorCode = ERROR_CODE_RUNTIME_EXCEPTION_AXIS;
      log.error(e.getMessage(), e);
      e.printStackTrace();
    } catch (IOException e) {
      errorMessage = e.getLocalizedMessage();
      errorCode = ERROR_CODE_RUNTIME_EXCEPTION_IO;
      log.error(e.getMessage(), e);
      e.printStackTrace();
    } catch (Throwable e) {
			if (e instanceof BpsServiceException) {
				errorCode = ((BpsServiceException) e).getErrorCode();
			} else {
				errorCode = ERROR_CODE_RUNTIME_EXCEPTION;
			}
      errorMessage = e.getLocalizedMessage();
      log.error(e.getMessage(), e);
      e.printStackTrace();
    } finally {
      if (packageName != null) {
        unlockPackageName(packageName);
      }
    }
    map.put("errorCode", errorCode);
    map.put("status", STATUS_FAIL);
    map.put("message", errorMessage);
    return map;
  }

  @SuppressWarnings("rawtypes")
  public static void main(String[] args) {
    try {

      boolean remoteDebug = true;
      boolean localDebug = false;
      if (remoteDebug) {
        Client client = ClientBuilder.newBuilder().register(MultiPartFeature.class).build();
        WebTarget target = client.target("http://localhost:8101/openoapi/wso2bpel/v1/package");
        FileDataBodyPart bodyPart = new FileDataBodyPart("file",
            new File("F:\\wso2bps-3.5.1\\wso2bps-3.5.1\\repository\\samples\\bpel\\Alarm.zip"));
        FormDataMultiPart formDataMultiPart = new FormDataMultiPart();
        formDataMultiPart.field("fileName", "Alarm.zip").bodyPart(bodyPart);
        String result = target.request(MediaType.APPLICATION_JSON)
            .post(Entity.entity(formDataMultiPart, formDataMultiPart.getMediaType()), String.class);
        System.out.println(result);
      }
      //
      // System.out.println("************************************************************************");
      //
      // bodyPart = new FileDataBodyPart("file", new
      // File("D:\\temp\\bpel-sample\\AssignDate\\AssignDate.zip"));
      // formDataMultiPart = new FormDataMultiPart();
      // formDataMultiPart.field("fileName",
      // "AssignDate.zip").bodyPart(bodyPart);
      // result = target.request(MediaType.APPLICATION_JSON)
      // .post(Entity.entity(formDataMultiPart,
      // formDataMultiPart.getMediaType()), String.class);
      // System.out.println(result);
      //
      if (localDebug) {
        System.setProperty("javax.net.ssl.trustStore", "*.keystore");
        System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
        System.setProperty("javax.net.ssl.trustStore",
            "D:\\software\\WSO2\\wso2bps-3.5.1\\repository\\resources\\security\\wso2carbon.jks");
        System.setProperty("javax.net.ssl.trustStorePassword", "wso2carbon");
        HttpTransportProperties.Authenticator authenticator =
            new HttpTransportProperties.Authenticator();
        List<String> auth = new ArrayList<String>();
        auth.add(Authenticator.BASIC);
        authenticator.setAuthSchemes(auth);
        authenticator.setUsername("admin");
        authenticator.setPassword("admin");
        authenticator.setPreemptiveAuthentication(true);
        //
        // String packageName = "RESTProcess";
        // Map deployedPackageInfoMap =
        // getDeployedPackageInfo(authenticator, packageName);

        BpsPackage packageTest = new BpsPackage();
        Map map;
        // map = packageTest.bpsUndeployPackage("AssignDate",
        // authenticator);
        // System.out.println(JsonUtil.bean2Json(map));
        map = packageTest.bpsDeployPackage("AssignDate.zip",
            "D:\\temp\\bpel-sample\\AssignDate\\AssignDate.zip", "AssignDate", authenticator);
        System.out.println(JsonUtil.bean2Json(map));
        map = packageTest.bpsUndeployPackage("AssignDate", authenticator);
        System.out.println(JsonUtil.bean2Json(map));
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}