Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.yammer.dropwizard.tasks;
 
 
A servlet which provides access to administrative Tasks. It only responds to POST requests, since most Tasks aren't side-effect free, and passes along the query string parameters of the request to the task as a multimap.

See also:
Task
public class TaskServlet extends HttpServlet {
    private static final long serialVersionUID = 7404713218661358124L;
    private static final Log LOG = Log.forClass(TaskServlet.class);
    private final ImmutableMap<StringTasktasks;

    
Creates a new TaskServlet given the provided set of Task instances.

Parameters:
tasks a series of tasks which the servlet will provide access to
    public TaskServlet(Iterable<Tasktasks) {
        final ImmutableMap.Builder<StringTaskbuilder = ImmutableMap.builder();
        for (Task task : tasks) {
            builder.put("/tasks/" + task.getName(), task);
        }
        this. = builder.build();
    }
    @Override
    protected void doPost(HttpServletRequest req,
                          HttpServletResponse respthrows ServletExceptionIOException {
        final Task task = .get(req.getRequestURI());
        if (task != null) {
            try {
                resp.setContentType(.);
                final PrintWriter output = resp.getWriter();
                try {
                    task.execute(getParams(req), output);
                } finally {
                    output.close();
                }
            } catch (Exception e) {
                .error(e"Error running {}"task.getName());
                resp.sendError(.);
            }
        } else {
            resp.sendError(.);
        }
    }
    private static ImmutableMultimap<StringStringgetParams(HttpServletRequest req) {
        final ImmutableMultimap.Builder<StringStringresults = ImmutableMultimap.builder();
        final Enumeration<?> names = req.getParameterNames();
        while (names.hasMoreElements()) {
            final String name = (Stringnames.nextElement();
            final String[] values = req.getParameterValues(name);
            results.putAll(namevalues);
        }
        return results.build();
    }
New to GrepCode? Check out our FAQ X