Search in sources :

Example 6 with Fail

use of org.wso2.carbon.humantask.core.engine.commands.Fail in project carbon-business-process by wso2.

the class HumanTaskUploadExecutor method saveAndExtractUploadedFile.

public SaveExtractReturn saveAndExtractUploadedFile(FileItem fileItem) throws Exception {
    String serviceUploadDir = getTempUploadDir();
    File servicesDir = new File(serviceUploadDir);
    if (!servicesDir.exists() && !servicesDir.mkdirs()) {
        throw new IOException("Fail to create the directory: " + servicesDir.getAbsolutePath());
    }
    // Writing HumanTask archive to file system
    String fileItemName = getFileName(fileItem.getName());
    File uploadedFile = new File(servicesDir, fileItemName);
    if (log.isDebugEnabled()) {
        log.debug("[HumanTaskUI]HumanTask Archive Path: " + uploadedFile.getAbsolutePath());
    }
    try {
        fileItem.write(uploadedFile);
    } catch (Exception e) {
        log.error("Error occurred while writing file item to file system.", e);
        throw new Exception("Error occurred while writing file item to file system.", e);
    }
    String destinationDir = serviceUploadDir + fileItemName.substring(0, fileItemName.lastIndexOf('.'));
    if (log.isDebugEnabled()) {
        log.debug("[HumanTaskUI]HumanTask package location: " + destinationDir);
    }
    try {
        ArchiveExtractor.extract(uploadedFile, destinationDir);
    } catch (Exception e) {
        log.error("Error extracting archive.", e);
        throw new Exception(e);
    }
    return new SaveExtractReturn(uploadedFile.getAbsolutePath(), destinationDir);
}
Also used : CarbonException(org.wso2.carbon.CarbonException)

Example 7 with Fail

use of org.wso2.carbon.humantask.core.engine.commands.Fail in project carbon-business-process by wso2.

the class BPELUploadExecutor method saveAndExtractUploadedFile.

public SaveExtractReturn saveAndExtractUploadedFile(FileItem fileItem) throws Exception {
    String serviceUploadDir = getTempUploadDir();
    File servicesDir = new File(serviceUploadDir);
    if (!servicesDir.exists() && !servicesDir.mkdirs()) {
        throw new IOException("Fail to create the directory: " + servicesDir.getAbsolutePath());
    }
    // Writing BPEL archive to file system
    String fileItemName = getFileName(fileItem.getName());
    File uploadedFile = new File(servicesDir, fileItemName);
    if (log.isDebugEnabled()) {
        log.debug("[BPELUI]BPEL Archive Path: " + uploadedFile.getAbsolutePath());
    }
    try {
        fileItem.write(uploadedFile);
    } catch (Exception e) {
        log.error("Error occurred while writing file item to file system.", e);
        throw new Exception("Erorr occurred while writing file item to file system.", e);
    }
    String destinationDir = serviceUploadDir + File.separator + fileItemName.substring(0, fileItemName.lastIndexOf('.'));
    if (log.isDebugEnabled()) {
        log.debug("[BPELUI]Bpel package location: " + destinationDir);
    }
    try {
        ArchiveExtractor.extract(uploadedFile, destinationDir);
    } catch (Exception e) {
        log.error("Error extracting archive.", e);
        throw new Exception(e);
    }
    // Handling backward compatibility issues. If user upload BPEL archive which follows the BPS 1.0.1 archive
    // format
    // we need to convert it to new format and upload.
    File deployXml = new File(destinationDir, "deploy.xml");
    if (!deployXml.exists()) {
        String depXmlSrc = fileItemName.substring(0, fileItemName.lastIndexOf('.')) + File.separator + "deploy.xml";
        deployXml = new File(destinationDir, depXmlSrc);
        if (deployXml.exists() && onlyOneChildDir(destinationDir, fileItemName.substring(0, fileItemName.lastIndexOf('.')))) {
            String tempUploadDir = getTempUploadDir();
            File tempDir = new File(tempUploadDir);
            if (!tempDir.exists() && !tempDir.mkdirs()) {
                throw new IOException("Fail to create the directory: " + tempDir.getAbsolutePath());
            }
            String filesToZipParent = destinationDir + File.separator + fileItemName.substring(0, fileItemName.lastIndexOf('.'));
            String zipLocation = tempDir.getAbsolutePath() + File.separator + fileItemName;
            try {
                zip(zipLocation, filesToZipParent);
            } catch (Exception e) {
                throw new Exception(e);
            }
            return new SaveExtractReturn(zipLocation, filesToZipParent);
        }
        throw new Exception("BPEL Archive format error.Please confirm that the file being uploaded is a " + "valid BPEL archive.");
    }
    return new SaveExtractReturn(uploadedFile.getAbsolutePath(), destinationDir);
}
Also used : IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException) CarbonException(org.wso2.carbon.CarbonException)

Example 8 with Fail

use of org.wso2.carbon.humantask.core.engine.commands.Fail in project carbon-business-process by wso2.

the class BPELUploader method uploadService.

public void uploadService(UploadedFileItem[] fileItems) throws AxisFault {
    // First lets filter for jar resources
    ConfigurationContext configurationContext = getConfigContext();
    String repo = configurationContext.getAxisConfiguration().getRepository().getPath();
    if (CarbonUtils.isURL(repo)) {
        throw new AxisFault("URL Repositories are not supported: " + repo);
    }
    // Writting the artifacts to the proper location
    String bpelDirectory = repo + File.separator + BPELConstants.BPEL_REPO_DIRECTORY;
    String bpelTemp = CarbonUtils.getCarbonHome() + BPELConstants.BPEL_PACKAGE_TEMP_DIRECTORY;
    File bpelTempDir = new File(bpelTemp);
    if (!bpelTempDir.exists() && !bpelTempDir.mkdirs()) {
        throw new AxisFault("Fail to create the directory: " + bpelTempDir.getAbsolutePath());
    }
    File bpelDir = new File(bpelDirectory);
    if (!bpelDir.exists() && !bpelDir.mkdirs()) {
        throw new AxisFault("Fail to create the directory: " + bpelDir.getAbsolutePath());
    }
    for (UploadedFileItem uploadedFile : fileItems) {
        String fileName = uploadedFile.getFileName();
        if (fileName == null || fileName.equals("")) {
            throw new AxisFault("Invalid file name. File name is not available");
        }
        if (uploadedFile.getFileType().equals(BPELConstants.BPEL_PACKAGE_EXTENSION)) {
            try {
                writeResource(uploadedFile.getDataHandler(), bpelTemp, fileName, bpelDir);
            } catch (IOException e) {
                throw new AxisFault("IOError: Writing resource failed.", e);
            }
        } else {
            throw new AxisFault("Invalid file type : " + uploadedFile.getFileType() + " ." + BPELConstants.BPEL_PACKAGE_EXTENSION + " file type is expected");
        }
    }
}
Also used : AxisFault(org.apache.axis2.AxisFault) ConfigurationContext(org.apache.axis2.context.ConfigurationContext) UploadedFileItem(org.wso2.carbon.bpel.deployer.services.types.UploadedFileItem) IOException(java.io.IOException) File(java.io.File)

Example 9 with Fail

use of org.wso2.carbon.humantask.core.engine.commands.Fail in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdDocumentsPost.

/**
 * Adds new document to an API
 *
 * @param apiId             UUID of API
 * @param body              DTO object including the document's meta information
 * @param ifMatch           If-Match header value
 * @param ifUnmodifiedSince If-Unmodified-Since header value
 * @param request           msf4j request object
 * @return newly added document meta info object
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response apisApiIdDocumentsPost(String apiId, DocumentDTO body, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    try {
        String username = RestApiUtil.getLoggedInUsername(request);
        APIPublisher apiProvider = RestAPIPublisherUtil.getApiPublisher(username);
        DocumentInfo documentation = MappingUtil.toDocumentInfo(body);
        if (body.getType() == DocumentDTO.TypeEnum.OTHER && StringUtils.isBlank(body.getOtherTypeName())) {
            // check otherTypeName for not null if doc type is OTHER
            RestApiUtil.handleBadRequest("otherTypeName cannot be empty if type is OTHER.", log);
        }
        String sourceUrl = body.getSourceUrl();
        if (body.getSourceType() == DocumentDTO.SourceTypeEnum.URL && (StringUtils.isBlank(sourceUrl) || !RestApiUtil.isURL(sourceUrl))) {
            RestApiUtil.handleBadRequest("Invalid document sourceUrl Format", log);
        }
        // this will fail if user does not have access to the API or the API does not exist
        String docid = apiProvider.addDocumentationInfo(apiId, documentation);
        documentation = apiProvider.getDocumentationSummary(docid);
        DocumentDTO newDocumentDTO = MappingUtil.toDocumentDTO(documentation);
        // Add initial inline content as empty String, if the Document type is INLINE
        if (body.getSourceType() == DocumentDTO.SourceTypeEnum.INLINE) {
            apiProvider.addDocumentationContent(docid, "");
            if (log.isDebugEnabled()) {
                log.debug("The updated source type of the document " + body.getName() + " is: " + body.getSourceType());
            }
        }
        return Response.status(Response.Status.CREATED).entity(newDocumentDTO).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while create  document for api " + apiId;
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) DocumentDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.DocumentDTO) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo)

Example 10 with Fail

use of org.wso2.carbon.humantask.core.engine.commands.Fail in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdDocumentsDocumentIdPut.

/**
 * Updates an API's document
 *
 * @param apiId             UUID of API
 * @param documentId        UUID of the document
 * @param body              DTO object including the document's meta information
 * @param ifMatch           If-Match header value
 * @param ifUnmodifiedSince If-Unmodified-Since header value
 * @param request           msf4j request object
 * @return updated document meta info DTO as the response
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response apisApiIdDocumentsDocumentIdPut(String apiId, String documentId, DocumentDTO body, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
        String existingFingerprint = apisApiIdDocumentsDocumentIdGetFingerprint(apiId, documentId, null, null, request);
        if (!StringUtils.isEmpty(ifMatch) && !StringUtils.isEmpty(existingFingerprint) && !ifMatch.contains(existingFingerprint)) {
            return Response.status(Response.Status.PRECONDITION_FAILED).build();
        }
        DocumentInfo documentInfoOld = apiPublisher.getDocumentationSummary(documentId);
        // validation checks for existence of the document
        if (documentInfoOld == null) {
            String msg = "Error while getting document";
            log.error(msg);
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900314L, msg);
            log.error(msg);
            return Response.status(Response.Status.NOT_FOUND).entity(errorDTO).build();
        }
        if (body.getType() == DocumentDTO.TypeEnum.OTHER && StringUtils.isBlank(body.getOtherTypeName())) {
            // check otherTypeName for not null if doc type is OTHER
            String msg = "otherTypeName cannot be empty if type is OTHER.";
            log.error(msg);
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900313L, msg);
            log.error(msg);
            return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
        }
        if (body.getSourceType() == DocumentDTO.SourceTypeEnum.URL && (StringUtils.isBlank(body.getSourceUrl()) || !RestApiUtil.isURL(body.getSourceUrl()))) {
            // check otherTypeName for not null if doc type is OTHER
            String msg = "Invalid document sourceUrl Format";
            log.error(msg);
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900313L, msg);
            log.error(msg);
            return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
        }
        // overriding some properties
        body.setName(documentInfoOld.getName());
        body.setDocumentId(documentInfoOld.getId());
        DocumentInfo documentation = MappingUtil.toDocumentInfo(body);
        // this will fail if user does not have access to the API or the API does not exist
        apiPublisher.updateDocumentation(apiId, documentation);
        // retrieve the updated documentation
        DocumentInfo newDocumentation = apiPublisher.getDocumentationSummary(documentId);
        String newFingerprint = apisApiIdDocumentsDocumentIdGetFingerprint(apiId, documentId, null, null, request);
        return Response.ok().header(HttpHeaders.ETAG, "\"" + newFingerprint + "\"").entity(MappingUtil.toDocumentDTO(newDocumentation)).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while updating the document " + documentId + " for API : " + apiId;
        log.error(errorMessage, e);
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
        paramList.put(APIMgtConstants.ExceptionsConstants.DOC_ID, documentId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) DocumentInfo(org.wso2.carbon.apimgt.core.models.DocumentInfo)

Aggregations

ConfigurationContext (org.apache.axis2.context.ConfigurationContext)5 MessageContext (org.apache.axis2.context.MessageContext)5 IOException (java.io.IOException)4 EventException (org.wso2.eventing.exceptions.EventException)4 File (java.io.File)3 HashMap (java.util.HashMap)3 AxisFault (org.apache.axis2.AxisFault)3 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)3 Calendar (java.util.Calendar)2 Date (java.util.Date)2 QName (javax.xml.namespace.QName)2 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)2 Test (org.testng.annotations.Test)2 CarbonException (org.wso2.carbon.CarbonException)2 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)2 DocumentInfo (org.wso2.carbon.apimgt.core.models.DocumentInfo)2 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)2 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)2 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)2 ArrayList (java.util.ArrayList)1