View Javadoc

1   /*
2    * $Id: StringDataSource.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.util;
12  
13  import java.io.ByteArrayInputStream;
14  import java.io.IOException;
15  import java.io.InputStream;
16  import java.io.OutputStream;
17  
18  import javax.activation.DataSource;
19  
20  public class StringDataSource implements DataSource
21  {
22      protected String content;
23      protected String contentType = "text/plain";
24      protected String name = "StringDataSource";
25  
26      public StringDataSource(String payload)
27      {
28          super();
29          content = payload;
30      }
31  
32      public StringDataSource(String payload, String name)
33      {
34          super();
35          content = payload;
36          this.name = name;
37      }
38  
39      public StringDataSource(String content, String name, String contentType)
40      {
41          this.content = content;
42          this.contentType = contentType;
43          this.name = name;
44      }
45  
46      public InputStream getInputStream() throws IOException
47      {
48          return new ByteArrayInputStream(content.getBytes());
49      }
50  
51      public OutputStream getOutputStream()
52      {
53          throw new UnsupportedOperationException("Read-only javax.activation.DataSource");
54      }
55  
56      public String getContentType()
57      {
58          return contentType;
59      }
60  
61      public String getName()
62      {
63          return name;
64      }
65  }
66