Coverage Report - org.paneris.bibliomania.BibliomaniaAccessHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
BibliomaniaAccessHandler
11%
3/27
0%
0/8
3.333
 
 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  1
 public class BibliomaniaAccessHandler extends HttpSessionAccessHandler {
 17  
   protected String loginPageServletClassName() {
 18  0
     return "org.paneris.bibliomania.BibliomaniaLogin";
 19  
   }
 20  
 
 21  1
   private static String key = null;
 22  1
   private static final Random random = new Random();
 23  
 
 24  
   public String loginPageURL(Melati melati,
 25  
                              HttpServletRequest request) {
 26  0
     StringBuffer url = new StringBuffer();
 27  0
     HttpUtil.appendZoneURL(url, request);
 28  0
     url.append('/');
 29  0
     url.append(loginPageServletClassName());
 30  
 
 31  0
     return url.toString();
 32  
   }
 33  
   
 34  
   
 35  
   public void logUsIn(Melati melati, org.melati.poem.User user) {
 36  0
     super.logUsIn(melati,user);
 37  
 
 38  0
     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  0
     if (key == null) {
 45  0
       key = "" + random.nextInt();
 46  
       try {
 47  0
         File tmpkeys = new File("/tmp/bibliomaniakey");
 48  0
         if (!tmpkeys.exists())
 49  0
           tmpkeys.mkdir();
 50  0
         File[] old = tmpkeys.listFiles();
 51  0
         for (int f = 0; f < old.length; ++f)
 52  0
           old[f].delete();
 53  0
         new FileOutputStream(new File(tmpkeys, key)).close();
 54  
       }
 55  0
       catch (IOException e) {
 56  0
         throw new UnexpectedExceptionException(e);
 57  0
       }
 58  
     }
 59  0
     Cookie c = new Cookie("reg", key);
 60  0
     c.setPath("/");
 61  0
     melati.getResponse().addCookie(c);
 62  0
   }
 63  
 }