001: /*
002: * $Id: Context.java,v 1.13 2002/09/16 08:05:06 jkl Exp $
003: *
004: * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
005: *
006: * Use is subject to license terms, as defined in
007: * Anvil Sofware License, Version 1.1. See LICENSE
008: * file, or http://njet.org/license-1.1.txt
009: */
010: package anvil.server;
011:
012: import java.io.IOException;
013: import java.io.OutputStream;
014: import java.net.URL;
015: import java.net.MalformedURLException;
016: import javax.servlet.http.HttpServletRequest;
017: import javax.servlet.http.HttpServletResponse;
018: import anvil.Log;
019: import anvil.core.Any;
020: import anvil.parser.InputSource;
021: import anvil.parser.URLInputSource;
022: import anvil.session.Session;
023: import anvil.session.SessionContainer;
024:
025: /**
026: * class Context
027: *
028: * @author: Jani Lehtimäki
029: */
030: public class Context {
031:
032: private Address _address;
033: private RequestAdapter _adapter;
034: private HttpServletRequest _request = null;
035: private HttpServletResponse _response = null;
036: private OutputStream _output = null;
037: private Session _session = null;
038: private Citizen _citizen = null;
039: private String _resource = null;
040: private Log _log;
041:
042: public Context(Address address, String resource) {
043: _address = address;
044: _resource = resource;
045: }
046:
047: void init(RequestAdapter adapter, HttpServletRequest request,
048: HttpServletResponse response) {
049: _adapter = adapter;
050: _request = request;
051: _response = response;
052: _output = new OutputStreamProxy(_response);
053: String session_id = adapter.getSession();
054: if (session_id != null) {
055: SessionContainer container = _address.getZone()
056: .getSessionContainer();
057: if (container != null) {
058: _session = container.getSession(session_id);
059: if (_session != null) {
060: String citizen = _session.getCitizen();
061: if (citizen != null) {
062: Realm realm = _address.getZone()
063: .getAuthorization().getRealm();
064: if (realm != null) {
065: _citizen = realm.getCitizen(citizen);
066: }
067: }
068: }
069: }
070: }
071: _log = _address.getZone().log();
072: }
073:
074: void destroy() {
075: _address = null;
076: _request = null;
077: _response = null;
078: _output = null;
079: _session = null;
080: _citizen = null;
081: _adapter = null;
082: _resource = null;
083: _log = null;
084: }
085:
086: public Log log() {
087: return _log;
088: }
089:
090: public OutputStream getOutputStream() throws IOException {
091: return _output;
092: }
093:
094: public Address getAddress() {
095: return _address;
096: }
097:
098: public Server getServer() {
099: return _address.getServer();
100: }
101:
102: public String getHostname() {
103: return _address.getHostname();
104: }
105:
106: public String getPathinfo() {
107: return _address.getPathinfo();
108: }
109:
110: public String getOriginalPathinfo() {
111: return _adapter.getPathinfo();
112: }
113:
114: public RequestAdapter getAdapter() {
115: return _adapter;
116: }
117:
118: public String getResource() {
119: return _resource;
120: }
121:
122: public void setResource(String resource) {
123: _resource = resource;
124: }
125:
126: public Zone getZone() {
127: return _address.getZone();
128: }
129:
130: public URL getURL() {
131: return _address.getURL();
132: }
133:
134: public HttpServletRequest getRequest() {
135: return _request;
136: }
137:
138: public HttpServletResponse getResponse() {
139: return _response;
140: }
141:
142: public String getSessionId() {
143: if (_session != null) {
144: return _session.getId();
145: } else {
146: return _adapter.getSession();
147: }
148: }
149:
150: public Session getSession() {
151: return _session;
152: }
153:
154: public void setSession(Session session) {
155: _session = session;
156: }
157:
158: public void setCitizen(Citizen citizen) {
159: _citizen = citizen;
160: if (_session != null) {
161: _session.setCitizen(citizen.getName());
162: }
163: }
164:
165: public Citizen getCitizen() {
166: return _citizen;
167: }
168:
169: public Session getSession(String session_id) {
170: SessionContainer container = getZone().getSessionContainer();
171: if (container != null) {
172: return container.getSession(session_id);
173: } else {
174: return null;
175: }
176: }
177:
178: public Session createSession() {
179: SessionContainer container = getZone().getSessionContainer();
180: if (container != null) {
181: SessionPreferences prefs = getZone()
182: .getSessionPreferences();
183: return container.createSession(prefs.getLifetime(), prefs
184: .getTimeout());
185: } else {
186: return null;
187: }
188: }
189:
190: }
|