View Javadoc

1   package org.paneris.bibliomania;
2   
3   import java.io.File;
4   import java.io.FileOutputStream;
5   import java.io.IOException;
6   import java.util.Random;
7   
8   import javax.servlet.http.Cookie;
9   import javax.servlet.http.HttpServletRequest;
10  
11  import org.melati.Melati;
12  import org.melati.login.HttpSessionAccessHandler;
13  import org.melati.util.HttpUtil;
14  import org.melati.util.UnexpectedExceptionException;
15  
16  public class BibliomaniaAccessHandler extends HttpSessionAccessHandler {
17    protected String loginPageServletClassName() {
18      return "org.paneris.bibliomania.BibliomaniaLogin";
19    }
20  
21    private static String key = null;
22    private static final Random random = new Random();
23  
24    public String loginPageURL(Melati melati,
25                               HttpServletRequest request) {
26      StringBuffer url = new StringBuffer();
27      HttpUtil.appendZoneURL(url, request);
28      url.append('/');
29      url.append(loginPageServletClassName());
30  
31      return url.toString();
32    }
33    
34    
35    public void logUsIn(Melati melati, org.melati.poem.User user) {
36      super.logUsIn(melati,user);
37  
38      if (user == null) return;
39  
40      // funky code to to ensure users have logged in before accessing some
41      // plain html pages
42      // @see rewrites.apache
43      
44      if (key == null) {
45        key = "" + random.nextInt();
46        try {
47          File tmpkeys = new File("/tmp/bibliomaniakey");
48          if (!tmpkeys.exists())
49            tmpkeys.mkdir();
50          File[] old = tmpkeys.listFiles();
51          for (int f = 0; f < old.length; ++f)
52            old[f].delete();
53          new FileOutputStream(new File(tmpkeys, key)).close();
54        }
55        catch (IOException e) {
56          throw new UnexpectedExceptionException(e);
57        }
58      }
59      Cookie c = new Cookie("reg", key);
60      c.setPath("/");
61      melati.getResponse().addCookie(c);
62    }
63  }