01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */package org.apache.cxf.jaxb.attachment;
19:
20: import java.io.ByteArrayOutputStream;
21: import java.io.IOException;
22: import java.io.InputStream;
23: import java.io.UnsupportedEncodingException;
24: import java.net.MalformedURLException;
25: import java.net.URL;
26: import java.net.URLDecoder;
27: import java.util.Collection;
28: import java.util.logging.Logger;
29:
30: import javax.activation.DataHandler;
31: import javax.activation.DataSource;
32: import javax.activation.URLDataSource;
33: import javax.xml.bind.attachment.AttachmentUnmarshaller;
34:
35: import org.apache.cxf.attachment.LazyDataSource;
36: import org.apache.cxf.common.logging.LogUtils;
37: import org.apache.cxf.helpers.IOUtils;
38: import org.apache.cxf.interceptor.Fault;
39: import org.apache.cxf.message.Attachment;
40:
41: public class JAXBAttachmentUnmarshaller extends AttachmentUnmarshaller {
42: private static final Logger LOG = LogUtils
43: .getL7dLogger(JAXBAttachmentUnmarshaller.class);
44:
45: private Collection<Attachment> attachments;
46:
47: public JAXBAttachmentUnmarshaller(Collection<Attachment> attachments) {
48: super ();
49: this .attachments = attachments;
50: }
51:
52: @Override
53: public DataHandler getAttachmentAsDataHandler(String contentId) {
54: return new DataHandler(getAttachmentDataSource(contentId));
55: }
56:
57: @Override
58: public byte[] getAttachmentAsByteArray(String contentId) {
59: ByteArrayOutputStream bos = new ByteArrayOutputStream();
60: try {
61: InputStream is = getAttachmentDataSource(contentId)
62: .getInputStream();
63: IOUtils.copy(is, bos);
64: is.close();
65: bos.close();
66: } catch (IOException e) {
67: throw new Fault(new org.apache.cxf.common.i18n.Message(
68: "ATTACHMENT_READ_ERROR", LOG), e);
69: }
70: return bos.toByteArray();
71: }
72:
73: @Override
74: public boolean isXOPPackage() {
75: return attachments != null;
76: }
77:
78: private DataSource getAttachmentDataSource(String contentId) {
79: // Is this right? - DD
80: if (contentId.startsWith("cid:")) {
81: try {
82: contentId = URLDecoder.decode(contentId.substring(4),
83: "UTF-8");
84: } catch (UnsupportedEncodingException ue) {
85: contentId = contentId.substring(4);
86: }
87: return new LazyDataSource(contentId, attachments);
88: } else if (contentId.indexOf("://") == -1) {
89: return new LazyDataSource(contentId, attachments);
90: } else {
91: try {
92: return new URLDataSource(new URL(contentId));
93: } catch (MalformedURLException e) {
94: throw new Fault(e);
95: }
96: }
97:
98: }
99: }
|