List of usage examples for javax.mail Multipart getCount
public synchronized int getCount() throws MessagingException
From source file:org.accesointeligente.server.robots.ResponseChecker.java
public void connectAndCheck() { if (ApplicationProperties.getProperty("email.server") == null || ApplicationProperties.getProperty("email.user") == null || ApplicationProperties.getProperty("email.password") == null || ApplicationProperties.getProperty("email.folder") == null || ApplicationProperties.getProperty("email.failfolder") == null || ApplicationProperties.getProperty("attachment.directory") == null || ApplicationProperties.getProperty("attachment.baseurl") == null) { logger.error("Properties are not defined!"); return;/*from w ww .j ava 2 s.c o m*/ } org.hibernate.Session hibernate = null; try { session = Session.getInstance(props, null); store = session.getStore("imaps"); store.connect(ApplicationProperties.getProperty("email.server"), ApplicationProperties.getProperty("email.user"), ApplicationProperties.getProperty("email.password")); Folder failbox = store.getFolder(ApplicationProperties.getProperty("email.failfolder")); Folder inbox = store.getFolder(ApplicationProperties.getProperty("email.folder")); inbox.open(Folder.READ_WRITE); for (Message message : inbox.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false))) { try { logger.info("Sender: " + message.getFrom()[0] + "\tSubject: " + message.getSubject()); remoteIdentifiers = null; messageBody = null; remoteIdentifiers = new HashSet<String>(); if (message.getSubject() != null) { Matcher matcher = pattern.matcher(message.getSubject()); if (matcher.matches()) { remoteIdentifiers.add(formatIdentifier(matcher.group(1), matcher.group(2), Integer.parseInt(matcher.group(3)))); logger.info("remote identifier: " + formatIdentifier(matcher.group(1), matcher.group(2), Integer.parseInt(matcher.group(3)))); } } Object content = message.getContent(); if (content instanceof Multipart) { Multipart mp = (Multipart) message.getContent(); logger.info("Email content type is Multipart, each part of " + mp.getCount() + " will be processed"); for (int i = 0, n = mp.getCount(); i < n; i++) { Part part = mp.getBodyPart(i); logger.info("Part: " + (i + 1) + " of " + mp.getCount()); processPart(part); } } else if (content instanceof String) { logger.info("Email content type is String"); messageBody = (String) content; Matcher matcher; StringTokenizer tokenizer = new StringTokenizer(messageBody); while (tokenizer.hasMoreTokens()) { String token = tokenizer.nextToken(); matcher = pattern.matcher(token); if (matcher.matches()) { remoteIdentifiers.add(formatIdentifier(matcher.group(1), matcher.group(2), Integer.parseInt(matcher.group(3)))); logger.info("remote identifier: " + formatIdentifier(matcher.group(1), matcher.group(2), Integer.parseInt(matcher.group(3)))); } } } else { logger.info("Email content type isn't String or Multipart"); message.setFlag(Flag.SEEN, false); inbox.copyMessages(new Message[] { message }, failbox); message.setFlag(Flag.DELETED, true); inbox.expunge(); continue; } Boolean requestFound = false; Matcher matcher = htmlPattern.matcher(messageBody); if (matcher.find()) { messageBody = htmlToString(messageBody); } logger.info("Searching for Request Remote Identifier"); for (String remoteIdentifier : remoteIdentifiers) { hibernate = HibernateUtil.getSession(); hibernate.beginTransaction(); Criteria criteria = hibernate.createCriteria(Request.class); criteria.add(Restrictions.eq("remoteIdentifier", remoteIdentifier)); Request request = (Request) criteria.uniqueResult(); hibernate.getTransaction().commit(); if (request != null) { logger.info("Request found for Remote Identifier: " + remoteIdentifier); Response response; // If the attachments haven't been used, use them. Otherwise, copy them. if (!requestFound) { response = createResponse(message.getFrom()[0].toString(), message.getSentDate(), message.getSubject(), messageBody); } else { response = createResponse(message.getFrom()[0].toString(), message.getSentDate(), message.getSubject(), messageBody); } hibernate = HibernateUtil.getSession(); hibernate.beginTransaction(); response.setRequest(request); request.setStatus(RequestStatus.RESPONDED); request.setExpired(RequestExpireType.WITHRESPONSE); request.setResponseDate(new Date()); hibernate.update(request); hibernate.update(response); hibernate.getTransaction().commit(); requestFound = true; } } if (!requestFound) { logger.info("Request not found"); createResponse(message.getFrom()[0].toString(), message.getSentDate(), message.getSubject(), messageBody); message.setFlag(Flag.SEEN, false); inbox.copyMessages(new Message[] { message }, failbox); message.setFlag(Flag.DELETED, true); inbox.expunge(); } } catch (Exception e) { if (hibernate != null && hibernate.isOpen() && hibernate.getTransaction().isActive()) { hibernate.getTransaction().rollback(); } logger.error(e.getMessage(), e); } } } catch (Exception e) { if (hibernate != null && hibernate.isOpen() && hibernate.getTransaction().isActive()) { hibernate.getTransaction().rollback(); } logger.error(e.getMessage(), e); } }
From source file:com.stimulus.archiva.domain.Email.java
protected boolean hasAttachment(Part p) throws MessagingException, IOException { if (p.getDisposition() != null && Compare.equalsIgnoreCase(p.getDisposition(), Part.ATTACHMENT)) { logger.debug("hasAttachment() attachment disposition."); return true; }/* www. ja v a2 s. c om*/ if (p.getFileName() != null) { logger.debug("hasAttachment() filename specified."); return true; } try { if (p.isMimeType("multipart/*")) { Multipart mp = (Multipart) p.getContent(); for (int i = 0; i < mp.getCount(); i++) { logger.debug("hasAttachment() scanning multipart[" + i + "]"); if (hasAttachment(mp.getBodyPart(i))) return true; } } else if (p.isMimeType("message/rfc822")) { return hasAttachment((Part) p.getContent()); } } catch (Exception e) { logger.debug("exception occurred while detecting attachment", e); } return false; }
From source file:org.geoserver.wcs.GetCoverageTest.java
@Test public void testRasterFilterGreen() throws Exception { String queryString = "wcs?identifier=" + getLayerId(MOSAIC) + "&request=getcoverage" + "&service=wcs&version=1.1.1&&format=image/tiff" + "&BoundingBox=0,0,1,1,urn:ogc:def:crs:EPSG:6.6:4326" + "&CQL_FILTER=location like 'green%25'"; MockHttpServletResponse response = getAsServletResponse(queryString); // parse the multipart, check there are two parts Multipart multipart = getMultipart(response); assertEquals(2, multipart.getCount()); BodyPart coveragePart = multipart.getBodyPart(1); assertEquals("image/tiff", coveragePart.getContentType()); assertEquals("<theCoverage>", coveragePart.getHeader("Content-ID")[0]); // make sure we can read the coverage back ImageReader reader = ImageIO.getImageReadersByFormatName("tiff").next(); reader.setInput(ImageIO.createImageInputStream(coveragePart.getInputStream())); RenderedImage image = reader.read(0); // check the pixel int[] pixel = new int[3]; image.getData().getPixel(0, 0, pixel); assertEquals(0, pixel[0]);// www .j av a2s . com assertEquals(255, pixel[1]); assertEquals(0, pixel[2]); }
From source file:org.geoserver.wcs.GetCoverageTest.java
@Test public void testRasterFilterRed() throws Exception { String queryString = "wcs?identifier=" + getLayerId(MOSAIC) + "&request=getcoverage" + "&service=wcs&version=1.1.1&&format=image/tiff" + "&BoundingBox=0,0,1,1,urn:ogc:def:crs:EPSG:6.6:4326" + "&CQL_FILTER=location like 'red%25'"; MockHttpServletResponse response = getAsServletResponse(queryString); // parse the multipart, check there are two parts Multipart multipart = getMultipart(response); assertEquals(2, multipart.getCount()); BodyPart coveragePart = multipart.getBodyPart(1); assertEquals("image/tiff", coveragePart.getContentType()); assertEquals("<theCoverage>", coveragePart.getHeader("Content-ID")[0]); // make sure we can read the coverage back ImageReader reader = ImageIO.getImageReadersByFormatName("tiff").next(); reader.setInput(ImageIO.createImageInputStream(coveragePart.getInputStream())); RenderedImage image = reader.read(0); // check the pixel int[] pixel = new int[3]; image.getData().getPixel(0, 0, pixel); assertEquals(255, pixel[0]);// w w w . j a v a 2 s.com assertEquals(0, pixel[1]); assertEquals(0, pixel[2]); }
From source file:org.mxhero.engine.plugin.attachmentlink.alcommand.internal.domain.Message.java
public void buildMd5CheckSums(List<String> checksums, Part part) throws MessagingException, IOException { if (part.isMimeType(MULTIPART_TYPE)) { Multipart mp = (Multipart) part.getContent(); for (int i = 0; i < mp.getCount(); i++) { buildMd5CheckSums(checksums, mp.getBodyPart(i)); }/*from ww w. j a v a2s . com*/ } else if (isAttach(part)) { checksums.add(getCheckSum(part)); } }
From source file:org.mxhero.engine.plugin.attachmentlink.alcommand.internal.domain.Message.java
public void buildAttachments(List<Attach> attachs, Part part, String baseStorePath) throws MessagingException, IOException { if (part.isMimeType(MULTIPART_TYPE)) { Multipart mp = (Multipart) part.getContent(); for (int i = 0; i < mp.getCount(); i++) { buildAttachments(attachs, mp.getBodyPart(i), baseStorePath); }/*from www.j av a 2 s . c o m*/ } else if (isAttach(part)) { attachs.add(getAttach(part, baseStorePath)); } }
From source file:org.geoserver.wcs.GetCoverageTest.java
@Test public void testReadNoGridCRS() throws Exception { String request = // " <wcs:GetCoverage service=\"WCS\" version=\"1.1.1\" " + " xmlns:wcs=\"http://www.opengis.net/wcs/1.1.1\" " + " xmlns:gml=\"http://www.opengis.net/gml\"" + " xmlns:ows=\"http://www.opengis.net/ows/1.1\" >\n" + " <ows:Identifier>" + getLayerId(MockData.TASMANIA_DEM) + " </ows:Identifier>\n" + " <wcs:DomainSubset>\n" + " <ows:BoundingBox crs=\"http://www.opengis.net/gml/srs/epsg.xml#4326\">\n" + " <ows:LowerCorner>-180.0 -90.0</ows:LowerCorner>\n" + " <ows:UpperCorner>180.0 90.0</ows:UpperCorner>\n" + " </ows:BoundingBox>\n" // + " </wcs:DomainSubset>\n" + " <wcs:Output format=\"image/tiff\"/>\n" + " </wcs:GetCoverage>"; MockHttpServletResponse response = postAsServletResponse("wcs", request); // parse the multipart, check there are two parts Multipart multipart = getMultipart(response); assertEquals(2, multipart.getCount()); BodyPart coveragePart = multipart.getBodyPart(1); assertEquals("image/tiff", coveragePart.getContentType()); assertEquals("<theCoverage>", coveragePart.getHeader("Content-ID")[0]); // make sure we can read the coverage back ImageReader reader = ImageIO.getImageReadersByFormatName("tiff").next(); reader.setInput(ImageIO.createImageInputStream(coveragePart.getInputStream())); RenderedImage image = reader.read(0); }
From source file:org.geoserver.wcs.GetCoverageTest.java
/** * This tests just ended up throwing an exception as the coverage being encoded * was too large due to a bug in the scales estimation * /*from w ww. j a v a2 s . c o m*/ * @throws Exception */ @Test public void testRotatedPost() throws Exception { String request = "<GetCoverage xmlns=\"http://www.opengis.net/wcs/1.1.1\" xmlns:gml=\"http://www.opengis.net/gml\"\n" + " xmlns:ows11=\"http://www.opengis.net/ows/1.1\"\n" + " xmlns:ows=\"http://www.opengis.net/ows/1.1\"\n" + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \r\n" + // " xmlns:wcs=\"http://schemas.opengis.net/wcs/1.1.1\"\n" + " xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n" + " service=\"WCS\"\n" + " version=\"1.1.1\"\n" + " xsi:schemaLocation=\"http://www.opengis.net/wcs/1.1.1 http://schemas.opengis.net/wcs/1.1.1/wcsAll.xsd\">\n" + " <ows11:Identifier>RotatedCad</ows11:Identifier>\n" + " <DomainSubset>\n" + " <ows11:BoundingBox crs=\"urn:ogc:def:crs:OGC:1.3:CRS84\">\n" + " <ows11:LowerCorner>7.7634301664746515 45.14713380418506</ows11:LowerCorner>\n" + " <ows11:UpperCorner>7.764350661575157 45.14763319238466</ows11:UpperCorner>\n" + " </ows11:BoundingBox>\n" + " </DomainSubset>\n" + " <Output format=\"image/tiff\"/>\n" + "</GetCoverage>"; MockHttpServletResponse response = postAsServletResponse("wcs", request); // parse the multipart, check there are two parts Multipart multipart = getMultipart(response); assertEquals(2, multipart.getCount()); BodyPart coveragePart = multipart.getBodyPart(1); assertEquals("image/tiff", coveragePart.getContentType()); assertEquals("<theCoverage>", coveragePart.getHeader("Content-ID")[0]); // make sure we can read the coverage back ImageReader reader = ImageIO.getImageReadersByFormatName("tiff").next(); reader.setInput(ImageIO.createImageInputStream(coveragePart.getInputStream())); RenderedImage image = reader.read(0); // check the image is suitably small (without requiring an exact size) assertTrue(image.getWidth() < 1000); assertTrue(image.getHeight() < 1000); }
From source file:org.geoserver.wcs.GetCoverageTest.java
@Test public void testLargerThanData() throws Exception { MockHttpServletResponse response = getAsServletResponse("wcs/BlueMarble/wcs?identifier=" + getLayerId(TASMANIA_BM) + "&request=getcoverage&service=wcs&version=1.1.1&&format=image/geotiff" + "&BoundingBox=-90,-180,90,180,urn:ogc:def:crs:EPSG:6.6:4326&GridBaseCRS=EPSG:4326"); // parse the multipart, check there are two parts Multipart multipart = getMultipart(response); assertEquals(2, multipart.getCount()); BodyPart coveragePart = multipart.getBodyPart(1); assertEquals("image/tiff", coveragePart.getContentType()); assertEquals("<theCoverage>", coveragePart.getHeader("Content-ID")[0]); // save/*from w w w . j a v a 2 s .c o m*/ File tiffFile = File.createTempFile("wcs", "", new File("target")); IOUtils.copy(coveragePart.getInputStream(), new FileOutputStream(tiffFile)); // make sure we can read the coverage back GeoTiffReader reader = new GeoTiffReader(tiffFile); GridCoverage2D result = reader.read(null); coverages.add(result); // see that we got the entire coverage, but nothing more CoverageInfo ci = getCatalog().getCoverageByName(TASMANIA_BM.getLocalPart()); GridCoverage2D original = (GridCoverage2D) ci.getGridCoverage(null, null); coverages.add(original); // the grid should not be swapped since the target output is expressed in EPSG:XYWZ form GridEnvelope originalRange = original.getGridGeometry().getGridRange(); GridEnvelope actualRange = result.getGridGeometry().getGridRange(); assertEquals(originalRange.getSpan(0), actualRange.getSpan(0)); assertEquals(originalRange.getSpan(1), actualRange.getSpan(1)); // check also the geographic bounds Envelope2D originalEnv = original.getEnvelope2D(); Envelope2D actualEnv = result.getEnvelope2D(); assertEquals(originalEnv.getMinX(), actualEnv.getMinX(), 1e-6); assertEquals(originalEnv.getMinY(), actualEnv.getMinY(), 1e-6); assertEquals(originalEnv.getMaxX(), actualEnv.getMaxX(), 1e-6); assertEquals(originalEnv.getMaxY(), actualEnv.getMaxY(), 1e-6); // cleanup tiffFile.delete(); }
From source file:org.apache.solr.handler.dataimport.FsMailEntityProcessor.java
public boolean addPartToDocument(Part part, Map<String, Object> row, boolean outerMost) throws Exception { if (outerMost && part instanceof Message) { if (!addEnvelopToDocument(part, row)) { return false; }//from w ww . j av a 2 s. c o m // store hash row.put(HASH, DigestUtils.md5Hex((String) row.get(FROM_CLEAN) + "" + (String) row.get(SUBJECT))); } String ct = part.getContentType(); ContentType ctype = new ContentType(ct); if (part.isMimeType("multipart/*")) { Multipart mp = (Multipart) part.getContent(); int count = mp.getCount(); if (part.isMimeType("multipart/alternative")) { count = 1; } for (int i = 0; i < count; i++) { addPartToDocument(mp.getBodyPart(i), row, false); } } else if (part.isMimeType("message/rfc822")) { addPartToDocument((Part) part.getContent(), row, false); } else { String disp = part.getDisposition(); @SuppressWarnings("resource") // Tika will close stream InputStream is = part.getInputStream(); String fileName = part.getFileName(); Metadata md = new Metadata(); md.set(HttpHeaders.CONTENT_TYPE, ctype.getBaseType().toLowerCase(Locale.ROOT)); md.set(TikaMetadataKeys.RESOURCE_NAME_KEY, fileName); String content = this.tika.parseToString(is, md); if (disp != null && disp.equalsIgnoreCase(Part.ATTACHMENT)) { if (row.get(ATTACHMENT) == null) { row.put(ATTACHMENT, new ArrayList<String>()); } List<String> contents = (List<String>) row.get(ATTACHMENT); contents.add(content); row.put(ATTACHMENT, contents); if (row.get(ATTACHMENT_NAMES) == null) { row.put(ATTACHMENT_NAMES, new ArrayList<String>()); } List<String> names = (List<String>) row.get(ATTACHMENT_NAMES); names.add(fileName); row.put(ATTACHMENT_NAMES, names); } else { if (row.get(CONTENT) == null) { row.put(CONTENT, new ArrayList<String>()); } List<String> contents = (List<String>) row.get(CONTENT); contents.add(content); row.put(CONTENT, contents); } } return true; }