ohair@286: /* alanb@368: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.xml.internal.ws.encoding; ohair@286: ohair@286: import com.sun.xml.internal.ws.api.SOAPVersion; ohair@286: import com.sun.xml.internal.ws.api.WSFeatureList; ohair@286: import com.sun.xml.internal.ws.api.message.Attachment; ohair@286: import com.sun.xml.internal.ws.api.message.AttachmentEx; ohair@286: import com.sun.xml.internal.ws.api.message.Message; ohair@286: import com.sun.xml.internal.ws.api.message.Packet; ohair@286: import com.sun.xml.internal.ws.api.pipe.Codec; ohair@286: import com.sun.xml.internal.ws.api.pipe.ContentType; ohair@286: import com.sun.xml.internal.ws.developer.StreamingAttachmentFeature; ohair@286: ohair@286: import java.io.IOException; ohair@286: import java.io.InputStream; ohair@286: import java.io.OutputStream; ohair@286: import java.nio.channels.ReadableByteChannel; ohair@286: import java.util.Iterator; ohair@286: import java.util.UUID; ohair@286: ohair@286: /** ohair@286: * {@link Codec}s that uses the MIME multipart as the underlying format. ohair@286: * ohair@286: *

ohair@286: * When the runtime needs to dynamically choose a {@link Codec}, and ohair@286: * when there are more than one {@link Codec}s that use MIME multipart, ohair@286: * it is often impossible to determine the right {@link Codec} unless ohair@286: * you parse the multipart message to some extent. ohair@286: * ohair@286: *

ohair@286: * By having all such {@link Codec}s extending from this class, ohair@286: * the "sniffer" can decode a multipart message partially, and then ohair@286: * pass the partial parse result to the ultimately-responsible {@link Codec}. ohair@286: * This improves the performance. ohair@286: * ohair@286: * @author Kohsuke Kawaguchi ohair@286: */ ohair@286: abstract class MimeCodec implements Codec { ohair@286: ohair@286: public static final String MULTIPART_RELATED_MIME_TYPE = "multipart/related"; ohair@286: alanb@368: protected Codec mimeRootCodec; ohair@286: protected final SOAPVersion version; ohair@286: protected final WSFeatureList features; ohair@286: ohair@286: protected MimeCodec(SOAPVersion version, WSFeatureList f) { ohair@286: this.version = version; ohair@286: this.features = f; ohair@286: } ohair@286: ohair@286: public String getMimeType() { ohair@286: return MULTIPART_RELATED_MIME_TYPE; ohair@286: } ohair@286: alanb@368: protected Codec getMimeRootCodec(Packet packet) { alanb@368: return mimeRootCodec; alanb@368: } alanb@368: ohair@286: // TODO: preencode String literals to byte[] so that they don't have to ohair@286: // go through char[]->byte[] conversion at runtime. ohair@286: public ContentType encode(Packet packet, OutputStream out) throws IOException { ohair@286: Message msg = packet.getMessage(); ohair@286: if (msg == null) { ohair@286: return null; ohair@286: } alanb@368: ContentTypeImpl ctImpl = (ContentTypeImpl)getStaticContentType(packet); alanb@368: String boundary = ctImpl.getBoundary(); alanb@368: boolean hasAttachments = (boundary != null); alanb@368: Codec rootCodec = getMimeRootCodec(packet); ohair@286: if (hasAttachments) { ohair@286: writeln("--"+boundary, out); ohair@286: ContentType ct = rootCodec.getStaticContentType(packet); ohair@286: String ctStr = (ct != null) ? ct.getContentType() : rootCodec.getMimeType(); ohair@286: writeln("Content-Type: " + ctStr, out); ohair@286: writeln(out); ohair@286: } ohair@286: ContentType primaryCt = rootCodec.encode(packet, out); ohair@286: ohair@286: if (hasAttachments) { ohair@286: writeln(out); ohair@286: // Encode all the attchments ohair@286: for (Attachment att : msg.getAttachments()) { ohair@286: writeln("--"+boundary, out); ohair@286: //SAAJ's AttachmentPart.getContentId() returns content id already enclosed with ohair@286: //angle brackets. For now put angle bracket only if its not there ohair@286: String cid = att.getContentId(); ohair@286: if(cid != null && cid.length() >0 && cid.charAt(0) != '<') ohair@286: cid = '<' + cid + '>'; ohair@286: writeln("Content-Id:" + cid, out); ohair@286: writeln("Content-Type: " + att.getContentType(), out); ohair@286: writeCustomMimeHeaders(att, out); ohair@286: writeln("Content-Transfer-Encoding: binary", out); ohair@286: writeln(out); // write \r\n ohair@286: att.writeTo(out); ohair@286: writeln(out); // write \r\n ohair@286: } ohair@286: writeAsAscii("--"+boundary, out); ohair@286: writeAsAscii("--", out); ohair@286: } ohair@286: // TODO not returing correct multipart/related type(no boundary) alanb@368: return hasAttachments ? ctImpl : primaryCt; ohair@286: } ohair@286: ohair@286: private void writeCustomMimeHeaders(Attachment att, OutputStream out) throws IOException { ohair@286: if (att instanceof AttachmentEx) { ohair@286: Iterator allMimeHeaders = ((AttachmentEx) att).getMimeHeaders(); ohair@286: while (allMimeHeaders.hasNext()) { ohair@286: AttachmentEx.MimeHeader mh = allMimeHeaders.next(); ohair@286: String name = mh.getName(); ohair@286: ohair@286: if (!"Content-Type".equalsIgnoreCase(name) && !"Content-Id".equalsIgnoreCase(name)) { ohair@286: writeln(name +": " + mh.getValue(), out); ohair@286: } ohair@286: } ohair@286: } ohair@286: } ohair@286: ohair@286: public ContentType getStaticContentType(Packet packet) { alanb@368: ContentType ct = (ContentType) packet.getInternalContentType(); alanb@368: if ( ct != null ) return ct; ohair@286: Message msg = packet.getMessage(); alanb@368: boolean hasAttachments = !msg.getAttachments().isEmpty(); alanb@368: Codec rootCodec = getMimeRootCodec(packet); ohair@286: ohair@286: if (hasAttachments) { alanb@368: String boundary = "uuid:" + UUID.randomUUID().toString(); ohair@286: String boundaryParameter = "boundary=\"" + boundary + "\""; ohair@286: // TODO use primaryEncoder to get type alanb@368: String messageContentType = MULTIPART_RELATED_MIME_TYPE + ohair@286: "; type=\"" + rootCodec.getMimeType() + "\"; " + ohair@286: boundaryParameter; alanb@368: ContentTypeImpl impl = new ContentTypeImpl(messageContentType, packet.soapAction, null); alanb@368: impl.setBoundary(boundary); alanb@368: impl.setBoundaryParameter(boundaryParameter); alanb@368: packet.setContentType(impl); alanb@368: return impl; ohair@286: } else { alanb@368: ct = rootCodec.getStaticContentType(packet); alanb@368: packet.setContentType(ct); alanb@368: return ct; ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * Copy constructor. ohair@286: */ ohair@286: protected MimeCodec(MimeCodec that) { ohair@286: this.version = that.version; ohair@286: this.features = that.features; ohair@286: } ohair@286: ohair@286: public void decode(InputStream in, String contentType, Packet packet) throws IOException { ohair@286: MimeMultipartParser parser = new MimeMultipartParser(in, contentType, features.get(StreamingAttachmentFeature.class)); ohair@286: decode(parser,packet); ohair@286: } ohair@286: ohair@286: public void decode(ReadableByteChannel in, String contentType, Packet packet) { ohair@286: throw new UnsupportedOperationException(); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Parses a {@link Packet} from a {@link MimeMultipartParser}. ohair@286: */ ohair@286: protected abstract void decode(MimeMultipartParser mpp, Packet packet) throws IOException; ohair@286: ohair@286: public abstract MimeCodec copy(); ohair@286: ohair@286: ohair@286: public static void writeln(String s,OutputStream out) throws IOException { ohair@286: writeAsAscii(s,out); ohair@286: writeln(out); ohair@286: } ohair@286: ohair@286: /** ohair@286: * Writes a string as ASCII string. ohair@286: */ ohair@286: public static void writeAsAscii(String s,OutputStream out) throws IOException { ohair@286: int len = s.length(); ohair@286: for( int i=0; i