Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2014 Philip Helger ( philip[at]helger[dot]com 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 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.helger.webbasics.action.servlet;
Abstract action handling servlet

Philip Helger
 public abstract class AbstractActionServlet extends AbstractUnifiedResponseServlet
   private static final Logger s_aLogger = LoggerFactory.getLogger (AbstractActionServlet.class);
   private static final IStatisticsHandlerKeyedTimer s_aStatsTimer = StatisticsManager.getKeyedTimerHandler (AbstractActionServlet.class);
   private static final IStatisticsHandlerKeyedCounter s_aStatsCounterSuccess = StatisticsManager.getKeyedCounterHandler (AbstractActionServlet.class +
   private static final IStatisticsHandlerKeyedCounter s_aStatsCounterError = StatisticsManager.getKeyedCounterHandler (AbstractActionServlet.class +
   private static final String SCOPE_ATTR_NAME = "$";
   private static final String SCOPE_ATTR_INVOKER = "$ph-actionservlet.invoker";
   private static final String SCOPE_ATTR_EXECUTOR = "$ph-actionservlet.executor";
   public AbstractActionServlet ()


The current custom exception handler or null if none is set.
   @ReturnsMutableObject (reason = "design")
     return ;

Check if it is valid in the current scope to invoke the passed Action. This method is called after initRequestState was invoked.

sActionName The Action that was desired to be invoked.
aRequestScope The current request scope. Never null.
true if the Action may be invoked.
   protected boolean isValidToInvokeAction (@Nonnull final String sActionName,
                                            @Nonnull final IRequestWebScopeWithoutResponse aRequestScope)
     return true;

Get the action invoker matching the passed request

aRequestScope The request scope to use. May not be null.
Never null.
  protected abstract IActionInvoker getActionInvoker (@Nonnull IRequestWebScopeWithoutResponse aRequestScope);
  protected EContinue initRequestState (@Nonnull final IRequestWebScopeWithoutResponse aRequestScope,
                                        @Nonnull final UnifiedResponse aUnifiedResponse)
    // cut the leading "/"
    String sActionName = RequestHelper.getPathWithinServlet (aRequestScope.getRequest ());
    if (StringHelper.startsWith (sActionName'/'))
      sActionName = sActionName.substring (1);
    final IActionInvoker aActionInvoker = getActionInvoker (aRequestScope);
    final IActionExecutor aActionExecutor = aActionInvoker.createExecutor (sActionName);
    if (aActionExecutor == null)
      .warn ("Unknown action '" + sActionName + "' provided!");
      // No such action
      aUnifiedResponse.setStatus (.);
      return .;
    // Call the initialization of the action executor
    aActionExecutor.initExecution (aRequestScope);
    // Remember in scope
    // Important: use a wrapper to avoid scope destruction
    aRequestScope.setAttribute (sActionName);
    aRequestScope.setAttribute (, Wrapper.create (aActionInvoker));
    aRequestScope.setAttribute (aActionExecutor);
    return .;
  protected void handleRequest (@Nonnull final IRequestWebScopeWithoutResponse aRequestScope,
                                @Nonnull final UnifiedResponse aUnifiedResponsethrows ServletExceptionIOException
    // Action is present
    final String sActionName = aRequestScope.getAttributeAsString ();
    final IActionInvoker aActionInvoker = (IActionInvokeraRequestScope.getTypedAttribute (,
                                                                                            Wrapper.class).get ();
    final IActionExecutor aActionExecutor = aRequestScope.getTypedAttribute (IActionExecutor.class);
    // For actions caching is not an option, because it is dynamic content
    aUnifiedResponse.disableCaching ();
    if (!isValidToInvokeAction (sActionNameaRequestScope))
      .warn ("Invoking the Action '" + sActionName + "' is not valid in this context!");
      aUnifiedResponse.setStatus (.);
        // Start the timing
        final StopWatch aSW = new StopWatch (true);
        // Handle the main action
        aActionInvoker.invokeAction (sActionNameaActionExecutoraRequestScopeaUnifiedResponse);
        // Remember the time
        .addTime (sActionNameaSW.stopAndGetMillis ());
        .increment (sActionName);
      catch (final Throwable t)
        .increment (sActionName);
        // Notify custom exception handler
        for (final IActionExceptionCallback aExceptionCallback : getExceptionCallbacks ().getAllCallbacks ())
            aExceptionCallback.onActionExecutionException (aActionInvoker,
          catch (final Throwable t2)
            .error ("Exception in custom Action exception handler of function '" + sActionName + "'"t2);
        // Re-throw
        if (t instanceof IOException)
          throw (IOExceptiont;
        if (t instanceof ServletException)
          throw (ServletExceptiont;
        throw new ServletException ("Error invoking Action '" + sActionName + "'"t);
New to GrepCode? Check out our FAQ X