private static void readMultiPart(MultipartResult res, MimeMultipart multipart) throws MessagingException { for (int i = 0; i < multipart.getCount(); i++) { BodyPart part = multipart.getBodyPart(i); try { if (part.isMimeType("image/*") || part.isMimeType("application/*")) continue; Object content = null; try { content = part.getContent(); } catch (UnsupportedEncodingException ex) { String body = ConvertUtil.inputStreamToString(part.getInputStream()); res.body = body; continue; } if (part.isMimeType("text/plain")) { res.body = content.toString(); } else if (part.isMimeType("text/*")) { res.bodyHtml = content.toString(); } else if (content instanceof MimeMultipart) { readMultiPart(res, (MimeMultipart) content); } else if (content instanceof IMAPNestedMessage) { res.body = getBody((IMAPNestedMessage) content); return; } else if (content instanceof InputStream) { if (content instanceof IMAPInputStream) { String body = ConvertUtil.inputStreamToString(part.getInputStream()); res.body = body; } else System.out.println( String.format( "Ignoring binary content in mail: %s [%s]", part.getContentType(), content.getClass())); } else if (part.isMimeType("message/*")) { res.body = content.toString(); } else { System.out.println( String.format( "Unknown content type in mail: %s [%s]", part.getContentType(), content.getClass())); } } catch (IllegalStateException ex) { System.out.println( String.format("Could not read contents in mail: %s", part.getContentType())); } catch (UnsupportedDataTypeException ex) { System.out.println( String.format("Could not read contents in mail: %s", part.getContentType())); } catch (FolderClosedException ex) { throw ex; } catch (Throwable ex) { System.out.println( String.format("Error while reading mail part: %s", part.getClass().toString())); } } }
public static String getBody(Message message) throws Exception { try { message.getContent(); // This might throw an exception... } catch (Throwable ex) { try { // Content could not be resolved for some reason - just get the raw stuff return ConvertUtil.inputStreamToString(message.getInputStream()); } catch (Throwable ex2) { // bodystructure is corrupt - get the really raw stuff ByteArrayOutputStream out = new ByteArrayOutputStream(); message.writeTo(out); return out.toString(); } } // check if message is a normal message or a Multipart message if (message.getContent() instanceof MimeMultipart) { MultipartResult res = new MailUtils.MultipartResult(); MailUtils.readMultiPart(res, (MimeMultipart) message.getContent()); return res.body.isEmpty() ? res.bodyHtml : res.body; } else { if (message.getContent().toString() != null) { return message.getContent().toString(); } } return null; }
@Override public void propertyChanged(List<PropertyList> path, PropertyUnit property) { super.propertyChanged(path, property); String str = PropertyUtil.toString(path, property); if (PropertyConstants.EDGE_PROPERTY_ROUNGING.equals(str)) { rounding = ConvertUtil.object2int(property.getValue()); } }
/** * 对 dataMap 的内容进行签名,并将签名结果放入 dataMap 中 * * @param dataMap 待签名kv * @param securityCheckKey 安全密钥 * @param de * @param encoding * @return */ public static void check( HttpServletRequest request, String securityCheckKey, DigestALGEnum de, String encoding) { Map<String, String[]> paramMap = request.getParameterMap(); check(ConvertUtil.toMap(paramMap), securityCheckKey, de, encoding); }
public static void form2To(ModelObject dest, Map<String, Object> orig) { debug("form2To-- props:" + orig); try { HashMap<String, Object> _props = new HashMap<String, Object>(); PropertyDescriptor srcDescriptors[] = BeanUtilsBean.getInstance().getPropertyUtils().getPropertyDescriptors(dest); for (PropertyDescriptor pd : srcDescriptors) { String fn = pd.getName(); if ("class".equals(fn)) { continue; } Class type = pd.getPropertyType(); debug("fn:" + fn + " type:" + type); if (!orig.containsKey(fn)) { continue; } Object value = orig.get(fn); if (value == null) { _props.put(fn, value); continue; } if (ModelObject.class.isAssignableFrom(type)) { String bean = type.getSimpleName(); if (value instanceof String) { // the value is ID debug("form2To-- type=" + type); ModelObject mo = (ModelObject) type.newInstance(); mo.setModelId((String) value); // ModelObject mo = getModelObject(bean, (String)value); _props.put(fn, mo); } else if (value instanceof Map) { Map<String, Object> cprops = (Map<String, Object>) value; ModelObject mo = (ModelObject) type.newInstance(); form2To(mo, cprops); _props.put(fn, mo); } else { throw new RuntimeException( "Unknown value type: " + value + "," + value.getClass() + " bean:" + bean); } } else if (Collection.class.isAssignableFrom(type)) { Collection col = null; if (value != null) { if (MyPropertyUtil.isFieldCollectionOfModel(dest.getClass().getDeclaredField(fn))) { // NOTE: this means always Add/Update child ModelObject separately continue; } else { // String bean = config.getItemType(obj.getModelName(), fn); if (value instanceof String) { col = (Collection) PropertyUtils.getProperty(dest, fn); String val = (String) value; if (((String) val).indexOf(",") > 0) { // for properties like Goods.categoryIds which is internally list of String, // and submitted in the format of comma-separated string // TODO escape "," String[] values = ConvertUtil.split(val, ","); col.addAll(Arrays.asList(values)); } else { col.add(value); } } else if (value instanceof Collection) { Collection c = (Collection) value; col = (Collection) PropertyUtils.getProperty(dest, fn); debug("size: " + c.size()); col.addAll(c); // _props.put(fn, c); } } // else { //// throw new RuntimeException("Unknown value type: " + // value+","+value.getClass()+" bean:"+bean); // throw new RuntimeException("Unknown value type: " + // value+","+value.getClass()); // } } else { // keep the old value col = (Collection) PropertyUtils.getProperty(dest, fn); } // _props.put(fn, col); } else { _props.put(fn, value); } } debug("form2To-- _props:" + _props); BeanUtils.populate(dest, _props); } catch (Exception e) { e.printStackTrace(); } }