1 /* 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.xml.internal.messaging.saaj.soap; 27 28 import java.awt.datatransfer.DataFlavor; 29 import java.io.*; 30 31 import javax.activation.*; 32 import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.MimeUtility; 33 import com.sun.xml.internal.messaging.saaj.packaging.mime.internet.ContentType; 34 35 /** 36 * JAF data content handler for text/plain --> String 37 * 38 */ 39 public class StringDataContentHandler implements DataContentHandler { 40 private static ActivationDataFlavor myDF = new ActivationDataFlavor( 41 java.lang.String.class, 42 "text/plain", 43 "Text String"); 44 45 protected ActivationDataFlavor getDF() { 46 return myDF; 47 } 48 49 /** 50 * Return the DataFlavors for this <code>DataContentHandler</code>. 51 * 52 * @return The DataFlavors 53 */ 54 public DataFlavor[] getTransferDataFlavors() { 55 return new DataFlavor[] { getDF() }; 56 } 57 58 /** 59 * Return the Transfer Data of type DataFlavor from InputStream. 60 * 61 * @param df The DataFlavor 62 * @param ds The DataSource corresponding to the data 63 * @return String object 64 */ 65 public Object getTransferData(DataFlavor df, DataSource ds) 66 throws IOException { 67 // use myDF.equals to be sure to get ActivationDataFlavor.equals, 68 // which properly ignores Content-Type parameters in comparison 69 if (getDF().equals(df)) 70 return getContent(ds); 71 else 72 return null; 73 } 74 75 public Object getContent(DataSource ds) throws IOException { 76 String enc = null; 77 InputStreamReader is = null; 78 79 try { 80 enc = getCharset(ds.getContentType()); 81 is = new InputStreamReader(ds.getInputStream(), enc); 82 } catch (IllegalArgumentException iex) { 83 /* 84 * An unknown charset of the form ISO-XXX-XXX will cause 85 * the JDK to throw an IllegalArgumentException. The 86 * JDK will attempt to create a classname using this string, 87 * but valid classnames must not contain the character '-', 88 * and this results in an IllegalArgumentException, rather than 89 * the expected UnsupportedEncodingException. Yikes. 90 */ 91 throw new UnsupportedEncodingException(enc); 92 } 93 94 try { 95 int pos = 0; 96 int count; 97 char buf[] = new char[1024]; 98 99 while ((count = is.read(buf, pos, buf.length - pos)) != -1) { 100 pos += count; 101 if (pos >= buf.length) { 102 int size = buf.length; 103 if (size < 256*1024) 104 size += size; 105 else 106 size += 256*1024; 107 char tbuf[] = new char[size]; 108 System.arraycopy(buf, 0, tbuf, 0, pos); 109 buf = tbuf; 110 } 111 } 112 return new String(buf, 0, pos); 113 } finally { 114 try { 115 is.close(); 116 } catch (IOException ex) { } 117 } 118 } 119 120 /** 121 * Write the object to the output stream, using the specified MIME type. 122 */ 123 public void writeTo(Object obj, String type, OutputStream os) 124 throws IOException { 125 if (!(obj instanceof String)) 126 throw new IOException("\"" + getDF().getMimeType() + 127 "\" DataContentHandler requires String object, " + 128 "was given object of type " + obj.getClass().toString()); 129 130 String enc = null; 131 OutputStreamWriter osw = null; 132 133 try { 134 enc = getCharset(type); 135 osw = new OutputStreamWriter(os, enc); 136 } catch (IllegalArgumentException iex) { 137 /* 138 * An unknown charset of the form ISO-XXX-XXX will cause 139 * the JDK to throw an IllegalArgumentException. The 140 * JDK will attempt to create a classname using this string, 141 * but valid classnames must not contain the character '-', 142 * and this results in an IllegalArgumentException, rather than 143 * the expected UnsupportedEncodingException. Yikes. 144 */ 145 throw new UnsupportedEncodingException(enc); 146 } 147 148 String s = (String)obj; 149 osw.write(s, 0, s.length()); 150 osw.flush(); 151 } 152 153 private String getCharset(String type) { 154 try { 155 ContentType ct = new ContentType(type); 156 String charset = ct.getParameter("charset"); 157 if (charset == null) 158 // If the charset parameter is absent, use US-ASCII. 159 charset = "us-ascii"; 160 return MimeUtility.javaCharset(charset); 161 } catch (Exception ex) { 162 return null; 163 } 164 } 165 166 }