View Javadoc

1   /*
2    * $Source: /usr/cvsroot/MelatiSite/src/main/java/org/paneris/melati/site/SiteServlet.java,v $
3    * $Revision: 1.24 $
4    *
5    *
6    * Contact details for copyright holder:
7    * 
8    *  Tim Pizey (http://paneris.org/~timp)
9    */
10  
11  package org.paneris.melati.site;
12  
13  import java.io.File;
14  import java.io.IOException;
15  
16  import javax.servlet.ServletException;
17  
18  import org.melati.Melati;
19  import org.melati.PoemContext;
20  import org.melati.poem.Column;
21  import org.melati.poem.Persistent;
22  import org.melati.poem.Table;
23  import org.melati.servlet.PathInfoException;
24  import org.melati.servlet.TemplateServlet;
25  import org.melati.template.ServletTemplateContext;
26  import org.melati.util.StringUtils;
27  
28  /**
29   * @author timp
30   * 
31   */
32  public abstract class SiteServlet extends TemplateServlet {
33  
34    /** Turn on placement of debugging info into context */
35    public static final boolean DEBUG = true;
36  
37    private static final String STATIC_ROOT = "/dist/MelatiSite/src/main/webapp";
38  
39    private static final String DB_NAME = "site";
40  
41    /**
42     * {@inheritDoc}
43     * 
44     * @see org.melati.servlet.PoemServlet#doConfiguredRequest(org.melati.Melati)
45     */
46    protected void doConfiguredRequest(final Melati melati)
47        throws ServletException, IOException {
48      String pathInfo = getPathInfo(melati);
49      System.err.println("pathinfo:" + pathInfo);
50  
51      // check if pathinfo exists in filesystem
52      // if so then redirect to it, unless we came from there
53      while (pathInfo != "" && !fileAt(pathInfo)) {
54        String s = pathInfo.substring(1);
55        int i = s.indexOf('/');
56        if (i == -1)
57          pathInfo = "";
58        else
59          pathInfo = s.substring(i);
60      }
61  
62      if (pathInfo != "") {
63        System.err.println("pathinfo:" + pathInfo);
64        System.err.println("Ref:" + melati.getRequest().getHeader("Referer"));
65        StringBuffer url = new StringBuffer();
66        String scheme = melati.getRequest().getScheme();
67        url.append(scheme);
68        url.append("://");
69        url.append(melati.getRequest().getServerName());
70        url.append(pathInfo);
71        // IE leaves referer empty in redirects !!
72        //String referer = melati.getRequest().getHeader("Referer");
73        //if (referer != null  && referer.indexOf(pathInfo) == -1) {
74          melati.getResponse().sendRedirect(url.toString());
75          return;
76        //}
77      }
78      super.doConfiguredRequest(melati);
79      melati.setResponseContentType("text/html");
80    }
81  
82    protected boolean fileAt(String filename) {
83      if (filename.equals(""))
84        return false;
85      if (filename.equals("/"))
86        return false;
87      String fsName = getSTATIC_ROOT() + filename;
88      File it = new File(fsName);
89      System.err.println("FS:" + fsName + " " + it.exists());
90      return it.exists();
91    }
92  
93    /**
94     * Concrete method for {@link TemplateServlet}.
95     * 
96     * @param melati
97     * @param context
98     * @return Template name
99     */
100   protected String doTemplateRequest(Melati melati,
101       ServletTemplateContext context) throws Exception {
102     return melati.templateName(reallyDoTemplateRequest(melati, context));
103   }
104 
105   /**
106    * Override this method to build up output in individual servlets.
107    * 
108    * @return Template name without path or extension
109    */
110   protected abstract String reallyDoTemplateRequest(Melati melati,
111       ServletTemplateContext templateContext) throws Exception;
112 
113   protected String getSetting(Melati melati, String settingName) {
114     String returnString = melati.getDatabase().getSettingTable().get(
115         settingName);
116     if (returnString == null)
117       throw new RuntimeException("Setting " + settingName
118           + " not found in setting table");
119     return returnString;
120   }
121 
122   /**
123    * Hardcode the db, rather than using Poem's URL specified LDB.
124    * 
125    * @see org.melati.servlet.ConfigServlet#melatiContext(org.melati.Melati)
126    */
127   protected PoemContext poemContext(Melati melati) throws PathInfoException {
128 
129     PoemContext it = new PoemContext();
130     it.setLogicalDatabase(getDB_NAME());
131 
132     String initParameterPathInfo = getInitParameter("pathinfo");
133     String[] parts;
134     if (initParameterPathInfo != null) {
135       parts = StringUtils.split(initParameterPathInfo, '/');
136       System.err.println("Got init param:" + initParameterPathInfo);
137     } else {
138       System.err.println("Did not find init param");
139       parts = melati.getPathInfoParts();
140     }
141     if (parts.length > 0) {
142       // Display/table.html - might be needed
143       // Display/ATemplate.html - template that is DB specific
144       // or contains no template specific info at all
145       if (parts.length == 1)
146         it.setMethod(parts[0]);
147 
148       // Display/page/1.html
149       // Display/page/TableSpecificSummaryTemplate.html
150       // Display/page/Primary.Search.Criterion.content.html
151       if (parts.length == 2) {
152         String r = parts[1];
153         if (r.lastIndexOf(".htm") != -1) {
154           r = r.substring(0, r.lastIndexOf(".htm"));
155           // it.method = "html";
156         }
157         it.setTable(parts[0]);
158         try {
159           it.setTroid(new Integer(r));
160         } catch (NumberFormatException e) {
161           it.setMethod(parts[1]);
162         }
163       }
164       // Display/page/1/SpecialTemplate.html
165       // Display/page/Primary.Search.Criterion.content/SpecialTemplate.html
166       // Display/page/Primary_Search_Criterion_content/SpecialTemplate.html
167       if (parts.length == 3) {
168         it.setTable(parts[0]);
169         try {
170           it.setTroid(new Integer(parts[1]));
171         } catch (NumberFormatException e) {
172           String r = parts[1];
173           if (r.lastIndexOf(".htm") != -1) {
174             r = r.substring(0, r.lastIndexOf(".htm"));
175           }
176           String value = StringUtils.tr(r, '.', ' ');
177           value = StringUtils.tr(r, '_', ' ');
178           Table t = melati.getTable();
179           if (t != null) {
180             Column c = t.primaryCriterionColumn();
181             if (c == null)
182               throw new NullPointerException("primaryCriterionColumn null");
183             Persistent o = c.firstWhereEq(value);
184             if (o != null)
185               it.setTroid(o.troid());
186           }
187         }
188         it.setMethod(parts[2]);
189       }
190 /*
191       if (parts.length > 3) 
192      {
193         String pathInfo = melati.getRequest().getPathInfo();
194         pathInfo = pathInfo.substring(1);
195         for (int i = 0; i< 2; i++) {
196           pathInfo = pathInfo.substring(pathInfo.indexOf("/") + 1);
197         }          
198         it.method = pathInfo;
199      }
200 */
201     }
202     return it;
203   }
204 
205   
206   
207   /**
208    * A little something to generate alternating colours.
209    * 
210    * @author timp
211    * 
212    */
213   public static class Util {
214 
215     /**
216      * Return a contrasting colour given a colour.
217      * 
218      * @param rgb
219      *          the colour to contrast with
220      * @return the colour string
221      */
222     public String contrastingColour(String rgb) {
223       if (rgb.length() != 6)
224         throw new IllegalArgumentException();
225       String red = rgb.substring(0, 2);
226       String green = rgb.substring(2, 4);
227       String blue = rgb.substring(4);
228 
229       int redI = Integer.parseInt(red, 16);
230       int greenI = Integer.parseInt(green, 16);
231       int blueI = Integer.parseInt(blue, 16);
232       int luminence = redI + greenI + blueI;
233       int redNew;
234       int greenNew;
235       int blueNew;
236       if (luminence > ((3 * 255) / 2)) {
237         // r>b>g
238         // r>g>b
239         // g>r>b
240         // b>r>g
241         // b>g>r
242         // g>b>r
243         if (redI > blueI) {
244           if (blueI > greenI) { // r>b>g
245             redNew = (greenI / 2);
246             greenNew = (redI / 2);
247             blueNew = (blueI / 2);
248           } else {
249             if (redI > greenI) { // r>g>b
250               redNew = (blueI / 2);
251               greenNew = (greenI / 2);
252               blueNew = (redI / 2);
253             } else { // g>r>b
254               redNew = (redI / 2);
255               greenNew = (blueI / 2);
256               blueNew = (greenI / 2);
257             }
258           }
259         } else {
260           if (redI > greenI) { // b>r>g
261             redNew = (redI / 2);
262             greenNew = (blueI / 2);
263             blueNew = (greenI / 2);
264           } else {
265             if (blueI > greenI) { // b>g>r
266               redNew = (blueI / 2);
267               greenNew = (greenI / 2);
268               blueNew = (redI / 2);
269             } else { // g>b>r
270               redNew = (greenI / 2);
271               greenNew = (redI / 2);
272               blueNew = (blueI / 2);
273             }
274           }
275         }
276       } else {
277         if (redI > blueI) {
278           if (blueI > greenI) { // r>b>g
279             redNew = 255 - (greenI / 2);
280             greenNew = 255 - (redI / 2);
281             blueNew = 255 - (blueI / 2);
282           } else {
283             if (redI > greenI) { // r>g>b
284               redNew = 255 - (blueI / 2);
285               greenNew = 255 - (greenI / 2);
286               blueNew = 255 - (redI / 2);
287             } else { // g>r>b
288               redNew = 255 - (redI / 2);
289               greenNew = 255 - (blueI / 2);
290               blueNew = 255 - (greenI / 2);
291             }
292           }
293         } else {
294           if (redI > greenI) { // b>r>g
295             redNew = 255 - (redI / 2);
296             greenNew = 255 - (blueI / 2);
297             blueNew = 255 - (greenI / 2);
298           } else {
299             if (blueI > greenI) { // b>g>r
300               redNew = 255 - (blueI / 2);
301               greenNew = 255 - (greenI / 2);
302               blueNew = 255 - (redI / 2);
303             } else { // g>b>r
304               redNew = 255 - (greenI / 2);
305               greenNew = 255 - (redI / 2);
306               blueNew = 255 - (blueI / 2);
307             }
308           }
309         }
310       }
311       String redNewS = (redNew < 16) ? "0" + Integer.toHexString(redNew)
312           : Integer.toHexString(redNew);
313       String greenNewS = (greenNew < 16) ? "0" + Integer.toHexString(greenNew)
314           : Integer.toHexString(greenNew);
315       String blueNewS = (blueNew < 16) ? "0" + Integer.toHexString(blueNew)
316           : Integer.toHexString(blueNew);
317 
318       return redNewS + greenNewS + blueNewS;
319     }
320   }
321 
322   /**
323    * @return Returns the dB_NAME.
324    */
325   public String getDB_NAME() {
326     return DB_NAME;
327   }
328 
329   /**
330    * @return the root
331    */
332   public String getSTATIC_ROOT() {
333     return STATIC_ROOT;
334   }
335 
336   private String getPathInfo(Melati melati) {
337     String initParameterPathInfo = getInitParameter("pathinfo");
338     String pathInfo = melati.getRequest().getPathInfo();
339     if (initParameterPathInfo == null) {
340       return pathInfo == null ? "" : pathInfo;
341     } else {
342       return initParameterPathInfo + (pathInfo == null ? "" : pathInfo);
343     }
344 
345   }
346 
347 }