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.jaxb.attachment;
019:
020: import java.io.UnsupportedEncodingException;
021: import java.util.Collection;
022: import java.util.UUID;
023:
024: import javax.activation.DataHandler;
025: import javax.xml.bind.attachment.AttachmentMarshaller;
026:
027: import org.apache.cxf.attachment.AttachmentImpl;
028: import org.apache.cxf.attachment.AttachmentUtil;
029: import org.apache.cxf.attachment.ByteDataSource;
030: import org.apache.cxf.interceptor.Fault;
031: import org.apache.cxf.message.Attachment;
032:
033: public class JAXBAttachmentMarshaller extends AttachmentMarshaller {
034:
035: private static final int THRESH_HOLD = 10 * 1024;
036: private Collection<Attachment> atts;
037: private boolean isXop;
038:
039: public JAXBAttachmentMarshaller(Collection<Attachment> attachments) {
040: super ();
041:
042: atts = attachments;
043: isXop = attachments != null;
044: }
045:
046: public String addMtomAttachment(byte[] data, int offset,
047: int length, String mimeType, String elementNS,
048: String elementLocalName) {
049:
050: if (!isXop && length < THRESH_HOLD) {
051: return null;
052: }
053: ByteDataSource source = new ByteDataSource(data, offset, length);
054: if (mimeType != null) {
055: source.setContentType(mimeType);
056: } else {
057: source.setContentType("application/octet-stream");
058: }
059: DataHandler handler = new DataHandler(source);
060:
061: String id;
062: try {
063: id = AttachmentUtil.createContentID(elementNS);
064: } catch (UnsupportedEncodingException e) {
065: throw new Fault(e);
066: }
067: AttachmentImpl att = new AttachmentImpl(id, handler);
068: att.setXOP(this .isXop);
069: atts.add(att);
070:
071: return "cid:" + id;
072: }
073:
074: public String addMtomAttachment(DataHandler handler,
075: String elementNS, String elementLocalName) {
076:
077: if (!isXop) {
078: return null;
079: }
080:
081: String id;
082: try {
083: id = AttachmentUtil.createContentID(elementNS);
084: } catch (UnsupportedEncodingException e) {
085: throw new Fault(e);
086: }
087: AttachmentImpl att = new AttachmentImpl(id, handler);
088: att.setXOP(this .isXop);
089: atts.add(att);
090:
091: return "cid:" + id;
092: }
093:
094: @Override
095: public String addSwaRefAttachment(DataHandler handler) {
096: String id = UUID.randomUUID() + "@apache.org";
097: AttachmentImpl att = new AttachmentImpl(id, handler);
098: att.setXOP(false);
099: atts.add(att);
100: return id;
101: }
102:
103: public void setXOPPackage(boolean xop) {
104: this .isXop = xop;
105: }
106:
107: @Override
108: public boolean isXOPPackage() {
109: return isXop;
110: }
111: }
|