Re: Setting the statuscode for the response in a directAction
Re: Setting the statuscode for the response in a directAction
- Subject: Re: Setting the statuscode for the response in a directAction
- From: "Jonathan 'Wolf' Rentzsch" <email@hidden>
- Date: Sun, 19 Dec 2004 13:52:31 -0600
Kaj Hejer, email@hidden, wrote:
> public WOActionResults invalidPageAction() {
> WOComponent errorPage = pageWithName("MyInvalidPage");
> System.out.println("context().response(): " +
>context().response());
>
>//
>errorPage.context().response().setStatus(WOMessage.HTTP_STATUS_NOT_FOUND
>); // gives a NPE
> return errorPage;
> }
>
>The problem here is that context().response() and
>errorPage.context().response() is null, so I don't have a reponseobject
>to set the statuscode on.
This is what I use, which is slightly different than what you want, but
may help you anyway:
private class HTTPStatusResponse extends WOResponse {
public HTTPStatusResponse( int statusInt, String statusString ) {
super();
String contentString = "HTTP/1.0 "+statusInt+" "+statusString;
appendContentString( contentString );
setHeader( ""+contentString.length(), "content-length" );
setHeader( "text/html", "content-type" );
setStatus( statusInt );
}
}
private class UnauthorizedResponse extends HTTPStatusResponse {
public UnauthorizedResponse() {
super( 401, "Unauthorized" );
setHeader( "Basic realm=\""+WOApplication.application().name()+"\"",
"WWW-Authenticate" );
}
}
private class ForbiddenResponse extends HTTPStatusResponse {
public ForbiddenResponse() {
super( 403, "Forbidden" );
}
}
private class NotFoundResponse extends HTTPStatusResponse {
public NotFoundResponse() {
super( 404, "Not Found" );
}
}
private class MovedPermanentlyResponse extends HTTPStatusResponse {
public MovedPermanentlyResponse( String destination ) {
super( 301, "Moved Permanently" );
setHeader( destination, "location" );
}
}
Then your code becomes:
public WOActionResults invalidPageAction() {
return new NotFoundResponse();
}
Or if you wish to avoid unnecessary allocations:
private static NotFoundResponse notFoundResponse = new NotFoundResponse();
public WOActionResults invalidPageAction() {
return notFoundResponse;
}
| Jonathan 'Wolf' Rentzsch http://rentzsch.com
| Red Shed Software http://redshed.net
| "better" necessarily means "different"
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden