Search in sources :

Example 26 with Attachment

use of org.wso2.carbon.attachment.mgt.api.attachment.Attachment in project carbon-business-process by wso2.

the class AttachmentManagerService method getAttachmentInfo.

/**
 * {@inheritDoc}
 */
@Override
public TAttachment getAttachmentInfo(final String id) throws AttachmentMgtException {
    try {
        AttachmentDAO attachmentDAO = getDaoConnectionFactory().getDAOConnection().getAttachmentMgtDAOFactory().getAttachmentInfo(id);
        Attachment attachment = getDaoTransformFactory().convertAttachment(attachmentDAO);
        return TransformerUtil.convertAttachment(attachment);
    } catch (org.wso2.carbon.attachment.mgt.core.exceptions.AttachmentMgtException e) {
        String errorMsg = "org.wso2.carbon.attachment.mgt.core.service" + ".AttachmentManagerService.getAttachmentInfo operation failed. " + "Reason:" + e.getLocalizedMessage();
        log.error(errorMsg, e);
        throw new AttachmentMgtException(errorMsg, e);
    }
}
Also used : AttachmentMgtException(org.wso2.carbon.attachment.mgt.skeleton.AttachmentMgtException) AttachmentDAO(org.wso2.carbon.attachment.mgt.core.dao.AttachmentDAO) TAttachment(org.wso2.carbon.attachment.mgt.skeleton.types.TAttachment) Attachment(org.wso2.carbon.attachment.mgt.api.attachment.Attachment)

Example 27 with Attachment

use of org.wso2.carbon.attachment.mgt.api.attachment.Attachment in project carbon-business-process by wso2.

the class AttachmentUploadClient method addUploadedFileItem.

public void addUploadedFileItem(FileItemData fileItemData) throws AttachmentMgtException, RemoteException {
    DataHandler handler = fileItemData.getDataHandler();
    TAttachment attachment = new TAttachment();
    attachment.setName(handler.getName());
    attachment.setContentType(handler.getContentType());
    // TODO: Remove this hard-coded value
    attachment.setCreatedBy("DummyUser");
    attachment.setContent(fileItemData.getDataHandler());
    String attachmentID = stub.add(attachment);
    log.info("Attachment was uploaded with id:" + attachmentID);
}
Also used : TAttachment(org.wso2.carbon.attachment.mgt.stub.types.TAttachment) DataHandler(javax.activation.DataHandler)

Example 28 with Attachment

use of org.wso2.carbon.attachment.mgt.api.attachment.Attachment in project carbon-business-process by wso2.

the class SampleAttachmentMgtClient method uploadAttachment.

private static String uploadAttachment() throws RemoteException, AttachmentMgtException {
    AttachmentMgtServiceStub stub = new AttachmentMgtServiceStub();
    Options options = new Options();
    options.setTo(new EndpointReference("http://127.0.0.1:9763/services/AttachmentMgtService/"));
    options.setProperty(Constants.Configuration.ENABLE_MTOM, Boolean.TRUE);
    stub._getServiceClient().setOptions(options);
    TAttachment att = new TAttachment();
    // att.setId("ContentId");
    // att.setCreatedTime(Calendar.getInstance());
    att.setName("ContentName");
    att.setCreatedBy("DenisAuthor");
    att.setContentType("text/plain");
    // FileDataSource dataSource = new FileDataSource(new File("/home/denis/Desktop/note.txt"));
    FileDataSource dataSource = new FileDataSource(new File("/home/denis/Desktop/fromSoapUI.xml"));
    DataHandler fileDataHandler = new DataHandler(dataSource);
    att.setContent(fileDataHandler);
    String id = stub.add(att);
    if (log.isDebugEnabled()) {
        log.debug("Attachment uploaded with id: " + id);
    }
    return id;
}
Also used : Options(org.apache.axis2.client.Options) TAttachment(org.wso2.carbon.attachment.mgt.stub.types.TAttachment) AttachmentMgtServiceStub(org.wso2.carbon.attachment.mgt.stub.AttachmentMgtServiceStub) FileDataSource(javax.activation.FileDataSource) DataHandler(javax.activation.DataHandler) File(java.io.File) EndpointReference(org.apache.axis2.addressing.EndpointReference)

Example 29 with Attachment

use of org.wso2.carbon.attachment.mgt.api.attachment.Attachment in project carbon-business-process by wso2.

the class SampleAttachmentMgtClient method getAttachmentInfo.

private static void getAttachmentInfo(String id) throws RemoteException, AttachmentMgtException {
    AttachmentMgtServiceStub stub = new AttachmentMgtServiceStub();
    Options options = new Options();
    options.setTo(new EndpointReference("http://127.0.0.1:9763/services/AttachmentMgtService/"));
    options.setProperty(Constants.Configuration.ENABLE_MTOM, Boolean.TRUE);
    stub._getServiceClient().setOptions(options);
    TAttachment attachment = stub.getAttachmentInfo(id);
    if (log.isDebugEnabled()) {
        log.debug("Attachment details received. Id: " + attachment.getId());
    }
}
Also used : Options(org.apache.axis2.client.Options) TAttachment(org.wso2.carbon.attachment.mgt.stub.types.TAttachment) AttachmentMgtServiceStub(org.wso2.carbon.attachment.mgt.stub.AttachmentMgtServiceStub) EndpointReference(org.apache.axis2.addressing.EndpointReference)

Example 30 with Attachment

use of org.wso2.carbon.attachment.mgt.api.attachment.Attachment in project carbon-business-process by wso2.

the class AttachmentUploadExecutor method execute.

@Override
public boolean execute(HttpServletRequest request, HttpServletResponse response) throws IOException {
    PrintWriter out = response.getWriter();
    String webContext = (String) request.getAttribute(CarbonConstants.WEB_CONTEXT);
    String serverURL = (String) request.getAttribute(CarbonConstants.SERVER_URL);
    String cookie = (String) request.getAttribute(ServerConstants.ADMIN_SERVICE_COOKIE);
    Map<String, ArrayList<String>> formFieldsMap = getFormFieldsMap();
    String redirect = null;
    try {
        if (formFieldsMap.get("redirect") != null) {
            redirect = formFieldsMap.get("redirect").get(0);
        }
        ArrayList<FileItemData> fileItemsMap = getFileItemsMap().get("fileToUpload");
        FileItemData fileToBeUpload = fileItemsMap.get(0);
        if (fileItemsMap == null || fileItemsMap.isEmpty() || fileItemsMap.size() != 1) {
            String msg = "File uploading failed.";
            log.error(msg);
            out.write("<textarea>" + "(function(){i18n.fileUplodedFailed();})();" + "</textarea>");
            return true;
        }
        AttachmentUploadClient client = new AttachmentUploadClient(configurationContext, serverURL + "AttachmentMgtService", cookie);
        response.setContentType("text/html; charset=utf-8");
        client.addUploadedFileItem(fileToBeUpload);
        String msg = "Your attachment has been uploaded successfully. Please refresh this page in a while to see " + "the status of the new process.";
        if (redirect != null) {
            CarbonUIMessage.sendCarbonUIMessage(msg, CarbonUIMessage.INFO, request, response, getContextRoot(request) + "/" + webContext + "/" + redirect);
        } else {
            CarbonUIMessage.sendCarbonUIMessage(msg, CarbonUIMessage.INFO, request);
        }
        return true;
    } catch (Exception ex) {
        String errMsg = "File upload failed.";
        log.error(errMsg, ex);
        if (redirect != null) {
            CarbonUIMessage.sendCarbonUIMessage(errMsg, CarbonUIMessage.ERROR, request, response, getContextRoot(request) + "/" + webContext + "/" + redirect);
        } else {
            CarbonUIMessage.sendCarbonUIMessage(errMsg, CarbonUIMessage.ERROR, request);
        }
    }
    return false;
}
Also used : FileItemData(org.wso2.carbon.utils.FileItemData) ArrayList(java.util.ArrayList) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Aggregations

HashMap (java.util.HashMap)11 AttachmentMgtException (org.wso2.carbon.attachment.mgt.skeleton.AttachmentMgtException)11 TAttachment (org.wso2.carbon.attachment.mgt.skeleton.types.TAttachment)10 DataHandler (javax.activation.DataHandler)9 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)9 ArrayList (java.util.ArrayList)8 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)8 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)8 File (java.io.File)7 Response (javax.ws.rs.core.Response)7 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)7 RestResponseFactory (org.wso2.carbon.bpmn.rest.common.RestResponseFactory)7 TopLevelNode (org.ballerinalang.model.tree.TopLevelNode)6 AttachmentDAO (org.wso2.carbon.attachment.mgt.core.dao.AttachmentDAO)6 BaseTaskService (org.wso2.carbon.bpmn.rest.service.base.BaseTaskService)5 IOException (java.io.IOException)4 List (java.util.List)4 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)4 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)4 Attachment (org.wso2.carbon.attachment.mgt.api.attachment.Attachment)4