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