Return a mimemultipart from raw Content
FIXME Doesn't support nested multipart in the body content
@param contentTypeHeader content type header related to the rawContent
@param rawContent body content
@return a mimemultipart from raw Content
336236
private static MimeMultipart getContentAsMimeMultipart(ContentTypeHeader contentTypeHeader, byte[] rawContent) {
		// Issue 1123 : http://code.google.com/p/mobicents/issues/detail?id=1123 : Multipart type is supported
		String delimiter = contentTypeHeader.getParameter(MULTIPART_BOUNDARY);
		String start = contentTypeHeader.getParameter(MULTIPART_START);

		MimeMultipart mimeMultipart = new MimeMultipart(contentTypeHeader.getContentSubType());
		if (delimiter == null) {
			MimeBodyPart mbp = new MimeBodyPart();
		    DataSource ds = new ByteArrayDataSource(rawContent, contentTypeHeader.getContentSubType());
		    try {
				mbp.setDataHandler(new DataHandler(ds));
				mimeMultipart.addBodyPart(mbp);
			} catch (MessagingException e) {
				throw new IllegalArgumentException("couldn't create the multipart object from the message content " + rawContent, e);
			}
		} else {
			// splitting the body content by delimiter
		    String[] fragments = new String(rawContent).split(MULTIPART_BOUNDARY_DELIM + delimiter);
			for (String fragment: fragments) {
				final String trimmedFragment = fragment.trim();
				// skipping empty fragment and ending fragment looking like --
				if(trimmedFragment.length() > 0 && !MULTIPART_BOUNDARY_DELIM.equals(trimmedFragment)) {
					String fragmentHeaders = null;
					String fragmentBody = fragment;
					// if there is a start, it means that there is probably headers before the content that need to be added to the mime body part
					// so we split headers from body content
					if(start != null && start.length() > 0) {
						int indexOfStart = fragment.indexOf(start);
						if(indexOfStart != -1) {
							fragmentHeaders = fragmentBody.substring(0, indexOfStart + start.length());
							fragmentBody = fragmentBody.substring(indexOfStart + start.length()).trim();
						}
					}
					MimeBodyPart mbp = new MimeBodyPart();
				    try {
				    	String contentType = contentTypeHeader.getContentSubType();
				    	// check if the body content start with a Content-Type header
				    	// if so we strip it from the content body
				    	if(fragmentBody.startsWith(ContentTypeHeader.NAME)) {
				    		int indexOfLineReturn =  fragmentBody.indexOf(LINE_RETURN_DELIM);
				    		contentType = fragmentBody.substring(0, indexOfLineReturn -1).trim();
				    		fragmentBody = fragmentBody.substring(indexOfLineReturn).trim();
				    	}
				    	// setting the content body stripped from the headers
				    	mbp.setContent(fragmentBody, contentType);
				    	// adding the headers to the body part
				    	mbp.addHeaderLine(contentType);
				    	if(fragmentHeaders != null) {
				    		StringTokenizer stringTokenizer = new StringTokenizer(fragmentHeaders, LINE_RETURN_DELIM);
				    		while (stringTokenizer.hasMoreTokens()) {
				    			String token = stringTokenizer.nextToken().trim();
				    			if(token != null && token.length() > 0) {
				    				mbp.addHeaderLine(token);
				    			}
							}
				    	}
						mimeMultipart.addBodyPart(mbp);
					} catch (MessagingException e) {
						throw new IllegalArgumentException("couldn't create the multipart object from the message content " + rawContent, e);
					}
				}
			}
		}
		return mimeMultipart;
	}
1002201
public static JinxConstants.ContentType flickrContentTypeIdToContentType(int type) {
        JinxConstants.ContentType ret;
        switch (type) {
            case 1:
                ret = JinxConstants.ContentType.photo;
                break;
            case 2:
                ret = JinxConstants.ContentType.screenshot;
                break;
            case 3:
                ret = JinxConstants.ContentType.other;
                break;
            case 4:
                ret = JinxConstants.ContentType.photos_and_screenshots;
                break;
            case 5:
                ret = JinxConstants.ContentType.screenshots_and_other;
                break;
            case 6:
                ret = JinxConstants.ContentType.photos_and_other;
                break;
            case 7:
                ret = JinxConstants.ContentType.all;
                break;
            default:
                ret = null;
                break;
        }
        return ret;
    }
1282874
@Override
    public RequestContent getRequestContent(HttpServletRequest request,
                                            HttpHeaders headers)
        throws Exception {
        RequestContent rContent = null;

        // See if the request is a multi-part file upload request
        if (ServletFileUpload.isMultipartContent(request)) {

            // Create a new file upload handler
            ServletFileUpload upload = new ServletFileUpload();

            // Parse the request, use the first available File item
            FileItemIterator iter = upload.getItemIterator(request);
            while (iter.hasNext()) {
                FileItemStream item = iter.next();
                if (!item.isFormField()) {
                    rContent = new RequestContent();
                    rContent.contentStream = item.openStream();
                    rContent.mimeType = item.getContentType();

                    FileItemHeaders itemHeaders = item.getHeaders();
                    if (itemHeaders != null) {
                        String contentLength =
                            itemHeaders.getHeader("Content-Length");
                        if (contentLength != null) {
                            rContent.size = Long.parseLong(contentLength);
                        }
                    }

                    break;
                }
            }
        } else {
            // If the content stream was not found as a multipart,
            // try to use the stream from the request directly
            rContent = new RequestContent();
            rContent.contentStream = request.getInputStream();
            if (request.getContentLength() >= 0) {
                rContent.size = request.getContentLength();
            }
        }

        // Attempt to set the mime type and size if not already set
        if (rContent != null) {
            if (rContent.mimeType == null) {
                MediaType mediaType = headers.getMediaType();
                if (mediaType != null) {
                    rContent.mimeType = mediaType.toString();
                }
            }

            if (rContent.size == 0) {
                List<String> lengthHeaders =
                    headers.getRequestHeader("Content-Length");
                if (lengthHeaders != null && lengthHeaders.size() > 0) {
                    rContent.size = Long.parseLong(lengthHeaders.get(0));
                }
            }
        }

        return rContent;
    }
624336
public static boolean isMultipartContent (@Nullable final String sContentType)
  {
    return sContentType != null && sContentType.toLowerCase (Locale.US).startsWith (MULTIPART);
  }
341628
static String parseContentType(String contentType) throws IOException {
        if (contentType.indexOf(System.getProperty("line.separator")) > 0) {
            BufferedReader reader = new BufferedReader(new StringReader(contentType));

            try {
                String plainContentType = reader.readLine();
                if (plainContentType != null && plainContentType.trim().endsWith(";")) {
                    plainContentType = plainContentType.trim().substring(0, plainContentType.length() - 1);
                }

                return plainContentType;
            } finally {
                try {
                    reader.close();
                } catch (IOException e) {
                    log.warn("Failed to close reader", e);
                }
            }
        }

        return contentType;
    }
1066705
public function addPart($body, $contentType = null, $charset = null)
    {
        return $this->attach((new Swift_MimePart($body, $contentType, $charset))->setEncoder($this->getEncoder()));
    }
691658
private String initMultipartUpload(String bucketName, String objectName, Map<String, String> headerMap)
    throws InvalidBucketNameException, NoSuchAlgorithmException, InsufficientDataException, IOException,
           InvalidKeyException, NoResponseException, XmlPullParserException, ErrorResponseException,
           InternalException {
    // set content type if not set already
    if (headerMap.get("Content-Type") == null) {
      headerMap.put("Content-Type", "application/octet-stream");
    }

    Map<String,String> queryParamMap = new HashMap<>();
    queryParamMap.put("uploads", "");

    HttpResponse response = executePost(bucketName, objectName, headerMap, queryParamMap, "");

    InitiateMultipartUploadResult result = new InitiateMultipartUploadResult();
    result.parseXml(response.body().charStream());
    response.body().close();
    return result.uploadId();
  }
196748
public void setContent(Object content, String contentType)
			throws UnsupportedEncodingException {
		// https://code.google.com/p/sipservlets/issues/detail?id=202
		if(getSipSession().getProxy() == null) {
			checkMessageState();
		}
		checkContentType(contentType);
		checkCommitted();

		if(contentType != null && contentType.length() > 0) {
			this.addHeader(ContentTypeHeader.NAME, contentType);
			ContentTypeHeader contentTypeHeader = (ContentTypeHeader)this.message.getHeader(ContentTypeHeader.NAME);
			String charset = this.getCharacterEncoding();
			try {
			    // https://code.google.com/p/sipservlets/issues/detail?id=169
				if(contentType.contains(CONTENT_TYPE_MULTIPART) && content instanceof Multipart) {
					// Fix for Issue 2667 : Correct Handling of MimeMultipart
					Multipart multipart = (Multipart) content;
					OutputStream os = new ByteArrayOutputStream();
					multipart.writeTo(os);
					this.message.setContent(os.toString(), contentTypeHeader);
				} else {
					Object tmpContent = content;
					if(tmpContent instanceof String  && charset != null) {
						//test for unsupportedencoding exception
						new String("testEncoding".getBytes(charset));
						tmpContent = new String(((String)tmpContent).getBytes());
					}
					this.message.setContent(content, contentTypeHeader);
				}
			} catch (UnsupportedEncodingException uee) {
				throw uee;
			} catch (Exception e) {
				throw new IllegalArgumentException("Parse error reading content " + content + " with content type " + contentType, e);
			}
		}
	}
1190324
public static boolean isMultiPartForm(@CheckForNull String contentType) {
        if (contentType == null) {
            return false;
        }

        String[] parts = contentType.split(";");
        return ArrayUtils.contains(parts, "multipart/form-data");
    }
1062343
public Object getContent() throws IOException, UnsupportedEncodingException {
		ContentTypeHeader contentTypeHeader = (ContentTypeHeader)
 			this.message.getHeader(ContentTypeHeader.NAME);
		if(contentTypeHeader != null && logger.isDebugEnabled()) {
			logger.debug("Content type " + contentTypeHeader.getContentType());
			logger.debug("Content sub type " + contentTypeHeader.getContentSubType());
		}
		if(contentTypeHeader!= null && CONTENT_TYPE_TEXT.equals(contentTypeHeader.getContentType())) {
			String content = null;
			if(message.getRawContent() != null) {
				String charset = this.getCharacterEncoding();
				if(charset == null) {
					content = new String(message.getRawContent());
				} else {
					content = new String(message.getRawContent(), charset);
				}
			} else {
				content = "";
			}
			return content;
		} else if(contentTypeHeader!= null && CONTENT_TYPE_MULTIPART.equals(contentTypeHeader.getContentType())) {
			try {
				return new MimeMultipart(new ByteArrayDataSource(message.getRawContent(),
						contentTypeHeader.toString().replaceAll(ContentTypeHeader.NAME+": ", "")));
			} catch (MessagingException e) {
				logger.warn("Problem with multipart message.", e);
				return this.message.getRawContent();
			}
		} else {
			return this.message.getRawContent();
		}
	}
754571
public static HttpResponse request(HttpRequest request, String body, String contentType) {
        return request.contentType(contentType).body(body).contentLength(body.length()).execute();
    }
83059
public function addEmbeddedFile(string $file, string $content = null, string $contentType = null): MimePart
    {
        return $this->inlines[$file] = $this->createAttachment($file, $content, $contentType, 'inline')
            ->setHeader('Content-ID', $this->getRandomId());
    }
690092
protected BodyPart handleImageBinaryPart(MimePart image, String contentType) throws IOException, MessagingException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        FileCopyUtils.copy(image.getInputStream(), bos);
        String base64 = Base64.encodeBase64String(bos.toByteArray());
        return new BodyPart(base64, contentType);
    }
1097311
public function addAttachment(string $file, string $content = null, string $contentType = null): MimePart
    {
        return $this->attachments[] = $this->createAttachment($file, $content, $contentType, 'attachment');
    }
120252
protected function _sendRawPost($url, $rawPost, $contentType = 'text/xml; charset=UTF-8')
    {
        $initializeRequest = function(Request $request) use ($rawPost, $contentType) {
            $request->setRawData($rawPost);
            $request->addHeader('Content-Type: ' . $contentType);
            return $request;
        };

        return $this->_sendRawRequest($url, Request::METHOD_POST, $rawPost, $initializeRequest);
    }
878720
public ContentType getContentType() {
        String contentType = m_headers.get(HttpDefs.CONTENT_TYPE);
        if (contentType == null) {
            return null;
        }
        return new ContentType(contentType);
    }
485347
public Attachment uploadAttachment(String contentType, File content)
            throws RedmineException, IOException {
        try (InputStream is = new FileInputStream(content)) {
            return uploadAttachment(content.getName(), contentType, is, content.length());
        }
    }