/**
* Copyright 2010 Philippe Beaudoin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.philbeaudoin.gwtpsample.server;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.philbeaudoin.gwtp.dispatch.server.actionHandler.ActionHandler;
import com.philbeaudoin.gwtp.dispatch.server.ExecutionContext;
import com.philbeaudoin.gwtp.dispatch.shared.ActionException;
import com.philbeaudoin.gwtpsample.shared.FieldVerifier;
import com.philbeaudoin.gwtpsample.shared.SendTextToServer;
import com.philbeaudoin.gwtpsample.shared.SendTextToServerResult;
public class SendTextToServerHandler implements ActionHandler<SendTextToServer, SendTextToServerResult> {
private ServletContext servletContext;
private Provider<HttpServletRequest> requestProvider;
@Inject
SendTextToServerHandler( ServletContext servletContext, Provider<HttpServletRequest> requestProvider ) {
this.servletContext = servletContext;
this.requestProvider = requestProvider;
}
@Override
public SendTextToServerResult execute(
SendTextToServer action,
ExecutionContext context) throws ActionException {
String input = action.getTextToServer();
// Verify that the input is valid.
if (!FieldVerifier.isValidName(input)) {
// If the input is not valid, throw an IllegalArgumentException back to
// the client.
throw new ActionException(
"Name must be at least 4 characters long");
}
String serverInfo = servletContext.getServerInfo();
String userAgent = requestProvider.get().getHeader("User-Agent");
return new SendTextToServerResult(
"Hello, " + input + "!<br><br>I am running " + serverInfo
+ ".<br><br>It looks like you are using:<br>" + userAgent );
}
@Override
public void undo(
SendTextToServer action,
SendTextToServerResult result,
ExecutionContext context) throws ActionException {
// Not undoable
}
@Override
public Class<SendTextToServer> getActionType() {
return SendTextToServer.class;
}
}