List of usage examples for javax.activation DataHandler getDataSource
public DataSource getDataSource()
From source file:com.lin.umws.service.impl.UserServiceImpl.java
public void handlerUpload(DataHandler handler) { DataSource dataSource = handler.getDataSource(); System.out.println("Content-Type: " + dataSource.getContentType()); System.out.println("Name: " + dataSource.getName()); try {/*ww w. j a va 2s . c o m*/ IOUtils.copy(dataSource.getInputStream(), System.out); } catch (IOException e) { e.printStackTrace(); } }
From source file:org.mule.module.http.functional.listener.HttpListenerAttachmentsTestCase.java
@Test public void respondWithSeveralAttachments() throws Exception { MuleMessage response = muleContext.getClient().send(getUrl(filePath.getValue()), getTestMuleMessage()); assertThat(response.getInboundAttachmentNames().size(), is(2)); DataHandler attachment1 = response.getInboundAttachment(FILE_BODY_FIELD_NAME); HttpPart part = ((HttpPartDataSource) attachment1.getDataSource()).getPart(); assertThat(part.getName(), is(FILE_BODY_FIELD_NAME)); assertThat(part.getFileName(), is(FIELD_BDOY_FILE_NAME)); assertThat(part.getContentType(), is("application/octet-stream")); assertThat(IOUtils.toString(part.getInputStream()), is(FILE_BODY_FIELD_VALUE)); DataHandler attachment2 = response.getInboundAttachment(TEXT_BODY_FIELD_NAME); assertThat((String) attachment2.getContent(), is(TEXT_BODY_FIELD_VALUE)); assertThat(attachment2.getContentType(), is(TEXT_PLAIN)); }
From source file:org.wso2.appserver.integration.tests.webapp.mgt.WebappAdminTestCase.java
@Test(groups = "wso2.as", description = "download the war file through admin api", dependsOnMethods = "testWebApplicationDeployment") public void testWebAppDownload() throws IOException { String destination = webAppDownloadDirectory + webAppFileName; FileUtils.deleteQuietly(new File(destination)); DataHandler dataHandler = webAppAdminStub.downloadWarFileHandler(webAppFileName, hostName, "webapp"); InputStream in = null;/*ww w. j a va2s . c om*/ OutputStream outputStream = null; try { in = dataHandler.getDataSource().getInputStream(); outputStream = new FileOutputStream(new File(destination)); int read; byte[] bytes = new byte[1024]; while ((read = in.read(bytes)) != -1) { outputStream.write(bytes, 0, read); } } finally { assertNotNull(in); assertNotNull(outputStream); in.close(); outputStream.close(); } String extractedDir = extractZip(webAppDownloadDirectory + webAppFileName); log.info("Unpacked webapp in " + extractedDir); assertTrue(new File(extractedDir + File.separator + "index.jsp").exists(), "Download web app extraction was not successful."); }
From source file:org.shredzone.cilla.ws.assembler.GallerySectionAssembler.java
private void mergePictures(GallerySectionDto dto, GallerySection entity) throws CillaServiceException { int sequence = 0; Set<Picture> removables = new HashSet<>(entity.getPictures()); for (PictureDto picDto : dto.getPictures()) { Picture picture;/*w w w . j a va 2s.co m*/ if (picDto.isPersisted()) { picture = pictureDao.fetch(picDto.getId()); pictureAssembler.merge(picDto, picture); picture.setSequence(sequence++); removables.remove(picture); pictureService.updatePicture(picture, (picDto.getUploadFile() != null ? picDto.getUploadFile().getDataSource() : null)); } else { DataHandler dh = picDto.getUploadFile(); if (dh == null) { throw new CillaParameterException("new picture requires a file"); } picture = new Picture(); picture.setSequence(sequence++); pictureAssembler.merge(picDto, picture); pictureService.addPicture(entity, picture, dh.getDataSource()); picDto.setId(picture.getId()); } } for (Picture picture : removables) { pictureService.removePicture(picture); } }
From source file:edu.stanford.muse.email.EmailFetcherStats.java
private List<String> getAttachmentNames(MimeMessage m, Part p) throws MessagingException, IOException { List<String> result = new ArrayList<String>(); try {//from ww w .java2 s .c om if (p.isMimeType("multipart/*") || p.isMimeType("message/rfc822")) { if (p.isMimeType("multipart/alternative")) return result; // ignore alternative's because real attachments don't have alternatives DataHandler dh = p.getDataHandler(); DataSource ds = dh.getDataSource(); if (ds instanceof MultipartDataSource) { MultipartDataSource mpds = (MultipartDataSource) ds; for (int i = 0; i < mpds.getCount(); i++) result.addAll(getAttachmentNames(m, mpds.getBodyPart(i))); } else { String name = ds.getName(); if (!Util.nullOrEmpty(name)) result.add(name); } } else { String filename = p.getFileName(); if (filename != null) result.add(filename); } } catch (Exception e) { // sometimes we see javax.mail.MessagingException: Unable to load BODYSTRUCTURE // in this case, just ignore, not much we can do i guess. Util.print_exception(e, log); } return result; }
From source file:org.apache.abdera.protocol.client.util.DataSourceRequestEntity.java
public DataSourceRequestEntity(DataHandler dataHandler) { this(dataHandler.getDataSource()); }
From source file:org.apache.axiom.attachments.impl.BufferUtils.java
/** * The method checks to see if attachment is eligble for optimization. * An attachment is eligible for optimization if and only if the size of * the attachment is greated then the optimzation threshold size limit. * if the Content represented by DataHandler has size less than the * optimize threshold size, the attachment will not be eligible for * optimization, instead it will be inlined. * @param dh//from ww w . jav a 2s .c o m * @param limit * @return 1 if DataHandler data is bigger than limit, 0 if DataHandler data is smaller or * -1 if an error occurs or unsupported. * @throws IOException */ public static int doesDataHandlerExceedLimit(DataHandler dh, int limit) { //If Optimized Threshold not set return true. if (limit == 0) { return -1; } long size = DataSourceUtils.getSize(dh.getDataSource()); if (size != -1) { return size > limit ? 1 : 0; } else { // In all other cases, we prefer DataHandler#writeTo over DataSource#getInputStream. // The reason is that if the DataHandler was constructed from an Object rather than // a DataSource, a call to DataSource#getInputStream() will start a new thread and // return a PipedInputStream. This is so for Geronimo's as well as Sun's JAF // implementaion. The reason is that DataContentHandler only has a writeTo and no // getInputStream method. Obviously starting a new thread just to check the size of // the data is an overhead that we should avoid. try { dh.writeTo(new SizeLimitedOutputStream(limit)); } catch (SizeLimitExceededException ex) { return 1; } catch (IOException ex) { log.warn(ex.getMessage()); return -1; } return 0; } }
From source file:org.apache.axiom.attachments.lifecycle.impl.DataHandlerExtImpl.java
public DataHandlerExtImpl(DataHandler dataHandler, LifecycleManager manager) { super(dataHandler.getDataSource()); this.dataHandler = dataHandler; this.manager = manager; }
From source file:org.apache.axis.attachments.AttachmentPart.java
/** * Bulds a new <code>AttachmentPart</code> with a <code>DataHandler</code>. * * @param dh the <code>DataHandler</code> *///w w w .java2 s. c om public AttachmentPart(javax.activation.DataHandler dh) { setMimeHeader(HTTPConstants.HEADER_CONTENT_ID, SessionUtils.generateSessionId()); datahandler = dh; if (dh != null) { setMimeHeader(HTTPConstants.HEADER_CONTENT_TYPE, dh.getContentType()); javax.activation.DataSource ds = dh.getDataSource(); if (ds instanceof ManagedMemoryDataSource) { extractFilename((ManagedMemoryDataSource) ds); //and get the filename if appropriate } } }
From source file:org.apache.axis.attachments.AttachmentPart.java
/** * Sets the given <CODE>DataHandler</CODE> object as the * data handler for this <CODE>AttachmentPart</CODE> object. * Typically, on an incoming message, the data handler is * automatically set. When a message is being created and * populated with content, the <CODE>setDataHandler</CODE> * method can be used to get data from various data sources into * the message.//from ww w.j a v a 2 s . c om * @param datahandler <CODE>DataHandler</CODE> object to * be set * @throws java.lang.IllegalArgumentException if * there was a problem with the specified <CODE> * DataHandler</CODE> object */ public void setDataHandler(DataHandler datahandler) { if (datahandler == null) { throw new java.lang.IllegalArgumentException(Messages.getMessage("illegalArgumentException00")); } this.datahandler = datahandler; setMimeHeader(HTTPConstants.HEADER_CONTENT_TYPE, datahandler.getContentType()); //now look at the source of the data javax.activation.DataSource ds = datahandler.getDataSource(); if (ds instanceof ManagedMemoryDataSource) { //and get the filename if appropriate extractFilename((ManagedMemoryDataSource) ds); } }