diff options
Diffstat (limited to 'catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/plan/wso2/Wso2ServiceConsumer.java')
-rw-r--r-- | catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/plan/wso2/Wso2ServiceConsumer.java | 67 |
1 files changed, 19 insertions, 48 deletions
diff --git a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/plan/wso2/Wso2ServiceConsumer.java b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/plan/wso2/Wso2ServiceConsumer.java index 1d753744..67f9a9e3 100644 --- a/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/plan/wso2/Wso2ServiceConsumer.java +++ b/catalog-core/catalog-mgr/src/main/java/org/openo/commontosca/catalog/model/plan/wso2/Wso2ServiceConsumer.java @@ -15,9 +15,7 @@ */ package org.openo.commontosca.catalog.model.plan.wso2; -import java.io.BufferedInputStream; import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; @@ -26,7 +24,6 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; -import java.util.zip.ZipInputStream; import lombok.AllArgsConstructor; import lombok.Data; @@ -38,6 +35,7 @@ import org.apache.http.entity.mime.MultipartEntityBuilder; import org.glassfish.jersey.client.ClientConfig; import org.openo.commontosca.catalog.common.Config; import org.openo.commontosca.catalog.db.exception.CatalogResourceException; +import org.openo.commontosca.catalog.model.common.TemplateUtils; import org.openo.commontosca.catalog.model.plan.wso2.entity.DeletePackageResponse; import org.openo.commontosca.catalog.model.plan.wso2.entity.DeployPackageResponse; import org.openo.commontosca.catalog.model.plan.wso2.entity.StartProcessRequest; @@ -49,10 +47,9 @@ import com.eclipsesource.jaxrs.consumer.ConsumerFactory; import com.google.gson.Gson; - public class Wso2ServiceConsumer { public static final String WSO2_APP_URL = "/openoapi/wso2bpel/v1/package"; - private static final Logger LOGGER = LoggerFactory.getLogger(Wso2ServiceConsumer.class); + private static final Logger logger = LoggerFactory.getLogger(Wso2ServiceConsumer.class); /** * deploy package. @@ -67,9 +64,11 @@ public class Wso2ServiceConsumer { if (ipPort == null) { throw new CatalogResourceException("getMsbServerAddr failed."); } + ZipFile zf = null; InputStream ins = null; try { - ins = getInputStream(zipFileLocation, planFilePath); + zf = new ZipFile(zipFileLocation); + ins = getInputStream(zf, planFilePath); RestResponse res = RestfulClient.post( ipPort.getIp(), ipPort.getPort(), WSO2_APP_URL, buildRequest(ins, planFilePath)); @@ -88,17 +87,14 @@ public class Wso2ServiceConsumer { } throw new CatalogResourceException( - "Deploy Package return fail. Response = " + res.getResult()); - } catch (FileNotFoundException e1) { - throw new CatalogResourceException("Deploy Package failed.", e1); + "Deploy Package return failed. Response = " + res.getResult()); + } catch (FileNotFoundException e) { + throw new CatalogResourceException("buildRequest failed.", e); + } catch (IOException e) { + throw new CatalogResourceException("ZipFile failed.", e); } finally { - if (ins != null) { - try { - ins.close(); - } catch (IOException e1) { - LOGGER.error("inputStream close failed !"); - } - } + TemplateUtils.closeInputStream(ins); + TemplateUtils.closeZipFile(zf); } } @@ -128,39 +124,14 @@ public class Wso2ServiceConsumer { return builder.build(); } - @SuppressWarnings("resource") - private static InputStream getInputStream(String zipFileLocation, String planFilePath) - throws CatalogResourceException { - ZipInputStream zin = null; - try { - InputStream in = new BufferedInputStream(new FileInputStream(zipFileLocation)); - zin = new ZipInputStream(in); - ZipEntry ze; - while ((ze = zin.getNextEntry()) != null) { - if (planFilePath.equals(ze.getName())) { - ZipFile zf = new ZipFile(zipFileLocation); - return zf.getInputStream(ze); - } - } - } catch (IOException e1) { - throw new CatalogResourceException("Get InputStream failed. planFilePath = " + planFilePath, - e1); - } finally { - closeStream(zin); - } - - throw new CatalogResourceException("Get InputStream failed. planFilePath = " + planFilePath); - } - - private static void closeStream(ZipInputStream zin) { - - if (zin != null) { - try { - zin.closeEntry(); - } catch (IOException e1) { - LOGGER.error("zip inputStream close failed !"); - } + private static InputStream getInputStream(ZipFile zf, String planFilePath) + throws CatalogResourceException, IOException { + ZipEntry ze = TemplateUtils.getZipEntryZipFile(zf, planFilePath); + + if (ze == null) { + throw new CatalogResourceException("Can't file plan file in the csar. planFilePath = " + planFilePath); } + return zf.getInputStream(ze); } /** |