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 Logger LOGGER = LoggerFactory.getLogger(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('/' + task.getName(), task);
        }
        this. = builder.build();
    }
    @Override
    protected void doPost(HttpServletRequest req,
                          HttpServletResponse respthrows ServletExceptionIOException {
        final Task task = .get(req.getPathInfo());
        if (task != null) {
            resp.setContentType(.);
            final PrintWriter output = resp.getWriter();
            try {
                task.execute(getParams(req), output);
            } catch (Exception e) {
                .error("Error running {}"task.getName(), e);
                resp.setStatus(.);
                output.println();
                output.println(e.getMessage());
                e.printStackTrace(output);
            } finally {
                output.close();
            }
        } else {
            resp.sendError(.);
        }
    }
    private static ImmutableMultimap<StringStringgetParams(HttpServletRequest req) {
        final ImmutableMultimap.Builder<StringStringresults = ImmutableMultimap.builder();
        final Enumeration<Stringnames = req.getParameterNames();
        while (names.hasMoreElements()) {
            final String name = names.nextElement();
            final String[] values = req.getParameterValues(name);
            results.putAll(namevalues);
        }
        return results.build();
    }
New to GrepCode? Check out our FAQ X