View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.webscripts.atom;
20  
21  import java.io.IOException;
22  import java.io.OutputStream;
23  
24  import org.apache.abdera.model.Base;
25  import org.apache.abdera.writer.Writer;
26  import org.springframework.extensions.webscripts.FormatWriter;
27  import org.springframework.extensions.webscripts.WebScriptException;
28  
29  
30  /**
31   * Convert mimetypes to Atom classes.
32   * 
33   * @author davidc
34   */
35  public class AtomWriter implements FormatWriter<Base>
36  {
37      // dependencies
38      protected AbderaService abderaService;
39      
40      private Class<? extends Base> clazz;
41      private String mimetype;
42      private String writerName = AbderaService.DEFAULT_WRITER;
43      
44  
45      /**
46       * Sets the Abdera Service
47       * 
48       * @param abderaService
49       */
50      public void setAbderaService(AbderaService abderaService)
51      {
52          this.abderaService = abderaService; 
53      }
54  
55      /**
56       * Sets the Class to convert to
57       * 
58       * @param clazz
59       */
60      public void setClass(Class<? extends Base> clazz)
61      {
62          this.clazz = clazz;
63      }
64      
65      /**
66       * Sets the mimetype to convert from
67       * 
68       * @param mimetype
69       */
70      public void setMimetype(String mimetype)
71      {
72          this.mimetype = mimetype;
73      }
74      
75      /**
76       * Sets the Atom Writer
77       * 
78       * @param writerName
79       */
80      public void setWriter(String writerName)
81      {
82          this.writerName = writerName;
83      }
84      
85      /* (non-Javadoc)
86       * @see org.springframework.extensions.webscripts.FormatWriter#getSourceClass()
87       */
88      public Class<? extends Base> getSourceClass()
89      {
90          return clazz;
91      }
92  
93      /* (non-Javadoc)
94       * @see org.springframework.extensions.webscripts.FormatWriter#getDestinationMimetype()
95       */
96      public String getDestinationMimetype()
97      {
98          return mimetype;
99      }
100 
101     /* (non-Javadoc)
102      * @see org.springframework.extensions.webscripts.FormatWriter#write(java.lang.Object, java.io.Writer)
103      */
104     public void write(Base object, java.io.Writer output)
105     {
106         Writer writer = abderaService.getWriter(writerName);
107         try
108         {
109             object.writeTo(writer, output);
110         }
111         catch (IOException e)
112         {
113             throw new WebScriptException("Failed to write atom object to mimetype '" + mimetype + "'", e);
114         }
115     }
116 
117     /* (non-Javadoc)
118      * @see org.springframework.extensions.webscripts.FormatWriter#write(java.lang.Object, java.io.OutputStream)
119      */
120     public void write(Base object, OutputStream output)
121     {
122         Writer writer = abderaService.getWriter(writerName);
123         try
124         {
125             object.writeTo(writer, output);
126         }
127         catch (IOException e)
128         {
129             throw new WebScriptException("Failed to write atom object to mimetype '" + mimetype + "'", e);
130         }
131     }
132 
133 }