net.craftforge.essential.controller.phases.AllocationPhase Maven / Gradle / Ivy
/*
* This file is part of essential.
*
* essential is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* essential 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with essential. If not, see .
*/
package net.craftforge.essential.controller.phases;
import net.craftforge.essential.controller.ControllerException;
import net.craftforge.essential.controller.ControllerPhase;
import net.craftforge.essential.controller.ControllerState;
import net.craftforge.essential.controller.managers.ResourceManager;
import net.craftforge.essential.controller.resolvers.MethodResolver;
import net.craftforge.essential.controller.resolvers.UrlResolver;
import net.craftforge.essential.core.constants.HttpStatusCode;
import javax.inject.Inject;
import java.lang.reflect.Method;
/**
* Allocates and sets the resource class and resource method.
*
* @author Christian Bick
* @since 17.01.2011
*/
public class AllocationPhase implements ControllerPhase {
private ResourceManager resourceManager;
private MethodResolver methodResolver;
private UrlResolver urlResolver;
@Inject
public AllocationPhase(ResourceManager resourceManager, MethodResolver methodResolver, UrlResolver urlResolver) {
this.resourceManager = resourceManager;
this.methodResolver = methodResolver;
this.urlResolver = urlResolver;
}
/**
* {@inheritDoc}
*/
public void run(ControllerState state) throws ControllerException {
if (state.getResourceMethod() == null) {
// Obtain HTTP method, resource URL and resource manager from setup
String httpMethod = methodResolver.getHttpMethod();
String resourcePath = urlResolver.getResourcePart();
// Allocate resource class
Class> resourceClass = resourceManager.getResourceClass(resourcePath);
// Resource class must be allocated
if (resourceClass == null) {
throw new ControllerException("'" + httpMethod + " " + resourcePath + "' has no responsible resource class", HttpStatusCode.NotFound);
}
// Allocate resource method
Method resourceMethod = resourceManager.getResourceMethod(httpMethod, resourcePath, resourceClass);
// Resource method must be allocated
if (resourceMethod == null) {
throw new ControllerException("'" + httpMethod + " " + resourcePath + "' has no responsible resource method", HttpStatusCode.NotFound);
}
state.setResourceClass(resourceClass);
state.setResourceMethod(resourceMethod);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy