/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2000 Blacksmith, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/
package net.wotonomy.web;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import jakarta.servlet.http.HttpSession;
import net.wotonomy.foundation.NSArray;
import net.wotonomy.foundation.NSSelector;
import net.wotonomy.foundation.internal.Introspector;
import net.wotonomy.foundation.internal.ValueConverter;
/**
* A pure java implementation of WODirectAction. This class provides a number of
* services to subclasses, including access to the query parameters, the
* request, the session, and logging and debugging facilities. A new instance of
* the class is created and then destroyed for every request-response cycle.
*
*
* Subclass this to implement direct actions for your application. Subclasses
* would typically override the constructor to initialize state based on the
* request, and then provide additional methods that would be invoked based on
* the value at the end of the URI.
*
*
* Example: "http://www/MyApp.woa/MyActions/search" would call the method named
* "searchAction" on the DirectAction subclass named "MyActions". If the
* subclass name is omitted, a subclass named "DirectAction" is assumed to exist
* within the application.
*
* @author michael@mpowers.net
* @author $Author: cgruber $
* @version $Revision: 905 $
*/
public class WODirectAction {
private WORequest request;
WOContext context;
/**
* Default constructor. This is called implicitly by subclasses in all cases.
* Package access only.
*/
WODirectAction() {
}
/**
* Request constructor. This is the constructor used to create an action in
* response to a user request. Override to perform any necessary initialization
* in your subclass.
*/
public WODirectAction(WORequest aRequest) {
this();
request = aRequest;
}
/**
* Returns the response from the component named "Main".
*/
public WOActionResults defaultAction() {
return pageWithName("Main").generateResponse();
}
/**
* Returns the WORequest object for the current request.
*/
public WORequest request() {
return request;
}
/**
* Returns the existing session, or null if no session exists.
*/
public WOSession existingSession() {
// TODO check this actually works
WOSessionStore store = WOSessionStore.serverSessionStore();
HttpSession session = request.servletRequest().getSession();
if (session == null)
return null;
WOSession wosession = store.restoreSessionWithID(session.getId(), request);
//WOSession wosession = new WOSession();
//wosession.setServletSession(session);
return wosession;
}
/**
* Returns the existing session if it exists. If no session exists, returns a
* newly created session. Note that if the user client does not support session
* ids/cookies, this will create a new session with each request.
*/
public WOSession session() {
// TODO check this actually works
WOSessionStore store = WOSessionStore.serverSessionStore();
HttpSession session = request.servletRequest().getSession();
if (session == null)
return null;
WOSession wosession = store.restoreSessionWithID(session.getId(), request);
if (wosession == null) {
wosession = new WOSession();
wosession.setServletSession(request.servletRequest().getSession(true));
}
return wosession;
}
/**
* Returns the named WOComponent.
*/
public WOComponent pageWithName(String aString) {
return request.application().pageWithName(aString, context);
}
/**
* Appends "Action" to the specified string and tries to invoke method with that
* name and no arguments. Returns null if the method does not exist. If anAction
* is null, "defaultAction" will be assumed.
*/
public WOActionResults performActionNamed(String anAction) {
if (anAction == null)
anAction = "default";
try {
NSSelector sel = new NSSelector(anAction + "Action");
return (WOActionResults) sel.invoke(this);
} catch (NoSuchMethodException exc) {
// returns below
} catch (InvocationTargetException exc) {
Throwable e = exc.getTargetException();
exc.printStackTrace();
throw new RuntimeException(e.toString());
} catch (Exception exc) {
exc.printStackTrace();
throw new RuntimeException(exc.toString());
}
WOResponse error = new WOResponse();
error.setStatus(404); // not found
error.appendContentString("Could not find method named \"" + anAction + "Action\".");
return error;
}
/**
* Assigns the arrays of form values for the specified keys to properties on
* this object with matching names whose type is NSArray or is convertable from
* a Collection.
*/
public void takeFormValueArraysForKeyArray(NSArray