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.Feed;
26  import org.springframework.extensions.surf.util.Content;
27  import org.springframework.extensions.surf.util.URLEncoder;
28  import org.springframework.extensions.webscripts.Format;
29  import org.springframework.extensions.webscripts.FormatReader;
30  import org.springframework.extensions.webscripts.WebScriptException;
31  import org.springframework.extensions.webscripts.WebScriptRequest;
32  import org.springframework.extensions.webscripts.WebScriptResponse;
33  
34  
35  /**
36   * Convert application/atom+xml;type=feed to class org.apache.abdera.model.Feed
37   * 
38   * @author davidc
39   */
40  public class AtomFeedReader implements FormatReader<Feed>
41  {
42      // dependencies
43      protected AbderaService abderaService;
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      /* (non-Javadoc)
56       * @see org.springframework.extensions.webscripts.FormatReader#getDestinationClass()
57       */
58      public Class<Feed> getDestinationClass()
59      {
60          return Feed.class;
61      }
62  
63      /* (non-Javadoc)
64       * @see org.springframework.extensions.webscripts.FormatReader#getSourceMimetype()
65       */
66      public String getSourceMimetype()
67      {
68          return Format.ATOMFEED.mimetype();
69      }
70  
71      /* (non-Javadoc)
72       * @see org.springframework.extensions.webscripts.FormatReader#read(org.springframework.extensions.webscripts.WebScriptRequest)
73       */
74      public Feed read(WebScriptRequest req)
75      {
76          Content content = req.getContent();
77          if (content == null)
78          {
79              throw new WebScriptException("Failed to convert request to Atom Feed");
80          }
81          try
82          {
83              return abderaService.parseFeed(content.getReader(), req.getServerPath() + URLEncoder.encodeUri(req.getServicePath()));
84          }
85          catch (IOException e)
86          {
87              throw new WebScriptException("Failed to convert request to Atom Feed", e);
88          }
89      }
90      
91      /* (non-Javadoc)
92       * @see org.springframework.extensions.webscripts.FormatReader#createScriptParameters(org.springframework.extensions.webscripts.WebScriptRequest, org.springframework.extensions.webscripts.WebScriptResponse)
93       */
94      public Map<String, Object> createScriptParameters(WebScriptRequest req, WebScriptResponse res)
95      {
96          Map<String, Object> params = new HashMap<String, Object>();
97          params.put("feed", read(req));
98          return params;
99      }
100 }