View Javadoc

1   /*
2    * Copyright (c) 2007, James Leigh All rights reserved.
3    * 
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions are met:
6    * 
7    * - Redistributions of source code must retain the above copyright notice, this
8    *   list of conditions and the following disclaimer.
9    * - Redistributions in binary form must reproduce the above copyright notice,
10   *   this list of conditions and the following disclaimer in the documentation
11   *   and/or other materials provided with the distribution. 
12   * - Neither the name of the openrdf.org nor the names of its contributors may
13   *   be used to endorse or promote products derived from this software without
14   *   specific prior written permission.
15   * 
16   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26   * POSSIBILITY OF SUCH DAMAGE.
27   * 
28   */
29  package org.openrdf.elmo.example.rss;
30  
31  import java.io.IOException;
32  import java.io.PrintWriter;
33  import java.util.HashMap;
34  import java.util.Map;
35  
36  import javax.servlet.GenericServlet;
37  import javax.servlet.ServletException;
38  import javax.servlet.ServletRequest;
39  import javax.servlet.ServletResponse;
40  import javax.xml.namespace.QName;
41  
42  import org.openrdf.concepts.rdf.Seq;
43  import org.openrdf.concepts.rss.Channel;
44  import org.openrdf.concepts.rss.Item;
45  import org.openrdf.elmo.ElmoManager;
46  import org.openrdf.elmo.ElmoModule;
47  import org.openrdf.elmo.sesame.SesameManager;
48  import org.openrdf.elmo.sesame.SesameManagerFactory;
49  import org.openrdf.elmo.sesame.roles.SesameEntity;
50  import org.openrdf.rio.rss.RssWriter;
51  
52  /**
53   * Creates an RSS feed from HTTP parameters or arguments.
54   * 
55   * @author James Leigh
56   * 
57   */
58  public class App extends GenericServlet {
59  	private static final long serialVersionUID = 1L;
60  
61  	private SesameManagerFactory factory;
62  
63  	@Override
64  	public void init() throws ServletException {
65  		factory = new SesameManagerFactory(new ElmoModule());
66  	}
67  
68  	@Override
69  	public void destroy() {
70  		factory.close();
71  	}
72  
73  	@Override
74  	public void service(ServletRequest request, ServletResponse response)
75  			throws IOException {
76  		response.setContentType("application/rss+xml");
77  		writeRss(response.getWriter(), request.getParameterMap());
78  	}
79  
80  	public static void main(String[] args) throws IOException, ServletException {
81  		Map<String, String[]> map = new HashMap<String, String[]>();
82  		for (String arg : args) {
83  			String[] e = arg.split("=", 2);
84  			String[] v = map.get(e[0]);
85  			if (v == null) {
86  				map.put(e[0], new String[] { e[1] });
87  			} else {
88  				String[] p = new String[v.length + 1];
89  				for (int i = 0; i < v.length; i++) {
90  					p[i] = v[i];
91  				}
92  				p[v.length] = e[1];
93  				map.put(e[0], p);
94  			}
95  		}
96  		App app = new App();
97  		app.init();
98  		app.writeRss(new PrintWriter(System.out), map);
99  		app.destroy();
100 	}
101 
102 	private void writeRss(PrintWriter writer, Map parameterMap)
103 			throws IOException {
104 		SesameManager manager = factory.createElmoManager();
105 		Channel channel = createChannel(manager, parameterMap);
106 
107 		RssWriter rss = new RssWriter(writer);
108 		try {
109 			rss.setConnection(manager.getConnection());
110 			rss.startRDF();
111 			rss.printChannel(((SesameEntity) channel).getSesameResource());
112 			rss.endRDF();
113 		} catch (Exception e) {
114 			IOException io = new IOException(e.getMessage());
115 			io.initCause(e);
116 			throw io;
117 		} finally {
118 			rss.close();
119 			manager.close();
120 		}
121 	}
122 
123 	private Channel createChannel(ElmoManager manager, Map parameterMap) {
124 		String[] titles = (String[]) parameterMap.get("title");
125 		String[] links = (String[]) parameterMap.get("link");
126 		String[] descriptions = (String[]) parameterMap.get("description");
127 		String[] subjects = (String[]) parameterMap.get("subject");
128 
129 		QName qname = new QName(links[0]);
130 		Channel channel = manager.designate(Channel.class, qname);
131 		channel.setRssTitle(titles[0]);
132 		channel.getRssLinks().add(links[0]);
133 		channel.setRssDescription(descriptions[0]);
134 		channel.getDcSubjects().add(subjects[0]);
135 
136 		if (channel.getRssItems() != null) {
137 			Seq<Item> list = channel.getRssItems();
138 			for (Item item : list) {
139 				manager.remove(item);
140 			}
141 			list.clear();
142 			manager.remove(list);
143 		}
144 		Seq<Item> items = manager.designate(Seq.class);
145 		channel.setRssItems(items);
146 
147 		for (int i = 1; i < titles.length; i++) {
148 			Item item = manager.designate(Item.class, new QName(links[i]));
149 			item.setRssTitle(titles[i]);
150 			item.getRssLinks().add(links[i]);
151 			item.setRssDescription(descriptions[i]);
152 			items.add(item);
153 		}
154 
155 		return channel;
156 	}
157 
158 }