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 java.net.MalformedURLException;
009: import java.net.URL;
010:
011: import javax.servlet.http.Cookie;
012: import javax.servlet.http.HttpServletRequest;
013:
014: public class ServerSelection {
015:
016: public static final String DEFAULT = "default";
017:
018: public static final String OTHER = "other";
019:
020: static final String COOKIE_PREFIX = "server.select";
021:
022: static final String COOKIE_REMEMBER = "remember";
023:
024: static final String COOKIE_URL = "url";
025:
026: static final String COOKIE_TYPE = "type";
027:
028: private String type = DEFAULT;
029:
030: private String location;
031:
032: private String defaultServerURL;
033:
034: private boolean remember;
035:
036: private String defaultServerContextName;
037:
038: /**
039: * @return Returns the defaultWebapp.
040: */
041: public String getDefaultServerContextName() {
042: return defaultServerContextName;
043: }
044:
045: /**
046: * @param defaultWebapp The defaultWebapp to set.
047: */
048: public void setDefaultServerContextName(String defaultWebapp) {
049: this .defaultServerContextName = defaultWebapp;
050: }
051:
052: public String getLocation() {
053: String result = location;
054:
055: if (getType().equals(DEFAULT)) {
056: result = defaultServerURL;
057: }
058: return result;
059: }
060:
061: public void setLocation(String location) {
062: this .location = location;
063: }
064:
065: public String getType() {
066: return type;
067: }
068:
069: public void setType(String type) {
070: this .type = type;
071: }
072:
073: public boolean isRemember() {
074: return remember;
075: }
076:
077: public void setRemember(boolean remember) {
078: this .remember = remember;
079: }
080:
081: public String getDefaultServerURL() {
082: return defaultServerURL;
083: }
084:
085: public void setDefaultServerURL(String localURL) {
086: this .defaultServerURL = localURL;
087: }
088:
089: void setDefaultServerURL(HttpServletRequest request) {
090: StringBuilder result = new StringBuilder();
091:
092: try {
093: URL requestURL = new URL(request.getRequestURL().toString());
094: String protocol = requestURL.getProtocol();
095:
096: result.append(protocol);
097: result.append("://");
098: result.append(request.getServerName());
099:
100: // append port if different from default for protocol
101: if (!(protocol.equals("http") && request.getLocalPort() == 80)
102: && !(protocol.equals("https") && request
103: .getLocalPort() == 443)) {
104: result.append(":");
105: result.append(request.getLocalPort());
106: }
107: result.append(getDefaultServerContextName());
108: } catch (MalformedURLException e) {
109: // never happens
110: e.printStackTrace();
111: }
112:
113: setDefaultServerURL(result.toString());
114: }
115:
116: void setFromCookies(HttpServletRequest request) {
117: Cookie[] cookies = request.getCookies();
118: if (cookies != null) {
119: for (Cookie cookie : cookies) {
120: String cookieName = cookie.getName();
121: String cookieValue = cookie.getValue();
122: if (cookieName.startsWith(COOKIE_PREFIX)) {
123: if (cookieName.endsWith(COOKIE_URL)) {
124: setLocation(cookieValue);
125: } else if (cookieName.endsWith(COOKIE_TYPE)) {
126: setType(cookieValue);
127: } else if (cookieName.endsWith(COOKIE_REMEMBER)) {
128: setRemember(Boolean.parseBoolean(cookieValue));
129: }
130: }
131: }
132: }
133: }
134: }
|