Fix automatic package conversion
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / restapi / ConverterApi.java
index 4a73748..da07ef2 100644 (file)
  */
 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.restapi;
 
-import org.apache.http.entity.ContentType;
+import com.google.common.io.ByteStreams;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.Part;
 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.packagetransformer.OnapVnfPackageBuilder;
 import org.slf4j.Logger;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.HttpStatus;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.ResponseBody;
 
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import javax.servlet.http.Part;
-
-import java.io.IOException;
-
-import static org.apache.http.entity.ContentType.APPLICATION_OCTET_STREAM;
+import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.buildFatalFailure;
 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions.systemFunctions;
 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.DriverProperties.BASE_URL;
 import static org.slf4j.LoggerFactory.getLogger;
-import static org.springframework.http.HttpHeaders.CONTENT_DISPOSITION;
-import static org.springframework.http.HttpHeaders.CONTENT_LENGTH;
-import static org.springframework.http.HttpHeaders.CONTENT_TYPE;
+import static org.springframework.http.HttpHeaders.*;
 import static org.springframework.http.HttpStatus.OK;
+import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM;
 import static org.springframework.http.MediaType.TEXT_HTML_VALUE;
 import static org.springframework.web.bind.annotation.RequestMethod.GET;
 import static org.springframework.web.bind.annotation.RequestMethod.POST;
@@ -59,15 +55,26 @@ public class ConverterApi {
      */
     @RequestMapping(value = "/convert", method = POST)
     @ResponseBody
-    public void convert(HttpServletResponse httpResponse, HttpServletRequest request) throws Exception {
+    public void convert(HttpServletResponse httpResponse, HttpServletRequest request) throws IOException {
         logger.info("REST: convert package");
-        Part part = request.getParts().iterator().next();
-        byte[] bytes = vnfPackageConverter.covert(part.getInputStream());
-        httpResponse.addHeader(CONTENT_TYPE, APPLICATION_OCTET_STREAM.getMimeType());
+        byte[] content;
+        try {
+            Part part = request.getPart("fileToUpload");
+            content = ByteStreams.toByteArray(part.getInputStream());
+        } catch (Exception e) {
+            throw buildFatalFailure(logger, "Unable to extract package from REST parameters", e);
+        }
+        byte[] convertedPackage;
+        try {
+            convertedPackage = vnfPackageConverter.covert(new ByteArrayInputStream(content));
+        } catch (Exception e) {
+            throw buildFatalFailure(logger, "Unable to convert VNF package", e);
+        }
+        httpResponse.addHeader(CONTENT_TYPE, APPLICATION_OCTET_STREAM.toString());
         httpResponse.setStatus(OK.value());
-        httpResponse.addHeader(CONTENT_LENGTH, Integer.toString(bytes.length));
+        httpResponse.addHeader(CONTENT_LENGTH, Integer.toString(convertedPackage.length));
         httpResponse.addHeader(CONTENT_DISPOSITION, "attachment; filename=\"" + "core.csar" + "\"");
-        httpResponse.getOutputStream().write(bytes);
+        httpResponse.getOutputStream().write(convertedPackage);
         httpResponse.getOutputStream().flush();
     }