1 package org.paneris.bibliomania;
2
3 import java.io.IOException;
4
5 import org.melati.Melati;
6 import org.melati.poem.PoemThread;
7 import org.melati.poem.util.StringUtils;
8 import org.webmacro.WebMacroException;
9 import org.webmacro.servlet.HandlerException;
10 import org.webmacro.servlet.WebContext;
11
12 public class OldUrlRedirect extends BibliomaniaServlet {
13
14
15
16
17 private static final long serialVersionUID = 1L;
18
19 protected void fillContext(Melati melati, BibContext it) {
20 it.setLogicalDatabase("bibliomania");
21 }
22
23 public static String newURL(String contentPath, Bib bib) {
24 Unit object = null;
25 BibliomaniaDatabase db = (BibliomaniaDatabase)PoemThread.database();
26
27 if (contentPath != null) {
28 if (contentPath.endsWith("/"))
29 contentPath = contentPath.substring(0, contentPath.length() - 1);
30 String[] parts = StringUtils.split(contentPath, '/');
31
32 for (int i = parts.length; object == null && i >= 2; --i)
33 switch (i) {
34 case 0:
35 case 1:
36 break;
37 case 2:
38 object = db.getSectionTable().unitWithPath(
39 "/" + parts[1]);
40 break;
41 case 3:
42 object = db.getAuthorTable().unitWithPath(
43 "/" + parts[1] + "/" + parts[2]);
44 break;
45 case 4:
46 object = db.getBookTable().unitWithPath(
47 "/" + parts[1] + "/" + parts[2] + "/" + parts[3]);
48 break;
49 default:
50 object = db.getChapterTable().unitWithPath(
51 "/" + parts[1] + "/" + parts[2] + "/" + parts[3] + "/" +
52 parts[4]);
53 }
54 }
55
56 return object == null ? db.getHomepageURL() : bib.framesetURL(object);
57 }
58
59 protected String bibliomaniaHandle(Melati melati, WebContext context)
60 throws WebMacroException {
61 String contentPath = context.getRequest().getPathInfo();
62 try {
63 context.getResponse().sendRedirect(
64 newURL(contentPath,
65 ((BibliomaniaDatabase)melati.getDatabase()).getBib()));
66 } catch (IOException e) {
67 throw new HandlerException("Problem with Redirect:" + e.toString());
68 }
69 return null;
70 }
71 }