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 | 0 | 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 | 0 | it.setLogicalDatabase("bibliomania"); |
21 | 0 | } |
22 | |
|
23 | |
public static String newURL(String contentPath, Bib bib) { |
24 | 0 | Unit object = null; |
25 | 0 | BibliomaniaDatabase db = (BibliomaniaDatabase)PoemThread.database(); |
26 | |
|
27 | 0 | if (contentPath != null) { |
28 | 0 | if (contentPath.endsWith("/")) |
29 | 0 | contentPath = contentPath.substring(0, contentPath.length() - 1); |
30 | 0 | String[] parts = StringUtils.split(contentPath, '/'); |
31 | |
|
32 | 0 | for (int i = parts.length; object == null && i >= 2; --i) |
33 | 0 | switch (i) { |
34 | |
case 0: |
35 | |
case 1: |
36 | 0 | break; |
37 | |
case 2: |
38 | 0 | object = db.getSectionTable().unitWithPath( |
39 | |
"/" + parts[1]); |
40 | 0 | break; |
41 | |
case 3: |
42 | 0 | object = db.getAuthorTable().unitWithPath( |
43 | |
"/" + parts[1] + "/" + parts[2]); |
44 | 0 | break; |
45 | |
case 4: |
46 | 0 | object = db.getBookTable().unitWithPath( |
47 | |
"/" + parts[1] + "/" + parts[2] + "/" + parts[3]); |
48 | 0 | break; |
49 | |
default: |
50 | 0 | object = db.getChapterTable().unitWithPath( |
51 | |
"/" + parts[1] + "/" + parts[2] + "/" + parts[3] + "/" + |
52 | |
parts[4]); |
53 | |
} |
54 | |
} |
55 | |
|
56 | 0 | return object == null ? db.getHomepageURL() : bib.framesetURL(object); |
57 | |
} |
58 | |
|
59 | |
protected String bibliomaniaHandle(Melati melati, WebContext context) |
60 | |
throws WebMacroException { |
61 | 0 | String contentPath = context.getRequest().getPathInfo(); |
62 | |
try { |
63 | 0 | context.getResponse().sendRedirect( |
64 | |
newURL(contentPath, |
65 | |
((BibliomaniaDatabase)melati.getDatabase()).getBib())); |
66 | 0 | } catch (IOException e) { |
67 | 0 | throw new HandlerException("Problem with Redirect:" + e.toString()); |
68 | 0 | } |
69 | 0 | return null; |
70 | |
} |
71 | |
} |