Ignore:
Timestamp:
2004-12-02T09:50:37+13:00 (19 years ago)
Author:
schweer
Message:

started to rework control flow, user authentication

Location:
trunk/greenstone3-extensions/gsdl-as
Files:
5 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/AlertingService.java

    r8644 r8717  
    99package org.greenstone.gsdlas;
    1010
    11 import java.io.IOException;
    1211import java.lang.reflect.Method;
    1312import java.net.MalformedURLException;
     
    1514import java.util.*;
    1615
    17 import javax.servlet.ServletException;
    18 import javax.servlet.http.HttpServletRequest;
    19 import javax.servlet.http.HttpServletResponse;
     16import javax.servlet.http.*;
    2017
    2118import org.apache.velocity.Template;
    2219import org.apache.velocity.context.Context;
    23 import org.apache.velocity.exception.ParseErrorException;
    24 import org.apache.velocity.exception.ResourceNotFoundException;
    2520import org.apache.velocity.servlet.VelocityServlet;
    26 import org.greenstone.gsdlas.profiles.*;
     21import org.greenstone.gsdlas.users.UserManager;
    2722
    2823/**
     
    3631    public static final Set actions = new TreeSet();
    3732    static {
    38         actions.add("subscribe");
     33        actions.add("createSubscription");
     34        actions.add("deleteSubscription");
     35        actions.add("editSubscription");
    3936        actions.add("showFeed");
    40         actions.add("listSubscriptions"); // TODO remove (just for debugging)
    41     }
     37        actions.add("listSubscriptions");
     38        actions.add("login");
     39        actions.add("register");
     40    }
     41   
    4242   
    4343    protected Template handleRequest(HttpServletRequest req,
     
    4747       
    4848        if (action == null || !actions.contains(action)) {
    49             context.put("title", "Unknown action");
    50             context.put("message", "I don't know how to " + action);
    51             context.put("details", "The only actions I know are " + actions);
    52             try {
    53                 return getTemplate("error.vm");
    54             } catch (ResourceNotFoundException e1) {
    55                 // TODO Auto-generated catch block
    56                 e1.printStackTrace();
    57             } catch (ParseErrorException e1) {
    58                 // TODO Auto-generated catch block
    59                 e1.printStackTrace();
    60             } catch (Exception e1) {
    61                 // TODO Auto-generated catch block
    62                 e1.printStackTrace();
    63             }
    64             return null;
     49            String title = "Unknown action";
     50            String message = "I don't know how to " + action;
     51            String details = "The only actions I know are " + actions;
     52            return showError(context, message, details);
    6553        }
    6654       
    6755
    6856        Map args = req.getParameterMap();
     57        // TODO stop this, we need multi-valued stuff
    6958        args = normalise(args);
    7059       
     
    7564            templateString = (String) method.invoke(this, new Object[] {args, context});
    7665        } catch (Exception e) {
    77             templateString = "error.vm";
    78             context.put("title", "Error");
    79             context.put("message", "A general error has occured, I couldn't do what you told me to do.");
    80             context.put("details", e.getMessage() + " (" + e.getClass().getName() + "), action is " + action);
     66            String message = "An error has occured, I couldn't do what you told me to do.";
     67            String details = e.getMessage() + " (" + e.getClass().getName() + "), action is " + action;
     68            return showError(context, message, details);
    8169        }
    8270       
     
    8876            e.printStackTrace();
    8977            try {
    90                 error(req, res, e);
    91             } catch (ServletException e2) {
    92                 // TODO Auto-generated catch block
    93                 e2.printStackTrace();
    94             } catch (IOException e2) {
     78                error((HttpServletRequest)context.get(REQUEST),
     79                        (HttpServletResponse)context.get(RESPONSE),
     80                        e);
     81            } catch (Exception e2) {
    9582                // TODO Auto-generated catch block
    9683                e2.printStackTrace();
     
    10491     * @param context
    10592     * @return the Velocity template to use
     93     * @throws Exception
    10694     */
    107     public String subscribe(Map arguments, Context context) {
    108         context.put("title", "Adding Subscription");
    109         if (arguments.size() == 0) {
    110             context.put("message", "Empty subscription, not added");
    111             context.put("details", "A subscription has to define at least one predicate.");
    112             return "error.vm";
    113         }
    114         try {
    115             // TODO pass user information to profile store
    116             Subscription sub = new Subscription(arguments);
    117             // TODO this should probably be somewhere else
    118             for (Iterator iter = sub.getPredicates().iterator(); iter.hasNext();) {
    119                 Predicate predicate = (Predicate) iter.next();
    120                 if (predicate != null) {
    121                     predicate.addSubscription(sub);
    122                 }
    123             }
    124             ProfileStore.getInstance().addSubscription(sub);
    125         } catch (ParseException e) {
    126             context.put("message", "Eror in subscription creation, not added");
    127             context.put("details", e.getMessage());
    128             return "error.vm";
    129         }
    130         context.put("message", "Subscription added successfully");
    131         return "general.vm";
     95    public String createSubscription(Map arguments, Context context) throws Exception {
     96        HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true);
     97        if (!UserManager.getInstance().isLoggedIn(session)) {
     98            session.setAttribute("next_action", "createSubscription");
     99            return "login.vm";
     100        }
     101        // TODO handle wizard-style stuff
     102        // if "finish"
     103        ProfileStore.getInstance().createSubscription(arguments);
     104        return listSubscriptions(arguments, context);
     105        // else show next page
     106    }
     107
     108    public String deleteSubscription(Map arguments, Context context) {
     109        HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true);
     110        if (!UserManager.getInstance().isLoggedIn(session)) {
     111            session.setAttribute("next_action", "deleteSubscription");
     112            return "login.vm";
     113        }
     114        String subscriptionID = (String) arguments.get("subscriptionID");
     115        ProfileStore.getInstance().deleteSubscription(subscriptionID);
     116        return listSubscriptions(arguments, context);
     117    }
     118   
     119    public String editSubscription(Map arguments, Context context) {
     120        HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true);
     121        if (!UserManager.getInstance().isLoggedIn(session)) {
     122            session.setAttribute("next_action", "editSubscription");
     123            return "login.vm";
     124        }
     125        // TODO handle wizard-style stuff
     126        // if "finish"
     127        ProfileStore.getInstance().changeSubscription(arguments, session);
     128        return listSubscriptions(arguments, context);
     129        // else show next page
    132130    }
    133131   
     
    136134        return "feed.vm";
    137135    }
    138    
     136
    139137    public String listSubscriptions(Map arguments, Context context) {
    140138        context.put("title", "List of Subscriptions");
    141139        context.put("list", ProfileStore.getInstance().getAllSubscriptions());
    142140        return "list.vm";
     141    }
     142   
     143    public String login(Map arguments, Context context) throws Exception {
     144        HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true);
     145        UserManager.getInstance().loginUser(arguments, session);
     146        if (session.getAttribute("next_action") != null) {
     147            String nextAction = (String) session.getAttribute("next_action");
     148            Method method = AlertingService.class.getDeclaredMethod(nextAction, new Class[] {Map.class, Context.class});
     149            return (String) method.invoke(this, new Object[] {arguments, context});
     150        }
     151        return listSubscriptions(arguments, context);
     152    }
     153   
     154    public String register(Map arguments, Context context) throws Exception {
     155        HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true);
     156        UserManager.getInstance().createUser(arguments, session);
     157        return login(arguments, context);
    143158    }
    144159   
     
    172187        return result;
    173188    }
     189
     190    /**
     191     * @param context
     192     * @param message
     193     * @param details
     194     * @return
     195     */
     196    private Template showError(Context context, String message, String details) {
     197        context.put("title", "Error");
     198        context.put("message", message);
     199        context.put("details", details);
     200        try {
     201            return getTemplate("error.vm");
     202        } catch (Exception e) {
     203            try {
     204                super.error((HttpServletRequest)context.get("req"),
     205                        (HttpServletResponse)context.get("res"),
     206                        e);
     207            } catch (Exception e2) {
     208                e2.printStackTrace();
     209            }
     210        }
     211        return null;
     212    }
    174213}
  • trunk/greenstone3-extensions/gsdl-as/src/org/greenstone/gsdlas/ProfileStore.java

    r8630 r8717  
    1111import java.util.*;
    1212
     13import javax.servlet.http.HttpSession;
     14
    1315import org.greenstone.gsdlas.profiles.*;
    1416import org.greenstone.gsdlas.profiles.IdEqualsPredicate;
     
    4850     *            the subscription to add to the profile store.
    4951     */
    50     public void addSubscription(Subscription subscription) {
     52    private void addSubscription(Subscription subscription) {
    5153        Predicate documentIdPredicate = subscription.getPredicate("documentID");
    5254        Predicate collectionIdPredicate = subscription.getPredicate("collectionID");
     
    267269        return Collections.unmodifiableSet(allSubscriptions);
    268270    }
     271
     272
     273    /**
     274     * @param arguments
     275     * @throws ParseException
     276     */
     277    public void createSubscription(Map arguments) throws ParseException {
     278        // TODO pass user information to profile store
     279        Subscription sub = new Subscription(arguments);
     280        // TODO this should probably be somewhere else
     281        for (Iterator iter = sub.getPredicates().iterator(); iter.hasNext();) {
     282            Predicate predicate = (Predicate) iter.next();
     283            if (predicate != null) {
     284                predicate.addSubscription(sub);
     285            }
     286        }
     287        addSubscription(sub);
     288    }
     289
     290   
     291    /**
     292     * @param subscriptionID
     293     */
     294    public void deleteSubscription(String subscriptionID) {
     295        // TODO Auto-generated method stub
     296       
     297    }
     298
     299    /**
     300     * @param arguments
     301     * @param session
     302     */
     303    public void changeSubscription(Map arguments, HttpSession session) {
     304        String subscriptionID = (String) arguments.get("subscriptionID");
     305        // TODO Auto-generated method stub
     306       
     307    }
    269308   
    270309}
  • trunk/greenstone3-extensions/gsdl-as/test-src/org/greenstone/gsdlas/ProfileStoreTest.java

    r8630 r8717  
    8383    }
    8484   
    85     private void addSubscription(Map valueMap) throws ParseException {   
    86         Subscription sub = new Subscription(valueMap);
    87         // TODO this should probably be somewhere else
    88         for (Iterator iter = sub.getPredicates().iterator(); iter.hasNext();) {
    89             Predicate predicate = (Predicate) iter.next();
    90             if (predicate != null) {
    91                 predicate.addSubscription(sub);
    92             }
    93         }
    94         ProfileStore.getInstance().addSubscription(sub);   
     85    private void addSubscription(Map valueMap) throws ParseException { 
     86        ProfileStore.getInstance().createSubscription(valueMap);   
    9587    }
    9688   
Note: See TracChangeset for help on using the changeset viewer.