Java Code Examples for org.apache.cxf.jaxrs.ext.multipart.Attachment#getDataHandler()
The following examples show how to use
org.apache.cxf.jaxrs.ext.multipart.Attachment#getDataHandler() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BookCatalog.java From cxf with Apache License 2.0 | 6 votes |
@POST @Consumes("multipart/form-data") public Response addBook(final MultipartBody body) throws Exception { for (final Attachment attachment: body.getAllAttachments()) { final DataHandler handler = attachment.getDataHandler(); if (handler != null) { final String source = handler.getName(); final LuceneDocumentMetadata metadata = new LuceneDocumentMetadata() .withSource(source) .withField("modified", Date.class); final Document document = extractor.extract(handler.getInputStream(), metadata); if (document != null) { try (IndexWriter writer = getIndexWriter()) { writer.addDocument(document); writer.commit(); } } } } return Response.ok().build(); }
Example 2
Source File: MultipartProvider.java From cxf with Apache License 2.0 | 6 votes |
private <T> Object fromAttachment(Attachment multipart, Class<T> c, Type t, Annotation[] anns) throws IOException { if (DataHandler.class.isAssignableFrom(c)) { return multipart.getDataHandler(); } else if (DataSource.class.isAssignableFrom(c)) { return multipart.getDataHandler().getDataSource(); } else if (Attachment.class.isAssignableFrom(c)) { return multipart; } else { if (mediaTypeSupported(multipart.getContentType())) { mc.put("org.apache.cxf.multipart.embedded", true); mc.put("org.apache.cxf.multipart.embedded.ctype", multipart.getContentType()); mc.put("org.apache.cxf.multipart.embedded.input", multipart.getDataHandler().getInputStream()); anns = new Annotation[]{}; } MessageBodyReader<T> r = mc.getProviders().getMessageBodyReader(c, t, anns, multipart.getContentType()); if (r != null) { InputStream is = multipart.getDataHandler().getInputStream(); return r.readFrom(c, t, anns, multipart.getContentType(), multipart.getHeaders(), is); } } return null; }
Example 3
Source File: RestDocumentService.java From document-management-software with GNU Lesser General Public License v3.0 | 5 votes |
@Override @POST @Path("/create") @Consumes(MediaType.MULTIPART_FORM_DATA) @ApiOperation(nickname = "createDocument", value = "Creates a new document", notes = "Creates a new document using the metadata document object provided as JSON/XML", response = WSDocument.class) @ApiImplicitParams({ @ApiImplicitParam(name = "document", value = "The document metadata provided as WSDocument object encoded in JSON/XML format", required = true, dataType = "string", paramType = "form", examples = @Example(value = { @ExampleProperty(value = "{ \"fileName\":\"Help.pdf\",\"folderId\": 4, \"language\":\"en\" }") })), @ApiImplicitParam(name = "content", value = "File data", required = true, dataType = "file", paramType = "form") }) @ApiResponses(value = { @ApiResponse(code = 401, message = "Authentication failed"), @ApiResponse(code = 500, message = "Generic error, see the response message") }) public Response create(@ApiParam(hidden = true) List<Attachment> atts) throws Exception { log.debug("create()"); String sid = validateSession(); WSDocument document = null; DataHandler content = null; for (Attachment att : atts) { if ("document".equals(att.getContentDisposition().getParameter("name"))) { document = att.getObject(WSDocument.class); } else if ("content".equals(att.getContentDisposition().getParameter("name"))) { content = att.getDataHandler(); } } log.debug("document: {}", document); log.debug("content: {}", content); try { // return super.create(sid, document, content); WSDocument cdoc = super.create(sid, document, content); return Response.ok().entity(cdoc).build(); } catch (Exception e) { log.error(e.getMessage(), e); return Response.status(500).entity(e.getMessage()).build(); } }
Example 4
Source File: RestDocumentService.java From document-management-software with GNU Lesser General Public License v3.0 | 5 votes |
@Override @POST @Path("/uploadResource") @Consumes(MediaType.MULTIPART_FORM_DATA) @ApiOperation( value = "Uploads a new resource of the document", notes = "Uploads a new resource attached to the given document. If the resource already exists it is overwritten") @ApiImplicitParams({ @ApiImplicitParam(name = "docId", value = "the document id", required = true, dataType = "integer", paramType = "form"), @ApiImplicitParam(name = "fileVersion", value = "the specific file version", required = false, dataType = "string", paramType = "form"), @ApiImplicitParam(name = "suffix", value = "suffix specification(it cannot be empty, use 'conversion.pdf' to put the PDF conversion)", required = true, dataType = "string", paramType = "form"), @ApiImplicitParam(name = "content", value = "raw content of the file", required = true, dataType = "file", paramType = "form"), }) public void uploadResource(List<Attachment> attachments) throws Exception { String sid = validateSession(); Long docId = null; String fileVersion = null; String suffix = null; DataHandler datah = null; for (Attachment att : attachments) { Map<String, String> params = att.getContentDisposition().getParameters(); // log.debug("keys: {}", params.keySet()); // log.debug("name: {}", params.get("name")); if ("docId".equals(params.get("name"))) { docId = Long.parseLong(att.getObject(String.class)); } else if ("fileVersion".equals(params.get("fileVersion"))) { fileVersion = att.getObject(String.class); } else if ("suffix".equals(params.get("suffix"))) { suffix = att.getObject(String.class); } else if ("content".equals(params.get("content"))) { datah = att.getDataHandler(); } } super.uploadResource(sid, docId, fileVersion, suffix, datah); }
Example 5
Source File: FileAttachManagementImpl.java From opencps-v2 with GNU Affero General Public License v3.0 | 5 votes |
@Override public Response upload(HttpServletRequest request, HttpHeaders header, Company company, Locale locale, User user, ServiceContext serviceContext, String className, String classPK, Attachment attachment, String fileName, String fileType, long fileSize) { FileAttachInterface actions = new FileAttachActions(); FileAttachModel fileAttachModel = new FileAttachModel(); InputStream inputStream = null; try { DataHandler dataHandler = attachment.getDataHandler(); inputStream = dataHandler.getInputStream(); long groupId = GetterUtil.getLong(header.getHeaderString("groupId")); FileAttach fileAttach = actions.upload(user.getUserId(), company.getCompanyId(), groupId, className, classPK, inputStream, fileName, fileType, fileSize, "FileAttach/", "FileAttach file upload", serviceContext); fileAttachModel = FileAttachUtils.mapperFileAttachModel(fileAttach); return Response.status(200).entity(fileAttachModel).build(); } catch (Exception e) { return BusinessExceptionImpl.processException(e); } finally { if (inputStream != null) { try { inputStream.close(); } catch (Exception io) { _log.error(io); } } } }
Example 6
Source File: FileAttachManagementImpl.java From opencps-v2 with GNU Affero General Public License v3.0 | 5 votes |
@Override public Response update(HttpServletRequest request, HttpHeaders header, Company company, Locale locale, User user, ServiceContext serviceContext, long id, Attachment attachment, String fileName, String fileType, long fileSize) { FileAttachInterface actions = new FileAttachActions(); FileAttachModel fileAttachModel = new FileAttachModel(); InputStream inputStream = null; try { long groupId = GetterUtil.getLong(header.getHeaderString("groupId")); DataHandler dataHandler = attachment.getDataHandler(); inputStream = dataHandler.getInputStream(); FileAttach fileAttach = actions.update(user.getUserId(), company.getCompanyId(), groupId, id, inputStream, fileName, fileType, fileSize, "FileAttach/", "FileAttach file upload", serviceContext); fileAttachModel = FileAttachUtils.mapperFileAttachModel(fileAttach); return Response.status(200).entity(fileAttachModel).build(); } catch (Exception e) { return BusinessExceptionImpl.processException(e); } finally { if (inputStream != null) { try { inputStream.close(); } catch (Exception io) { _log.error(io); } } } }
Example 7
Source File: JwsMultipartSignatureOutFilter.java From cxf with Apache License 2.0 | 5 votes |
@Override public void filter(List<Attachment> parts) { for (int i = 0; i < parts.size() - 1; i++) { Attachment dataPart = parts.get(i); DataHandler handler = dataPart.getDataHandler(); dataPart.setDataHandler(new JwsSignatureDataHandler(handler)); } }
Example 8
Source File: RestDocumentService.java From document-management-software with GNU Lesser General Public License v3.0 | 4 votes |
@Override @POST @Path("/checkin") @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @ApiOperation(value = "Check-in an existing document", notes = "Performs a check-in (commit) operation of new content over an existing document. The document must be in checked-out status") @ApiImplicitParams({ @ApiImplicitParam(name = "docId", value = "The ID of an existing document to update", required = true, dataType = "integer", paramType = "form"), @ApiImplicitParam(name = "comment", value = "An optional comment", required = false, dataType = "string", paramType = "form"), @ApiImplicitParam(name = "release", value = "Indicates whether to create or not a new major release of the updated document", required = false, dataType = "string", paramType = "form", allowableValues = "true, false"), @ApiImplicitParam(name = "filename", value = "File name", required = true, dataType = "string", paramType = "form"), @ApiImplicitParam(name = "filedata", value = "File data", required = true, dataType = "file", paramType = "form") }) @ApiResponses(value = { @ApiResponse(code = 401, message = "Authentication failed"), @ApiResponse(code = 500, message = "Generic error, see the response message") }) public Response checkin(@ApiParam(hidden = true) List<Attachment> attachments) throws Exception { String sid = validateSession(); try { long docId = 0L; String comment = null; boolean release = false; String filename = null; DataHandler datah = null; for (Attachment att : attachments) { Map<String, String> params = att.getContentDisposition().getParameters(); if ("docId".equals(params.get("name"))) { docId = Long.parseLong(att.getObject(String.class)); } else if ("comment".equals(params.get("name"))) { comment = att.getObject(String.class); } else if ("release".equals(params.get("name"))) { release = Boolean.parseBoolean(att.getObject(String.class)); } else if ("filename".equals(params.get("name"))) { filename = att.getObject(String.class); } else if ("filedata".equals(params.get("name"))) { datah = att.getDataHandler(); } } super.checkin(sid, docId, comment, filename, release, datah); return Response.ok("file checked-in").build(); } catch (Throwable t) { log.error(t.getMessage(), t); return Response.status(500).entity(t.getMessage()).build(); } }
Example 9
Source File: RestDocumentService.java From document-management-software with GNU Lesser General Public License v3.0 | 4 votes |
@Override @POST @Path("/upload") @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @ApiOperation(value = "Uploads a document", notes = "Creates or updates an existing document, if used in update mode docId must be provided, when used in create mode folderId is required. Returns the ID of the created/updated document. <br/>Example: curl -u admin:admin -H ''Accept: application/json'' -X POST -F folderId=4 -F filename=newDoc.txt -F [email protected] http://localhost:8080/services/rest/document/upload") @ApiImplicitParams({ @ApiImplicitParam(name = "docId", value = "The ID of an existing document to update", required = false, dataType = "integer", paramType = "form"), @ApiImplicitParam(name = "folderId", value = "Folder ID where to place the document", required = false, dataType = "string", paramType = "form"), @ApiImplicitParam(name = "release", value = "Indicates whether to create or not a new major release of an updated document", required = false, dataType = "string", paramType = "form", allowableValues = "true, false"), @ApiImplicitParam(name = "filename", value = "File name", required = true, dataType = "string", paramType = "form"), @ApiImplicitParam(name = "language", value = "Language of the document (ISO 639-2)", required = false, dataType = "string", paramType = "form", defaultValue = "en"), @ApiImplicitParam(name = "filedata", value = "File data", required = true, dataType = "file", paramType = "form") }) @ApiResponses(value = { @ApiResponse(code = 401, message = "Authentication failed"), @ApiResponse(code = 500, message = "Generic error, see the response message") }) public Response upload(@ApiParam(hidden = true) List<Attachment> attachments) throws Exception { String sid = validateSession(); try { Long docId = null; Long folderId = null; boolean release = false; String filename = null; String language = null; DataHandler datah = null; for (Attachment att : attachments) { Map<String, String> params = att.getContentDisposition().getParameters(); // log.debug("keys: {}", params.keySet()); // log.debug("name: {}", params.get("name")); if ("docId".equals(params.get("name"))) { docId = Long.parseLong(att.getObject(String.class)); } else if ("folderId".equals(params.get("name"))) { folderId = Long.parseLong(att.getObject(String.class)); } else if ("release".equals(params.get("name"))) { release = Boolean.parseBoolean(att.getObject(String.class)); } else if ("filename".equals(params.get("name"))) { filename = att.getObject(String.class); } else if ("language".equals(params.get("name"))) { language = att.getObject(String.class); } else if ("filedata".equals(params.get("name"))) { datah = att.getDataHandler(); } } long documentId = super.upload(sid, docId, folderId, release, filename, language, datah); return Response.ok("" + documentId).build(); } catch (Throwable t) { log.error(t.getMessage(), t); return Response.status(500).entity(t.getMessage()).build(); } }
Example 10
Source File: RestDocumentService.java From document-management-software with GNU Lesser General Public License v3.0 | 4 votes |
@Override @POST @Path("/replaceFile") @Consumes(MediaType.MULTIPART_FORM_DATA) @Produces({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @ApiOperation(value = "Replace the file of a version", notes = "Replaces the file associated to a given version.") @ApiImplicitParams({ @ApiImplicitParam(name = "docId", value = "The ID of an existing document to update", required = true, dataType = "integer", paramType = "form"), @ApiImplicitParam(name = "fileVersion", value = "The file version", required = false, dataType = "string", paramType = "form"), @ApiImplicitParam(name = "comment", value = "Comment", required = false, dataType = "string", paramType = "form"), @ApiImplicitParam(name = "filedata", value = "File data", required = true, dataType = "file", paramType = "form") }) @ApiResponses(value = { @ApiResponse(code = 401, message = "Authentication failed"), @ApiResponse(code = 500, message = "Generic error, see the response message") }) public Response replaceFile(List<Attachment> attachments) throws Exception { String sid = validateSession(); try { Long docId = null; String fileVersion = null; String comment = null; DataHandler datah = null; for (Attachment att : attachments) { Map<String, String> params = att.getContentDisposition().getParameters(); if ("docId".equals(params.get("name"))) { docId = Long.parseLong(att.getObject(String.class)); } else if ("fileVersion".equals(params.get("name"))) { fileVersion = att.getObject(String.class); } else if ("comment".equals(params.get("name"))) { comment = att.getObject(String.class); } else if ("filedata".equals(params.get("name"))) { datah = att.getDataHandler(); } } super.replaceFile(sid, docId, fileVersion, comment, datah); return Response.ok("" + docId).build(); } catch (Throwable t) { log.error(t.getMessage(), t); return Response.status(500).entity(t.getMessage()).build(); } }
Example 11
Source File: PaymentFileManagementImpl.java From opencps-v2 with GNU Affero General Public License v3.0 | 4 votes |
/** * Update Payment File Confirm of DossierId and referenceUid * * @param form * params * @return Response */ @Override public Response updatePaymentFileConfirm(HttpServletRequest request, HttpHeaders header, Company company, Locale locale, User user, ServiceContext serviceContext, String id, String referenceUid, Attachment file/*, String confirmNote, String paymentMethod, String confirmPayload*/) { BackendAuth auth = new BackendAuthImpl(); long groupId = GetterUtil.getLong(header.getHeaderString("groupId")); long dossierId = GetterUtil.getLong(id); // TODO get Dossier by referenceUid if dossierId = 0 // String referenceUid = dossierId == 0 ? id : StringPool.BLANK; try { if (!auth.isAuth(serviceContext)) { throw new UnauthenticationException(); } DataHandler dataHandler = null; if (file != null) { dataHandler = file.getDataHandler(); } PaymentFileActions action = new PaymentFileActionsImpl(); PaymentFile paymentFile = action.updateFileConfirm(groupId, dossierId, referenceUid/*, confirmNote, paymentMethod, confirmPayload*/,StringPool.BLANK, StringPool.BLANK,StringPool.BLANK, dataHandler != null ? dataHandler.getName() : StringPool.BLANK, 0L, dataHandler != null ? dataHandler.getInputStream() : null, serviceContext); PaymentFileModel result = PaymentFileUtils.mappingToPaymentFileModel(paymentFile); return Response.status(200).entity(result).build(); } catch (Exception e) { return BusinessExceptionImpl.processException(e); } }
Example 12
Source File: DossierDocumentApiImpl.java From opencps-v2 with GNU Affero General Public License v3.0 | 4 votes |
@Override public DossierDocumentModel createDossierDoc(String id, Attachment upfileDetail, String referenceUid, String documentType, String documentName, String documentCode) { _log.debug("====START CREATE DOCUMENT==== "); long groupId = GetterUtil.getLong(header.getHeaderString("groupId")); Long dossierId = GetterUtil.getLong(id); DossierDocumentModel result = null; ServiceContext context = new ServiceContext(); context.setUserId(user.getUserId()); try { // if (!auth.isAuth(serviceContext)) { // throw new UnauthenticationException(); // } Dossier dossier = null; long dossierActionId = 0; if (dossierId != 0) { dossier = DossierLocalServiceUtil.fetchDossier(dossierId); } else { dossier = DossierLocalServiceUtil.getByRef(groupId, id); } if (dossier != null) { if (dossier.getOriginDossierId() != 0) { Dossier hsltDossier = DossierLocalServiceUtil.fetchDossier(dossier.getOriginDossierId()); if (hsltDossier != null) { dossierActionId = hsltDossier.getDossierActionId(); dossierId = hsltDossier.getDossierId(); } else { dossierId = 0l; } } else { dossierActionId = dossier.getDossierActionId(); dossierId = dossier.getDossierId(); } } else { dossierId = 0l; } DossierDocument oldDocument = DossierDocumentLocalServiceUtil.getDocByReferenceUid(groupId, dossierId, referenceUid); DataHandler dataHandler = upfileDetail.getDataHandler(); DossierDocumentActions action = new DossierDocumentActionsImpl(); DossierDocument dossierDoc = null; if (oldDocument == null) { dossierDoc = action.addDossierDoc(groupId, dossierId, referenceUid, dossierActionId, documentType, documentName, documentCode, dataHandler.getName(), 0, dataHandler.getInputStream(), StringPool.BLANK, context); } else { dossierDoc = DossierDocumentLocalServiceUtil.updateDossierDoc(groupId, oldDocument.getDossierDocumentId(), dossierId, referenceUid, dossierActionId, documentType, documentName, documentCode, dataHandler.getName(), 0, dataHandler.getInputStream(), StringPool.BLANK, context); } // if(Validator.isNotNull(formData)) { // dossierFile.setFormData(formData); // } // _log.info("__Start update dossier file at:" + new Date()); // DossierFileLocalServiceUtil.updateDossierFile(dossierFile); if (dossierDoc != null) { result = DossierDocumentParser.mappingDocumentTypeModel(dossierDoc); } _log.debug("====END CREATE DOCUMENT==== "); } catch (Exception e) { _log.debug(e); _log.error("====CREATE DOCUMENT ERROR==== "); respones.setStatus(HttpURLConnection.HTTP_INTERNAL_ERROR); } return result; }
Example 13
Source File: MessageContextImpl.java From cxf with Apache License 2.0 | 4 votes |
private void convertToAttachments(Object value) { List<?> handlers = (List<?>)value; List<org.apache.cxf.message.Attachment> atts = new ArrayList<>(); for (int i = 1; i < handlers.size(); i++) { Attachment handler = (Attachment)handlers.get(i); AttachmentImpl att = new AttachmentImpl(handler.getContentId(), handler.getDataHandler()); for (String key : handler.getHeaders().keySet()) { att.setHeader(key, handler.getHeader(key)); } att.setXOP(false); atts.add(att); } Message outMessage = getOutMessage(); outMessage.setAttachments(atts); outMessage.put(AttachmentOutInterceptor.WRITE_ATTACHMENTS, "true"); Attachment root = (Attachment)handlers.get(0); String rootContentType = root.getContentType().toString(); MultivaluedMap<String, String> rootHeaders = new MetadataMap<>(root.getHeaders()); if (!AttachmentUtil.isMtomEnabled(outMessage)) { rootHeaders.putSingle(Message.CONTENT_TYPE, rootContentType); } String messageContentType = outMessage.get(Message.CONTENT_TYPE).toString(); int index = messageContentType.indexOf(";type"); if (index > 0) { messageContentType = messageContentType.substring(0, index).trim(); } AttachmentOutputInterceptor attInterceptor = new AttachmentOutputInterceptor(messageContentType, rootHeaders); outMessage.put(Message.CONTENT_TYPE, rootContentType); Map<String, List<String>> allHeaders = CastUtils.cast((Map<?, ?>)outMessage.get(Message.PROTOCOL_HEADERS)); if (allHeaders != null) { allHeaders.remove(Message.CONTENT_TYPE); } attInterceptor.handleMessage(outMessage); }
Example 14
Source File: ServiceInfoManagementImpl.java From opencps-v2 with GNU Affero General Public License v3.0 | 2 votes |
@Override public Response addFileTemplateToServiceInfo(HttpServletRequest request, HttpHeaders header, Company company, Locale locale, User user, ServiceContext serviceContext, Attachment file, String id, String fileTemplateNo, String templateName, String fileType, int fileSize, String fileName) { long groupId = GetterUtil.getLong(header.getHeaderString("groupId")); long userId = serviceContext.getUserId(); ServiceFileTemplate serviceFileTemplate = null; BackendAuth auth = new BackendAuthImpl(); ServiceInfoActions actions = new ServiceInfoActionsImpl(); DataHandler dataHandler = file.getDataHandler(); InputStream inputStream = null; try { if (!auth.isAuth(serviceContext)) { throw new UnauthenticationException(); } if (!auth.hasResource(serviceContext, ServiceInfo.class.getName(), ActionKeys.ADD_ENTRY)) { throw new UnauthorizationException(); } inputStream = dataHandler.getInputStream(); serviceFileTemplate = actions.addServiceFileTemplate(userId, groupId, GetterUtil.getLong(id), fileTemplateNo, templateName, fileName, inputStream, fileType, fileSize, serviceContext); FileTemplateModel result = ServiceInfoUtils.mappingToFileTemplateModel(serviceFileTemplate); return Response.status(200).entity(result).build(); } catch (Exception e) { return BusinessExceptionImpl.processException(e); } }