Skip to content

Instantly share code, notes, and snippets.

@wpivotto
Created October 25, 2011 14:50
Show Gist options
  • Save wpivotto/1312993 to your computer and use it in GitHub Desktop.
Save wpivotto/1312993 to your computer and use it in GitHub Desktop.
Tasks (Quartz Jobs) and Request Scope
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface PreventExternalAccess {
}
@Intercepts
@Lazy
public class RemoteAddressInterceptor implements Interceptor {
private final Result result;
private final HttpServletRequest request;
private static final Logger logger = LoggerFactory.getLogger(RemoteAddressInterceptor.class);
public RemoteAddressInterceptor(Result result, HttpServletRequest request) {
this.result = result;
this.request = request;
}
public boolean accepts(ResourceMethod method) {
return method.containsAnnotation(PreventExternalAccess.class);
}
public void intercept(InterceptorStack stack, ResourceMethod method, Object instance) throws InterceptionException {
logger.debug("Remote Host = {}", request.getRemoteHost());
if(request.getRemoteAddr().equals("127.0.0.1"))
stack.next(method, instance);
else
result.use(status()).notAcceptable();
}
}
import br.com.caelum.vraptor.controller.TasksController;
import br.com.caelum.vraptor.ioc.PrototypeScoped;
import br.com.caelum.vraptor.tasks.Task;
import br.com.caelum.vraptor.tasks.scheduler.Scheduled;
@PrototypeScoped
@Scheduled(fixedRate = 5000)
public class RequestScopeTask implements Task {
private final TaskRequest request;
public RequestScopeTask(TaskRequest request) {
this.request = request;
}
public void execute() {
request.access(TasksController.class).execute(getClass().getName());
}
}
package br.com.caelum.vraptor.jobs;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import javax.servlet.ServletContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.http.route.Router;
import br.com.caelum.vraptor.ioc.Component;
import br.com.caelum.vraptor.ioc.PrototypeScoped;
import br.com.caelum.vraptor.proxy.MethodInvocation;
import br.com.caelum.vraptor.proxy.Proxifier;
import br.com.caelum.vraptor.proxy.SuperMethod;
import br.com.caelum.vraptor.resource.HttpMethod;
@Component
@PrototypeScoped
public class TaskRequest {
private final Router router;
private final ServletContext context;
private final Proxifier proxifier;
private static final Logger logger = LoggerFactory.getLogger(TaskRequest.class);
public TaskRequest(Router router, ServletContext context, Proxifier proxifier) {
this.router = router;
this.context = context;
this.proxifier = proxifier;
}
public <T> T access(final Class<T> type) {
return proxifier.proxify(type, new MethodInvocation<T>() {
public Object intercept(T proxy, Method method, Object[] args, SuperMethod superMethod) {
if (!acceptsHttpGet(method)) {
throw new IllegalArgumentException("Your logic method must accept HTTP GET method if you want to access it");
}
String url = router.urlFor(type, method, args);
String path = context.getContextPath() + url;
logger.debug("trying to access http://localhost:8080{}", path);
try {
new URL("http://localhost:8080" + path).getContent();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
}
return null;
}
});
}
private boolean acceptsHttpGet(Method method) {
if (method.isAnnotationPresent(Get.class)) {
return true;
}
for (HttpMethod httpMethod : HttpMethod.values()) {
if (method.isAnnotationPresent(httpMethod.getAnnotation())) {
return false;
}
}
return true;
}
}
package br.com.caelum.vraptor.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Resource;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.view.Results;
@Resource
public class TasksController {
private final Result result;
private static final Logger logger = LoggerFactory.getLogger(TasksController.class);
public TasksController(Result result) {
this.result = result;
}
@Get("/task/{task}/execute")
@PreventExternalAccess
public void execute(String task){
logger.debug("Executing task {}", task);
result.nothing();
}
}
@lucascs
Copy link

lucascs commented Oct 25, 2011

The default value for the annotation could be "127.0.0.1"

@wpivotto
Copy link
Author

Thanks Lucas, updated...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment