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.util.HashMap;
23  import java.util.Map;
24  
25  import org.apache.abdera.model.Element;
26  import org.apache.abdera.model.Entry;
27  import org.springframework.extensions.surf.util.Content;
28  import org.springframework.extensions.surf.util.URLEncoder;
29  import org.springframework.extensions.webscripts.Format;
30  import org.springframework.extensions.webscripts.FormatReader;
31  import org.springframework.extensions.webscripts.WebScriptException;
32  import org.springframework.extensions.webscripts.WebScriptRequest;
33  import org.springframework.extensions.webscripts.WebScriptResponse;
34  
35  
36  /**
37   * Convert application/atom+xml to either org.apache.abdera.model.Entry or
38   * org.apache.adbera.model.Feed
39   * 
40   * @author davidc
41   */
42  public class AtomReader implements FormatReader<Element>
43  {
44      // dependencies
45      protected AbderaService abderaService;
46      
47      /**
48       * Sets the Abdera Service
49       * 
50       * @param abderaService
51       */
52      public void setAbderaService(AbderaService abderaService)
53      {
54          this.abderaService = abderaService; 
55      }
56      
57      /* (non-Javadoc)
58       * @see org.springframework.extensions.webscripts.FormatReader#getDestinationClass()
59       */
60      public Class<Element> getDestinationClass()
61      {
62          return Element.class;
63      }
64  
65      /* (non-Javadoc)
66       * @see org.springframework.extensions.webscripts.FormatReader#getSourceMimetype()
67       */
68      public String getSourceMimetype()
69      {
70          return Format.ATOM.mimetype();
71      }
72  
73      /* (non-Javadoc)
74       * @see org.springframework.extensions.webscripts.FormatReader#read(org.springframework.extensions.webscripts.WebScriptRequest)
75       */
76      public Element read(WebScriptRequest req)
77      {
78          Content content = req.getContent();
79          if (content == null)
80          {
81              throw new WebScriptException("Failed to convert request to Atom");
82          }
83          try
84          {
85              return abderaService.parse(content.getReader(), req.getServerPath() + URLEncoder.encodeUri(req.getServicePath()));
86          }
87          catch (IOException e)
88          {
89              throw new WebScriptException("Failed to convert request to Atom", e);
90          }
91      }
92      
93      /* (non-Javadoc)
94       * @see org.springframework.extensions.webscripts.FormatReader#createScriptParameters(org.springframework.extensions.webscripts.WebScriptRequest, org.springframework.extensions.webscripts.WebScriptResponse)
95       */
96      public Map<String, Object> createScriptParameters(WebScriptRequest req, WebScriptResponse res)
97      {
98          Map<String, Object> params = new HashMap<String, Object>();
99          Element element = (Element)read(req);
100         if (element instanceof Entry)
101         {
102             params.put("entry", element);
103             params.put("slug", req.getHeader("Slug"));
104         }
105         else
106         {
107             params.put("feed", element);
108         }
109         return params;
110     }
111 }