01: /*
02: * @(#)sendRedirect.java 1.2 04/12/06
03: *
04: * Copyright (c) 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.servlet;
10:
11: import pnuts.servlet.*;
12: import pnuts.lang.*;
13:
14: import java.net.URL;
15: import java.io.IOException;
16: import javax.servlet.*;
17: import javax.servlet.http.*;
18:
19: /*
20: * function sendRedirect(url)
21: */
22: public class sendRedirect extends PnutsFunction {
23:
24: public sendRedirect() {
25: super ("sendRedirect");
26: }
27:
28: public boolean defined(int nargs) {
29: return (nargs == 1);
30: }
31:
32: protected Object exec(Object args[], Context context) {
33: int nargs = args.length;
34: if (nargs == 1) {
35: Object arg = args[0];
36: HttpServletResponse response = (HttpServletResponse) context
37: .get(PnutsServlet.SERVLET_RESPONSE);
38: if (response == null) {
39: throw new IllegalStateException();
40: }
41: try {
42: if (arg instanceof String) {
43: response.sendRedirect((String) arg);
44: } else if (arg instanceof URL) {
45: response.sendRedirect(String.valueOf(arg));
46: } else {
47: throw new IllegalArgumentException(String
48: .valueOf(arg));
49: }
50: } catch (IOException e) {
51: throw new PnutsException(e, context);
52: } catch (IllegalStateException e) {
53: PnutsException pe = new PnutsException(
54: "Headers have already been sent.", context);
55: pe.initCause(e);
56: throw pe;
57: }
58: return null;
59: } else {
60: undefined(args, context);
61: return null;
62: }
63: }
64:
65: public String toString() {
66: return "function sendRedirect(url)";
67: }
68: }
|