1 |
| package photospace.web.spring; |
2 |
| |
3 |
| import java.io.*; |
4 |
| import javax.servlet.http.*; |
5 |
| import org.apache.lucene.index.*; |
6 |
| import org.apache.lucene.queryParser.*; |
7 |
| import org.apache.lucene.search.*; |
8 |
| import org.springframework.context.support.*; |
9 |
| import org.springframework.web.bind.*; |
10 |
| import org.springframework.web.servlet.*; |
11 |
| import org.springframework.web.servlet.mvc.multiaction.*; |
12 |
| import org.springframework.web.servlet.view.*; |
13 |
| import photospace.meta.*; |
14 |
| import photospace.search.*; |
15 |
| import photospace.search.Searcher; |
16 |
| import photospace.space.*; |
17 |
| import photospace.web.*; |
18 |
| |
19 |
| public class DefaultController |
20 |
| extends MultiActionController |
21 |
| { |
22 |
| |
23 |
| private Searcher searcher; |
24 |
| private Locator locator; |
25 |
| private Translator translator; |
26 |
| |
27 |
| public static final String MODEL_KEY = "model"; |
28 |
| public static final String QUERY_KEY = "query"; |
29 |
| public static final String HISTORY_KEY = "history"; |
30 |
| |
31 |
| private static final int PAGE_SIZE = 40; |
32 |
| |
33 |
0
| public ModelAndView locate(HttpServletRequest request, HttpServletResponse response)
|
34 |
| throws Exception |
35 |
| { |
36 |
0
| String address = RequestUtils.getStringParameter(request, "address", null);
|
37 |
| |
38 |
0
| Position position = locator.locate(address);
|
39 |
| |
40 |
0
| return new ModelAndView("/locate", "position", position);
|
41 |
| } |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
0
| public ModelAndView history(HttpServletRequest request, HttpServletResponse response)
|
49 |
| throws Exception |
50 |
| { |
51 |
0
| History history = getHistory(request.getSession());
|
52 |
0
| if (history.getEntries().isEmpty()) return new ModelAndView(new RedirectView("/", true));
|
53 |
| |
54 |
0
| int index = RequestUtils.getIntParameter(request, "index", history.getEnd());
|
55 |
0
| return new ModelAndView(new RedirectView(history.goTo(index).getUrl()));
|
56 |
| } |
57 |
| |
58 |
0
| public ModelAndView search(HttpServletRequest request, HttpServletResponse response)
|
59 |
| throws Exception |
60 |
| { |
61 |
0
| String query = RequestUtils.getStringParameter(request, "query", "path:/");
|
62 |
0
| int order = RequestUtils.getIntParameter(request, "order", Searcher.ASCENDING);
|
63 |
0
| int start = RequestUtils.getIntParameter(request, "start", 0);
|
64 |
0
| int end = RequestUtils.getIntParameter(request, "end", start + (PAGE_SIZE - 1));
|
65 |
0
| SearchResult result;
|
66 |
0
| try
|
67 |
| { |
68 |
0
| result = searcher.search(query, order, start, end);
|
69 |
| } |
70 |
| catch (ParseException e) |
71 |
| { |
72 |
0
| return error(request, response, messages().getMessage("error.invalid.query",
|
73 |
| new Object[] {query})); |
74 |
| } |
75 |
| |
76 |
0
| Page page = new Page();
|
77 |
0
| page.setCollection(result);
|
78 |
| |
79 |
0
| request.setAttribute(QUERY_KEY, query);
|
80 |
0
| getHistory(request.getSession()).add(request, result);
|
81 |
| |
82 |
0
| return createView(page, RequestUtils.getStringParameter(request, "view", "media"), request, response);
|
83 |
| } |
84 |
| |
85 |
0
| public ModelAndView error(HttpServletRequest request, HttpServletResponse response, String message)
|
86 |
| throws Exception |
87 |
| { |
88 |
0
| request.getSession().setAttribute("error", message);
|
89 |
0
| return history(request, response);
|
90 |
| } |
91 |
| |
92 |
0
| public ModelAndView message(HttpServletRequest request, HttpServletResponse response, String message)
|
93 |
| throws Exception |
94 |
| { |
95 |
0
| request.getSession().setAttribute("message", message);
|
96 |
0
| return history(request, response);
|
97 |
| } |
98 |
| |
99 |
0
| public History getHistory(HttpSession session)
|
100 |
| { |
101 |
0
| if (session.getAttribute(HISTORY_KEY) == null) session.setAttribute(HISTORY_KEY, new History());
|
102 |
0
| return (History) session.getAttribute(HISTORY_KEY);
|
103 |
| } |
104 |
| |
105 |
0
| public ModelAndView view(HttpServletRequest request, HttpServletResponse response)
|
106 |
| throws Exception |
107 |
| { |
108 |
0
| String path = RequestUtils.getStringParameter(request, "path", "/");
|
109 |
0
| String query = RequestUtils.getStringParameter(request, "query", null);
|
110 |
0
| int order = RequestUtils.getIntParameter(request, "order", Searcher.ASCENDING);
|
111 |
0
| int start = RequestUtils.getIntParameter(request, "start", 0);
|
112 |
0
| int end = RequestUtils.getIntParameter(request, "end", start + (PAGE_SIZE - 1));
|
113 |
0
| int index = RequestUtils.getIntParameter(request, "index", -1);
|
114 |
| |
115 |
0
| Meta meta = searcher.browse(path, order, start, end);
|
116 |
| |
117 |
0
| if (meta == null)
|
118 |
| { |
119 |
0
| return error(request, response, messages().getMessage("error.notfound", new Object[] { path }));
|
120 |
| } |
121 |
| |
122 |
0
| Page page = new Page();
|
123 |
0
| page.setIndex(index);
|
124 |
| |
125 |
0
| if (meta instanceof CollectionMeta)
|
126 |
0
| page.setCollection((CollectionMeta) meta);
|
127 |
0
| else if (query != null && index != -1)
|
128 |
0
| page.setCollection(searcher.search(query, Searcher.PHOTO_CONSTRAINT, order, index - 1, index + 1));
|
129 |
0
| else if (index != -1)
|
130 |
0
| page.setCollection((CollectionMeta) searcher.browse(meta.getParentPath(), Searcher.PHOTO_CONSTRAINT, order, index - 1, index + 1));
|
131 |
| else |
132 |
0
| page.setCollection(new FolderMeta());
|
133 |
| |
134 |
| |
135 |
0
| if (!meta.equals(page.getCurrent()))
|
136 |
| { |
137 |
0
| page.setCollection(new FolderMeta());
|
138 |
0
| page.getCollection().addFile(meta);
|
139 |
0
| page.setIndex(0);
|
140 |
| } |
141 |
| else |
142 |
| { |
143 |
0
| page.getCurrent().setParent(meta.getParent());
|
144 |
| } |
145 |
| |
146 |
0
| getHistory(request.getSession()).add(request, meta);
|
147 |
| |
148 |
0
| return createView(page, RequestUtils.getStringParameter(request, "view", "media"), request, response);
|
149 |
| } |
150 |
| |
151 |
0
| public ModelAndView forbidden(HttpServletRequest request, HttpServletResponse response)
|
152 |
| throws Exception |
153 |
| { |
154 |
0
| return error(request, response, messages().getMessage("error.forbidden",
|
155 |
| (String) request.getAttribute("javax.servlet.error.message"))); |
156 |
| } |
157 |
| |
158 |
0
| private ModelAndView createView(Page page, String view, HttpServletRequest request, HttpServletResponse response)
|
159 |
| throws IOException |
160 |
| { |
161 |
0
| request.setAttribute(MODEL_KEY, page);
|
162 |
0
| if (page.getCurrent() instanceof CollectionMeta)
|
163 |
| { |
164 |
0
| if ("rdf".toLowerCase().equals(view))
|
165 |
| { |
166 |
0
| response.setContentType("text/xml");
|
167 |
0
| translator.toRdf((CollectionMeta) page.getCurrent(), 1).write(response.getOutputStream(), "RDF/XML-ABBREV");
|
168 |
0
| return null;
|
169 |
| } |
170 |
0
| else if ("rss".toLowerCase().equals(view))
|
171 |
| { |
172 |
0
| response.setContentType("text/xml");
|
173 |
0
| translator.toRss((CollectionMeta) page.getCurrent(), getViewUrl(request)).write(response.getOutputStream(), "RDF/XML-ABBREV");
|
174 |
0
| return null;
|
175 |
| } |
176 |
0
| return new ModelAndView("collection-" + view);
|
177 |
| } |
178 |
| else |
179 |
| { |
180 |
0
| return new ModelAndView("media-" + view);
|
181 |
| } |
182 |
| } |
183 |
| |
184 |
0
| private String getViewUrl(HttpServletRequest request)
|
185 |
| { |
186 |
0
| return request.getRequestURL().toString() + "?" + request.getQueryString().replaceFirst("=rss","=media");
|
187 |
| } |
188 |
| |
189 |
0
| public void setSearcher(Searcher searcher)
|
190 |
| { |
191 |
0
| this.searcher = searcher;
|
192 |
| } |
193 |
| |
194 |
0
| public void setLocator(Locator locator)
|
195 |
| { |
196 |
0
| this.locator = locator;
|
197 |
| } |
198 |
| |
199 |
0
| public void setTranslator(Translator translator)
|
200 |
| { |
201 |
0
| this.translator = translator;
|
202 |
| } |
203 |
| |
204 |
0
| private MessageSourceAccessor messages()
|
205 |
| { |
206 |
0
| return getMessageSourceAccessor();
|
207 |
| } |
208 |
| |
209 |
| } |