Ignore:
Timestamp:
2004-12-06T11:44:03+13:00 (19 years ago)
Author:
schweer
Message:

user authentication works; user information and subscriptions/predicates are stored to thedatabase

File:
1 edited

Legend:

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

    r8724 r8738  
    1414import java.util.*;
    1515
    16 import javax.mail.Session;
    1716import javax.servlet.http.*;
    1817
     
    2019import org.apache.velocity.context.Context;
    2120import org.apache.velocity.servlet.VelocityServlet;
     21import org.greenstone.gsdlas.users.UserManagementException;
    2222import org.greenstone.gsdlas.users.UserManager;
    2323
     
    3030public class AlertingService extends VelocityServlet {
    3131   
    32     public static final Set actions = new TreeSet();
     32    public static final Set actions;
    3333    static {
    34         actions.add("createSubscription");
    35         actions.add("deleteSubscription");
    36         actions.add("editSubscription");
    37         actions.add("showFeed");
    38         actions.add("listSubscriptions");
    39         actions.add("login");
    40         actions.add("register");
    41         actions.add("logout");
     34        Set set = new TreeSet();
     35        set.add("createSubscription");
     36        set.add("deleteSubscription");
     37        set.add("editSubscription");
     38        set.add("showFeed");
     39        set.add("listSubscriptions");
     40       
     41        set.add("login");
     42        set.add("register");
     43        set.add("logout");
     44        set.add("showLoginForm");
     45        set.add("showRegistrationForm");
     46        actions = Collections.unmodifiableSet(set);
    4247    }
    4348   
     
    6772        } catch (Exception e) {
    6873            String message = "An error has occured, I couldn't do what you told me to do.";
    69             String details = e.getMessage() + " (" + e.getClass().getName() + "), action is " + action;
     74            String details = e.getMessage() + " (" + e.getClass().getName() + "); "
     75                + e.getCause()
     76                + " ; action is " + action;
    7077            return showError(context, message, details);
    7178        }
     
    99106        if (!UserManager.getInstance().isLoggedIn(session)) {
    100107            session.setAttribute("next_action", "createSubscription");
    101             return "login.vm";
     108            return showLoginForm(arguments, context);
    102109        }
    103110        if (arguments.containsKey("next_page") && arguments.get("next_page").equals("finish")) {
     111            arguments.putAll(getPageArgsFromSession(session));
    104112            ProfileStore.getInstance().createSubscription(arguments);
    105113            return listSubscriptions(arguments, context);
     
    113121        if (!UserManager.getInstance().isLoggedIn(session)) {
    114122            session.setAttribute("next_action", "deleteSubscription");
    115             return "login.vm";
     123            return showLoginForm(arguments, context);
    116124        }
    117125        String subscriptionID = (String) arguments.get("subscriptionID");
     
    124132        if (!UserManager.getInstance().isLoggedIn(session)) {
    125133            session.setAttribute("next_action", "editSubscription");
    126             return "login.vm";
     134            return showLoginForm(arguments, context);
    127135        }
    128136        if (arguments.containsKey("next_page") && arguments.get("next_page").equals("finish")) {
     
    138146        return "feed.vm";
    139147    }
     148   
     149    public String showLoginForm(Map arguments, Context context) {
     150        return "login.vm";
     151    }
     152   
     153    public String showRegistrationForm(Map arguments, Context context) {
     154        return "register.vm";
     155    }
    140156
    141157    public String listSubscriptions(Map arguments, Context context) {
    142         context.put("title", "List of Subscriptions");
     158        HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true);
     159        if (!UserManager.getInstance().isLoggedIn(session)) {
     160            session.setAttribute("next_action", "listSubscriptions");
     161            return showLoginForm(arguments, context);
     162        }
     163        String username = (String) session.getAttribute("username");
     164        context.put("title", "List of Subscriptions for " + username);
    143165        context.put("list", ProfileStore.getInstance().getAllSubscriptions());
    144166        return "list.vm";
     
    147169    public String login(Map arguments, Context context) throws Exception {
    148170        HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true);
    149         UserManager.getInstance().loginUser(arguments, session);
     171        try {
     172            UserManager.getInstance().loginUser(arguments, session);
     173        } catch (UserManagementException e) {
     174            context.put("error", Boolean.TRUE);
     175            context.put("errormessage", e.getMessage());
     176            return showLoginForm(arguments, context);
     177        }
    150178        if (session.getAttribute("next_action") != null) {
    151179            String nextAction = (String) session.getAttribute("next_action");
     
    158186    public String register(Map arguments, Context context) throws Exception {
    159187        HttpSession session = ((HttpServletRequest)context.get(REQUEST)).getSession(true);
    160         UserManager.getInstance().createUser(arguments, session);
     188        try {
     189            UserManager.getInstance().createUser(arguments, session);
     190        } catch (UserManagementException e) {
     191            context.put("error", Boolean.TRUE);
     192            context.put("errormessage", e.getMessage());
     193            return showRegistrationForm(arguments, context);
     194        }
    161195        return login(arguments, context);
    162196    }
     
    194228        for (Iterator iter = args.keySet().iterator(); iter.hasNext();) {
    195229            String key = (String) iter.next();
    196             if (!key.equals("action")) {
    197                 String firstValue = ((String[]) args.get(key))[0];
     230            if (key.equals("type") || key.equals("host")
     231                    || key.equals("collection") || key.equals("way")) {
     232                // multi-valued attributes
     233                String[] values = ((String[]) args.get(key));
     234                result.put(key, Arrays.asList(values));
     235            } else {
     236                String firstValue = ((String[])args.get(key))[0];
    198237                result.put(key, firstValue);
    199238            }
     
    260299        // get page-specific stuff
    261300        if (nextPage.equals("host")) {
    262            
     301            GreenstoneCommunicator gsComm = null;
     302            String[] hostNames;
     303            try {
     304                gsComm = new GreenstoneCommunicator();
     305                hostNames = gsComm.getHostNames();
     306            } catch (Exception e) {
     307                hostNames = new String[] { "localhost" };
     308            }
     309            context.put("hostnames", hostNames);
    263310        } else if (nextPage.equals("collection")) {
    264            
     311            // TODO might be query instead of just name
     312            List hostNames = (List) arguments.get("host");
     313            GreenstoneCommunicator gsComm = null;
     314            Map collNames = new TreeMap();
     315            for (Iterator iter = hostNames.iterator(); iter.hasNext();) {
     316                String host = (String) iter.next();
     317                Set collNamesForHost = new TreeSet();   
     318                try {
     319                    gsComm = new GreenstoneCommunicator(new URL("http://" + host + ":8080/soap/servlet/rpcrouter"));
     320                    collNamesForHost.addAll(Arrays.asList(gsComm.getCollectionNames()));
     321                } catch (Exception e) {
     322                    // TODO Auto-generated catch block
     323                    e.printStackTrace();
     324                }   
     325                collNames.put(host, collNamesForHost);
     326            }
     327            context.put("collectionnames", collNames);
     328            context.put("hostnames", hostNames);
    265329        }
    266330       
     
    324388    }
    325389
     390    private Map getPageArgsFromSession(HttpSession session) {
     391        Map result = new TreeMap();
     392        Map pageArgs = (Map) session.getAttribute("page_args");
     393        if (pageArgs != null) {
     394            for (Iterator iter = pageArgs.values().iterator(); iter.hasNext();) {
     395                Map args = (Map) iter.next();
     396                result.putAll(args);
     397            }
     398        }
     399        return result;
     400    }
     401   
    326402    /**
    327403     * @param page
Note: See TracChangeset for help on using the changeset viewer.