1 | |
package org.paneris.bibliomania; |
2 | |
|
3 | |
import javax.servlet.ServletConfig; |
4 | |
|
5 | |
import org.melati.Melati; |
6 | |
import org.melati.MelatiConfig; |
7 | |
import org.melati.login.AccessHandler; |
8 | |
import org.melati.poem.PoemThread; |
9 | |
import org.melati.PoemContext; |
10 | |
import org.melati.servlet.PathInfoException; |
11 | |
import org.melati.template.webmacro.WebmacroMelatiServlet; |
12 | |
import org.melati.poem.util.StringUtils; |
13 | |
import org.webmacro.servlet.WebContext; |
14 | |
|
15 | 0 | abstract public class BibliomaniaServlet extends WebmacroMelatiServlet { |
16 | |
|
17 | |
public static final String templatePrefix = "org/paneris/bibliomania/html/"; |
18 | |
|
19 | |
public String getSysAdminName () { |
20 | 0 | return "TimP"; |
21 | |
} |
22 | |
public String getSysAdminEmail () { |
23 | 0 | return "timp@paneris.org"; |
24 | |
} |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
public static String bibliomaniaTemplate(String name) { |
33 | 23 | return templatePrefix + name; |
34 | |
} |
35 | |
|
36 | |
protected MelatiConfig melatiConfig() { |
37 | 0 | MelatiConfig config = super.melatiConfig(); |
38 | 0 | config.setAccessHandler(new BibliomaniaAccessHandler()); |
39 | 0 | return config; |
40 | |
} |
41 | |
|
42 | |
protected String bibliomaniaHandle(Melati melati, WebContext context) |
43 | |
throws Exception { |
44 | 0 | String method = melati.getMethod(); |
45 | 0 | if (method == null) { |
46 | 0 | method="undefinedMethod"; |
47 | |
} |
48 | 0 | return bibliomaniaTemplate(method); |
49 | |
} |
50 | |
|
51 | 0 | public class BibContext extends PoemContext { |
52 | |
public Integer page; |
53 | |
public String pathInfo; |
54 | |
} |
55 | |
|
56 | |
protected void decipherExtraPathInfo(String[] parts, BibContext it) { |
57 | 0 | } |
58 | |
|
59 | |
protected void fillContext(Melati melati, BibContext it) |
60 | |
throws PathInfoException { |
61 | 0 | it.setLogicalDatabase("bibliomania"); |
62 | |
|
63 | 0 | String pathInfo = melati.getRequest().getPathInfo(); |
64 | 0 | it.pathInfo = pathInfo; |
65 | 0 | if (pathInfo == null) return; |
66 | 0 | String[] parts = StringUtils.split(pathInfo, '/'); |
67 | 0 | if (parts.length < 2) return; |
68 | |
|
69 | |
try { |
70 | |
int p; |
71 | 0 | for (p = 2; p < parts.length && p <= 7; ++p) { |
72 | 0 | String part = parts[p]; |
73 | 0 | if (!part.equals("-") && |
74 | |
(part.length() == 0 || !Character.isDigit(part.charAt(0)) || |
75 | |
part.endsWith(".html"))) |
76 | 0 | break; |
77 | |
} |
78 | |
|
79 | 0 | while (parts[p-1].equals("-")) --p; |
80 | |
|
81 | 0 | it.setTroid(new Integer(parts[p-1])); |
82 | |
|
83 | 0 | switch (p) { |
84 | |
case 2: |
85 | 0 | it.setTable("sectiongroup"); |
86 | 0 | break; |
87 | |
case 3: |
88 | 0 | it.setTable("section"); |
89 | 0 | break; |
90 | |
case 4: |
91 | 0 | it.setTable("author"); |
92 | 0 | break; |
93 | |
case 5: |
94 | 0 | it.setTable("book"); |
95 | 0 | break; |
96 | |
case 7: |
97 | 0 | it.page = new Integer(parts[p-1]); |
98 | 0 | it.setTroid(new Integer(parts[p-2])); |
99 | 0 | it.setTable("chapter"); |
100 | 0 | break; |
101 | |
case 6: |
102 | 0 | it.setTable("chapter"); |
103 | 0 | break; |
104 | |
default: |
105 | 0 | throw new ArrayIndexOutOfBoundsException(); |
106 | |
} |
107 | |
} |
108 | 0 | catch (Exception e) { |
109 | 0 | throw new PathInfoException(pathInfo, e); |
110 | 0 | } |
111 | 0 | } |
112 | |
|
113 | |
protected BibContext melatiContext() { |
114 | 0 | return new BibContext(); |
115 | |
} |
116 | |
|
117 | |
|
118 | |
protected PoemContext poemContext(Melati melati) |
119 | |
throws PathInfoException { |
120 | 0 | BibContext it = melatiContext(); |
121 | |
|
122 | 0 | fillContext(melati, it); |
123 | 0 | return it; |
124 | |
} |
125 | |
|
126 | |
protected final String handle (Melati melati, WebContext context) |
127 | |
throws Exception { |
128 | |
|
129 | 0 | BibliomaniaDatabase db = (BibliomaniaDatabase)melati.getDatabase(); |
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | 0 | db.setupContext(melati, context, (Unit)melati.getObject()); |
135 | |
|
136 | 0 | return bibliomaniaHandle(melati, context); |
137 | |
} |
138 | |
|
139 | 0 | private AccessHandler accessHandler = new BibliomaniaAccessHandler(); |
140 | |
|
141 | |
protected AccessHandler accessHandler() { |
142 | 0 | return accessHandler; |
143 | |
} |
144 | |
|
145 | |
public void assertRegisteredUser(BibliomaniaDatabase db) { |
146 | 0 | if (!PoemThread.accessToken().givesCapability( |
147 | |
db.getRegisteredUserCapability())) |
148 | 0 | throw new NotLoggedInException(); |
149 | 0 | } |
150 | |
|
151 | 1 | private static final Boolean[] startedCommandServer = new Boolean[1]; |
152 | |
|
153 | |
public void init(ServletConfig config) throws javax.servlet.ServletException { |
154 | 0 | super.init(config); |
155 | 0 | if (startedCommandServer[0] == null) { |
156 | 0 | synchronized (startedCommandServer) { |
157 | 0 | if (startedCommandServer[0] == null) { |
158 | |
try { |
159 | 0 | new CommandServer(0xB1B, 1).start(); |
160 | 0 | System.err.println("Started command server"); |
161 | |
} |
162 | 0 | catch (Exception e) { |
163 | 0 | System.err.println("Unable to start command server: " + e); |
164 | 0 | } |
165 | |
|
166 | 0 | startedCommandServer[0] = Boolean.TRUE; |
167 | |
} |
168 | 0 | } |
169 | |
} |
170 | 0 | } |
171 | |
} |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|