001: /*
002: * Copyright Javelin Software, All rights reserved.
003: */
004:
005: package com.javelin.examples.swinglets;
006:
007: import java.net.*;
008: import java.util.*;
009: import java.io.*;
010: import java.awt.*;
011: import java.text.*;
012:
013: import javax.swing.*;
014: import javax.servlet.*;
015: import javax.servlet.http.*;
016:
017: import com.javelin.swinglets.*;
018: import com.javelin.swinglets.event.*;
019: import com.javelin.security.*;
020:
021: /**
022: * Servlet to display a demo Servlet.
023: *
024: * @author Dino Fancellu
025: */
026:
027: public class SecurityServlet extends HttpServlet {
028: SLabel homeLink = null;
029:
030: class NormalUser implements User {
031: String loginName;
032:
033: public String getLoginName() {
034: return loginName;
035: }
036:
037: public void setLoginName(String name) {
038: loginName = name;
039: }
040:
041: public String getFullName() {
042: return loginName;
043: }
044:
045: public boolean isLoggedIn() {
046: return true;
047: }
048:
049: public boolean hasPermission(String perm) {
050: if (perm.equalsIgnoreCase("BROWSE")) {
051: return true;
052: }
053: if (perm.equalsIgnoreCase("UPDATEPROFILE")) {
054: return true;
055: }
056: return false;
057: }
058:
059: public boolean hasPermissions(Object[] perms) {
060: for (int x = 0; x < perms.length; x++) {
061: if (hasPermission((String) perms[x])) {
062: return true;
063: }
064: }
065: return false;
066: }
067: }
068:
069: class LoginPanel extends SPanel implements FormListener {
070: STextField loginName = new STextField();
071: SPasswordField password = new SPasswordField();
072: SPanel results = new SPanel();
073: STable header = new STable(2, 1);
074: //SLabel logoutLink=new SLabel("Logout",new SLink("logout"));
075: SLabel registerLink = new SLabel("Register", new SLink(
076: "/register"));
077: SLabel forgotLink = new SLabel("Forgot password?", new SLink(
078: "forgot"));
079: SForm loginForm = null;
080: SForm logoutForm = null;
081:
082: public void formReset(com.javelin.swinglets.event.FormEvent ev) {
083: }
084:
085: public void formSubmitted(
086: com.javelin.swinglets.event.FormEvent ev) {
087: if (ev.getSource() == logoutForm) {
088: System.out.println("trying to log out");
089: Security.logout();
090: return;
091: }
092:
093: String name = loginName.getText();
094: String pass = password.getText();
095: results.removeAll();
096:
097: if (name.length() == 0) {
098: results.add(new SLabel("Name must not be blank"));
099: } else {
100: if (pass.equalsIgnoreCase("pass")) {
101: results.add(new SLabel("Password correct"));
102: NormalUser us = new NormalUser();
103: us.setLoginName(name);
104: Security.setUser(us);
105: } else {
106: results.add(new SLabel("Password incorrect"));
107: }
108: }
109:
110: System.out.println(name);
111: System.out.println(pass);
112: }
113:
114: public void onPaint() {
115: User user = Security.getUser();
116: if (user.isLoggedIn()) {
117: setupLoggedIn(user.getLoginName());
118: } else {
119: setupNotLoggedIn();
120: }
121: }
122:
123: public void setupLoggedIn(String name) {
124: header.setValueAt(new SLabel("Logged in as " + name), 0, 0);
125: logoutForm.setBackground(SColor.white);
126: // logoutLink.setBackground(SColor.white);
127: logoutForm.setParent(this );
128: header.setValueAt(logoutForm, 1, 0);
129:
130: //header.setValueAt(logoutLink,1,0);
131: }
132:
133: public void setupNotLoggedIn() {
134: header.setValueAt(new SLabel("Login"), 0, 0);
135:
136: loginForm.setBackground(SColor.white);
137: loginForm.setParent(this );
138: header.setValueAt(loginForm, 1, 0);
139: }
140:
141: public LoginPanel() {
142: // setup header
143: header.setIntercellPadding(new Dimension(1, 1));
144: header.setIntercellSpacing(new Dimension(1, 1));
145: header.setGridWidth(0);
146: header.setBackground(SColor.getColor("lightskyblue"));
147: add(header);
148: // setup bits for login
149: STable table = new STable(4, 2);
150: table.setIntercellPadding(new Dimension(1, 1));
151: table.setIntercellSpacing(new Dimension(1, 1));
152: table.setGridWidth(0);
153:
154: table.setBackground(SColor.white);
155: table.setForeground(SColor.black);
156:
157: loginForm = new SForm();
158: loginForm.add(table);
159: loginForm.addFormEventListener(this );
160:
161: logoutForm = new SForm();
162: logoutForm.add(new SButton("Logout"));
163: logoutForm.addFormEventListener(this );
164:
165: table.setValueAt(new SLabel("Login Name"), 0, 0);
166: table.setValueAt(loginName, 0, 1);
167: table.setValueAt(new SLabel("Password"), 1, 0);
168: table.setValueAt(password, 1, 1);
169: table.setValueAt(registerLink, 2, 0);
170: table.setValueAt(forgotLink, 2, 1);
171:
172: table.setValueAt(new SButton("Login"), 3, 0);
173: table.setValueAt(results, 3, 1);
174:
175: setBackground(SColor.getColor("skyblue1"));
176:
177: //add(results);
178:
179: }
180: }
181:
182: public synchronized void init(ServletConfig config)
183: throws ServletException {
184: super .init(config);
185: SUIManager
186: .setLookAndFeel("com.javelin.swinglets.plaf.javascript.JSLookAndFeel");
187: SwingletManager.setDefaultRealPath(config.getServletContext()
188: .getRealPath(""));
189: }
190:
191: public synchronized void service(HttpServletRequest request,
192: HttpServletResponse response) throws IOException,
193: ServletException {
194: //IF you to override the real path, on a per servlet basis.
195: super .service(request, response);
196: }
197:
198: // need to handle doPost else forms won't work.
199: public synchronized void doPost(HttpServletRequest request,
200: HttpServletResponse response) throws IOException,
201: ServletException {
202: try {
203: ServletManager servletManager = ServletManager
204: .getManager(request);
205: SComponent component = servletManager.handle(request,
206: response, null);
207: //component.paint( ServletManager.getOutput( component, response ));
208: doGet(request, response);
209: } catch (Exception e) {
210: e.printStackTrace();
211: response.sendError(response.SC_INTERNAL_SERVER_ERROR, e
212: .getMessage());
213: } finally {
214: SwingletManager.setSwingletManager(null);
215: }
216: }
217:
218: public synchronized void doUpdate(HttpServletRequest request,
219: HttpServletResponse response) throws IOException,
220: ServletException {
221: System.out.println("update");
222: User user = null;
223: user = Security.getUser();
224:
225: if (!user.hasPermission("UPDATEPROFILE")) {
226: response.sendRedirect(request.getServletPath());
227: return;
228: }
229:
230: PrintWriter out = response.getWriter();
231: SFrame frame = new SFrame();
232: frame.setTitle("Security Servlet/Update");
233: frame.setLayoutManager(new SFlowLayout(SConstants.LEFT));
234: frame.add(new LoginPanel());
235: frame
236: .add(new SLabel(
237: "Pretend there's a panel to update things"));
238: frame.add(homeLink);
239: frame.paint(out);
240: response.setContentType(frame.getContentType());
241: }
242:
243: public synchronized void doBrowse(HttpServletRequest request,
244: HttpServletResponse response) throws IOException,
245: ServletException {
246: System.out.println("browse");
247: STable table = new STable(5, 5);
248:
249: for (int x = 0; x < 5; x++) {
250: for (int y = 0; y < 5; y++) {
251: StringBuffer buff = new StringBuffer();
252:
253: buff.append(x);
254: buff.append(' ');
255: buff.append(y);
256: table.setValueAt(buff, x, y);
257: }
258: }
259: PrintWriter out = response.getWriter();
260: SFrame frame = new SFrame();
261: frame.add(new LoginPanel());
262: frame.add(table);
263: frame.setTitle("Security Servlet/Update");
264: frame.setLayoutManager(new SFlowLayout(SConstants.LEFT));
265: frame
266: .add(new SLabel(
267: "Pretend there's a panel to update things"));
268: frame.add(homeLink);
269: frame.paint(out);
270: response.setContentType(frame.getContentType());
271:
272: }
273:
274: public synchronized void doGet(HttpServletRequest request,
275: HttpServletResponse response) throws IOException,
276: ServletException {
277: ServletManager servletManager = ServletManager
278: .getManager(request);
279: homeLink = new SLabel("Home", new SLink(request
280: .getServletPath()));
281: String query = request.getQueryString();
282: System.out.println("Query=" + query);
283:
284: if ("update".equalsIgnoreCase(query)) {
285: doUpdate(request, response);
286: return;
287: }
288:
289: if ("browse".equalsIgnoreCase(query)) {
290: doBrowse(request, response);
291: return;
292: }
293:
294: PrintWriter out = response.getWriter();
295: SFrame frame = new SFrame();
296: frame.setTitle("Security Servlet");
297: frame.setLayoutManager(new SFlowLayout(SConstants.LEFT));
298:
299: frame.add(new LoginPanel());
300:
301: User user = null;
302:
303: user = Security.getUser();
304:
305: StringBuffer fullpath = javax.servlet.http.HttpUtils
306: .getRequestURL(request);
307: fullpath.append('/');
308:
309: //frame.add(new SLabel("<BASE HREF=\""+fullpath+"\">"));
310:
311: if (user.hasPermission("BROWSE")) {
312: frame.add(new SLabel("Browse stuff", new SLink("?browse")));
313: }
314:
315: if (user.hasPermission("UPDATEPROFILE")) {
316: frame.add(new SLabel("Update your profile", new SLink(
317: "?update")));
318: }
319:
320: frame.paint(out);
321: response.setContentType(frame.getContentType());
322: }
323:
324: }
|