1
2
3
4
5 package org.fedoracommons.funapi;
6
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.OutputStream;
10
11 import java.util.ArrayList;
12 import java.util.List;
13
14 import javax.servlet.ServletException;
15 import javax.servlet.http.HttpServlet;
16 import javax.servlet.http.HttpServletRequest;
17 import javax.servlet.http.HttpServletResponse;
18
19
20
21
22
23
24
25
26
27
28 public class UnapiServlet
29 extends HttpServlet {
30
31 private static final long serialVersionUID = 1L;
32
33
34 private static final String CONTENT_TYPE_XML = "application/xml; charset=UTF-8";
35
36 private List<UnapiFormat> defaultFormats = new ArrayList<UnapiFormat>();
37
38 private FormatsSerializer serializer;
39
40 private ObjectResolver resolver;
41
42 @Override
43 public void init() throws ServletException {
44 String className = getServletConfig().getInitParameter("resolver");
45
46 try {
47 serializer = new FormatsSerializer();
48 Class<?> klass = Class.forName(className);
49 resolver = (ObjectResolver)klass.newInstance();
50 } catch (ClassNotFoundException e) {
51 throw new ServletException(e.getMessage(), e);
52 } catch (InstantiationException e) {
53 throw new ServletException(e.getMessage(), e);
54 } catch (IllegalAccessException e) {
55 throw new ServletException(e.getMessage(), e);
56 } catch (UnapiException e) {
57 throw new ServletException(e.getMessage(), e);
58 }
59 }
60
61 @Override
62 public void doGet(HttpServletRequest request, HttpServletResponse response)
63 throws ServletException, IOException {
64 String id = request.getParameter("id");
65 String format = request.getParameter("format");
66
67 if (id == null && format == null) {
68 listDefaultFormats(response);
69 } else if (format == null) {
70 listObjectFormats(response, id);
71 } else {
72 getObject(response, id, format);
73 }
74 }
75
76
77
78
79
80
81
82
83 private void listDefaultFormats(HttpServletResponse response) throws IOException {
84 UnapiFormats formats = null;
85 try {
86 if (defaultFormats.size() > 0) {
87 formats = new UnapiFormats(null, defaultFormats);
88 } else {
89 formats = resolver.getFormats();
90 }
91 response.setContentType(CONTENT_TYPE_XML);
92 serializer.serialize(formats, response.getOutputStream());
93 } catch (UnapiException e) {
94 response.sendError(e.getErrorCode(), e.getMessage());
95 }
96 }
97
98
99
100
101
102
103
104
105
106
107
108
109 private void listObjectFormats(HttpServletResponse response, String id) throws IOException {
110 try {
111 UnapiFormats formats = resolver.getFormats(id);
112 response.setContentType(CONTENT_TYPE_XML);
113 response.setStatus(HttpServletResponse.SC_MULTIPLE_CHOICES);
114
115
116
117
118
119 serializer.serialize(formats, response.getOutputStream());
120 } catch(UnapiException e) {
121 response.sendError(e.getErrorCode(), e.getMessage());
122 }
123 }
124
125
126
127
128
129
130
131
132
133
134
135
136 private void getObject(HttpServletResponse response, String id, String format) throws IOException {
137 try {
138 UnapiObject obj = resolver.getObject(id, format);
139 if (obj.getRedirectUrl() != null) {
140 response.sendRedirect(obj.getRedirectUrl());
141 } else {
142 InputStream in = obj.getInputStream();
143 if (in == null) {
144 throw new UnapiException("Error getting " + id + " as " +
145 format + ". Neither redirectUrl " +
146 "nor InputStream was provided.");
147 }
148 response.setContentType(obj.getContentType());
149 copy(in, response.getOutputStream());
150 in.close();
151 }
152 } catch(UnapiException e) {
153 response.sendError(500, e.getMessage());
154 }
155 }
156
157 private static final int BUFF_SIZE = 100000;
158
159
160
161
162 private static final byte[] buffer = new byte[BUFF_SIZE];
163
164
165
166
167
168
169
170
171
172
173
174
175
176 public static boolean copy(InputStream source, OutputStream destination) {
177 try {
178 while (true) {
179 synchronized (buffer) {
180 int amountRead = source.read(buffer);
181 if (amountRead == -1) {
182 break;
183 }
184 destination.write(buffer, 0, amountRead);
185 }
186 }
187 destination.flush();
188 destination.close();
189 return true;
190 } catch (IOException e) {
191 return false;
192 }
193 }
194 }