001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.attachment;
019:
020: import java.io.IOException;
021: import java.io.OutputStream;
022: import java.io.StringWriter;
023: import java.io.Writer;
024:
025: import org.apache.cxf.message.Attachment;
026: import org.apache.cxf.message.Message;
027:
028: public class AttachmentSerializer {
029:
030: private static final String BODY_ATTACHMENT_ID = "root.message@cxf.apache.org";
031: private Message message;
032: private String bodyBoundary;
033: private OutputStream out;
034: private String encoding;
035: private boolean xop = true;
036:
037: public AttachmentSerializer(Message messageParam) {
038: message = messageParam;
039: }
040:
041: /**
042: * Serialize the beginning of the attachment which includes the MIME
043: * beginning and headers for the root message.
044: */
045: public void writeProlog() throws IOException {
046: // Create boundary for body
047: bodyBoundary = AttachmentUtil.getUniqueBoundaryValue(0);
048:
049: String bodyCt = (String) message.get(Message.CONTENT_TYPE);
050: bodyCt = bodyCt.replaceAll("\"", "\\\"");
051: String enc = (String) message.get(Message.ENCODING);
052: if (enc == null) {
053: enc = "UTF-8";
054: }
055:
056: // Set transport mime type
057: StringBuilder ct = new StringBuilder();
058: ct.append("multipart/related; ");
059: if (xop) {
060: ct.append("type=\"application/xop+xml\"; ");
061: }
062:
063: ct.append("boundary=\"").append(bodyBoundary).append("\"; ")
064: .append("start=\"<").append(BODY_ATTACHMENT_ID).append(
065: ">\"; ").append("start-info=\"").append(bodyCt)
066: .append("\"");
067:
068: message.put(Message.CONTENT_TYPE, ct.toString());
069:
070: // 2. write headers
071: out = message.getContent(OutputStream.class);
072: encoding = (String) message.get(Message.ENCODING);
073: if (encoding == null) {
074: encoding = "UTF-8";
075: }
076: StringWriter writer = new StringWriter();
077: writer.write("\r\n");
078: writer.write("--");
079: writer.write(bodyBoundary);
080:
081: StringBuilder mimeBodyCt = new StringBuilder();
082: mimeBodyCt.append("application/xop+xml; charset=").append(enc)
083: .append("; type=\"").append(bodyCt)
084: .append("; charset=").append(enc).append("\"");
085:
086: writeHeaders(mimeBodyCt.toString(), BODY_ATTACHMENT_ID, writer);
087: out.write(writer.getBuffer().toString().getBytes(encoding));
088: }
089:
090: private void writeHeaders(String contentType, String attachmentId,
091: Writer writer) throws IOException {
092: writer.write("\r\n");
093: writer.write("Content-Type: ");
094: writer.write(contentType);
095: writer.write("\r\n");
096:
097: writer.write("Content-Transfer-Encoding: binary\r\n");
098:
099: writer.write("Content-ID: <");
100: writer.write(attachmentId);
101: writer.write(">\r\n\r\n");
102: }
103:
104: /**
105: * Write the end of the body boundary and any attachments included.
106: * @throws IOException
107: */
108: public void writeAttachments() throws IOException {
109: if (message.getAttachments() != null) {
110: for (Attachment a : message.getAttachments()) {
111: StringWriter writer = new StringWriter();
112: writer.write("\r\n");
113: writer.write("--");
114: writer.write(bodyBoundary);
115: writeHeaders(a.getDataHandler().getContentType(), a
116: .getId(), writer);
117: out.write(writer.getBuffer().toString().getBytes(
118: encoding));
119:
120: a.getDataHandler().writeTo(out);
121: }
122: }
123: StringWriter writer = new StringWriter();
124: writer.write("\r\n");
125: writer.write("--");
126: writer.write(bodyBoundary);
127: writer.write("--");
128: out.write(writer.getBuffer().toString().getBytes(encoding));
129: out.flush();
130: }
131:
132: public boolean isXop() {
133: return xop;
134: }
135:
136: public void setXop(boolean xop) {
137: this.xop = xop;
138: }
139:
140: }
|