001: /*
002: * Copyright (c) 1998-2007 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Emil Ong
028: */
029:
030: package com.caucho.jaxb.property;
031:
032: import java.security.*;
033: import java.lang.reflect.*;
034:
035: import java.io.*;
036: import java.net.URI;
037: import java.net.URISyntaxException;
038: import java.util.HashMap;
039: import java.util.logging.Logger;
040: import java.util.logging.Level;
041:
042: import javax.activation.DataContentHandler;
043: import javax.activation.DataHandler;
044: import javax.activation.CommandInfo;
045: import javax.activation.CommandMap;
046:
047: import javax.mail.util.ByteArrayDataSource;
048:
049: import javax.xml.XMLConstants;
050: import javax.xml.bind.JAXBException;
051: import javax.xml.bind.Marshaller;
052: import javax.xml.bind.MarshalException;
053: import javax.xml.bind.Unmarshaller;
054: import javax.xml.bind.UnmarshalException;
055: import javax.xml.namespace.QName;
056: import javax.xml.stream.XMLStreamException;
057: import javax.xml.stream.XMLStreamReader;
058: import javax.xml.stream.XMLStreamWriter;
059:
060: import org.w3c.dom.Document;
061: import org.w3c.dom.Node;
062:
063: import com.caucho.jaxb.JAXBUtil;
064: import com.caucho.jaxb.BinderImpl;
065:
066: import com.caucho.util.Attachment;
067: import com.caucho.util.Base64;
068: import com.caucho.util.L10N;
069:
070: /**
071: * DataHandler property.
072: *
073: * Note that DataHandler fields/properties are not affected by XmlMimeType
074: * annotations.
075: */
076: public class DataHandlerProperty extends CDataProperty implements
077: AttachmentProperty {
078: public static final QName SCHEMA_TYPE = new QName(
079: XMLConstants.W3C_XML_SCHEMA_NS_URI, "base64Binary", "xsd");
080:
081: private static final L10N L = new L10N(DataHandlerProperty.class);
082: private static final Logger log = Logger
083: .getLogger(DataHandlerProperty.class.getName());
084:
085: public static final DataHandlerProperty PROPERTY = new DataHandlerProperty();
086: private static final String DEFAULT_DATA_HANDLER_MIME_TYPE = "application/octet-stream";
087:
088: protected Object read(String in) throws IOException, JAXBException {
089: byte[] buffer = Base64.decodeToByteArray(in);
090:
091: ByteArrayDataSource bads = new ByteArrayDataSource(buffer,
092: DEFAULT_DATA_HANDLER_MIME_TYPE);
093:
094: return new DataHandler(bads);
095: }
096:
097: public String write(Object value) throws IOException, JAXBException {
098: if (value != null) {
099: DataHandler handler = (DataHandler) value;
100: ByteArrayOutputStream baos = new ByteArrayOutputStream();
101:
102: handler.writeTo(baos);
103:
104: return Base64.encodeFromByteArray(baos.toByteArray());
105: }
106:
107: return "";
108: }
109:
110: public QName getSchemaType() {
111: return SCHEMA_TYPE;
112: }
113:
114: public String getMimeType(Object obj) {
115: return ((DataHandler) obj).getContentType();
116: }
117:
118: public void writeAsAttachment(Object obj, OutputStream out)
119: throws IOException {
120: DataHandler handler = (DataHandler) obj;
121: handler
122: .setCommandMap(com.caucho.xml.saaj.SAAJCommandMap.COMMAND_MAP);
123:
124: handler.writeTo(out);
125: }
126:
127: public Object readFromAttachment(Attachment attachment)
128: throws IOException {
129: String mimeType = attachment.getHeaderValue("Content-Type");
130:
131: if (mimeType == null) {
132: mimeType = DEFAULT_DATA_HANDLER_MIME_TYPE;
133:
134: if (log.isLoggable(Level.FINER)) {
135: log
136: .finer("No Content-Type specified for DataHandler attachment, "
137: + "assuming "
138: + DEFAULT_DATA_HANDLER_MIME_TYPE);
139: }
140: }
141:
142: ByteArrayDataSource source = new ByteArrayDataSource(attachment
143: .getRawContents(), mimeType);
144:
145: return new DataHandler(source);
146: }
147: }
|