001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2007.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.http.webclient.server;
007:
008: import javax.servlet.http.Cookie;
009: import javax.servlet.http.HttpServletRequest;
010: import javax.servlet.http.HttpServletResponse;
011: import javax.servlet.http.HttpSession;
012:
013: import org.slf4j.Logger;
014: import org.slf4j.LoggerFactory;
015: import org.springframework.validation.BindException;
016: import org.springframework.web.servlet.ModelAndView;
017: import org.springframework.web.servlet.mvc.SimpleFormController;
018:
019: import org.openrdf.http.webclient.SessionKeys;
020: import org.openrdf.repository.Repository;
021:
022: public class ServerSelectionController extends SimpleFormController {
023:
024: static final Logger logger = LoggerFactory
025: .getLogger(ServerSelectionController.class);
026:
027: private String defaultServerContextName;
028:
029: /**
030: * @return Returns the defaultWebapp.
031: */
032: public String getDefaultServerContextName() {
033: return defaultServerContextName;
034: }
035:
036: /**
037: * @param defaultWebapp The defaultWebapp to set.
038: */
039: public void setDefaultServerContextName(String defaultWebapp) {
040: this .defaultServerContextName = defaultWebapp;
041: }
042:
043: @Override
044: protected ModelAndView onSubmit(HttpServletRequest request,
045: HttpServletResponse response, Object command,
046: BindException errors) throws Exception {
047: ServerSelection serverSelection = (ServerSelection) command;
048:
049: String path = request.getContextPath();
050:
051: Cookie useAlwaysCookie = new Cookie(
052: ServerSelection.COOKIE_PREFIX + "."
053: + ServerSelection.COOKIE_REMEMBER, String
054: .valueOf(serverSelection.isRemember()));
055: useAlwaysCookie.setPath(path);
056: useAlwaysCookie.setMaxAge(365 * 24 * 60 * 60);
057: response.addCookie(useAlwaysCookie);
058:
059: Cookie serverURLCookie = new Cookie(
060: ServerSelection.COOKIE_PREFIX + "."
061: + ServerSelection.COOKIE_URL, serverSelection
062: .getLocation());
063: serverURLCookie.setPath(path);
064: if (serverSelection.isRemember()) {
065: serverURLCookie.setMaxAge(365 * 24 * 60 * 60);
066: }
067: response.addCookie(serverURLCookie);
068:
069: Cookie serverTypeCookie = new Cookie(
070: ServerSelection.COOKIE_PREFIX + "."
071: + ServerSelection.COOKIE_TYPE, serverSelection
072: .getType());
073: serverTypeCookie.setPath(path);
074: if (serverSelection.isRemember()) {
075: serverTypeCookie.setMaxAge(365 * 24 * 60 * 60);
076: }
077: response.addCookie(serverTypeCookie);
078:
079: HttpSession session = request.getSession(true);
080: Server server = (Server) session
081: .getAttribute(SessionKeys.SERVER_KEY);
082: // if a new or different server was selected
083: if (server == null
084: || !server.getLocation().equals(
085: serverSelection.getLocation())) {
086:
087: // TODO: verify shutting down the repos is the right thing to do here
088: // (could/should queries still be running when switching to another
089: // server in the same session?)
090:
091: // shutdown the "current" repository, if any
092: Repository repo = (Repository) session
093: .getAttribute(SessionKeys.REPOSITORY_KEY);
094: if (repo != null) {
095: repo.shutDown();
096: session.removeAttribute(SessionKeys.REPOSITORY_KEY);
097: session
098: .removeAttribute(SessionKeys.REPOSITORY_INFO_KEY);
099: }
100:
101: // insert the new Server into the session
102: session.setAttribute(SessionKeys.SERVER_KEY, new Server(
103: serverSelection.getLocation()));
104: }
105:
106: return super .onSubmit(request, response, null, errors);
107: }
108:
109: @Override
110: protected Object formBackingObject(HttpServletRequest request)
111: throws Exception {
112: Object result = super .formBackingObject(request);
113:
114: ServerSelection serverSelection = (ServerSelection) result;
115: serverSelection
116: .setDefaultServerContextName(getDefaultServerContextName());
117: serverSelection.setDefaultServerURL(request);
118: serverSelection.setFromCookies(request);
119:
120: return result;
121: }
122: }
|