class PluginMediaServlet extends BaseUploadServlet {

    @Override
    protected void doPost(HttpServletRequest req,
                          HttpServletResponse response) throws ServletException, IOException {

        try {
            final String filename = req.getRequestURI().substring(req.getContextPath().length());
            final String pluginName = filename.replace(pattern,
                                                       "/");

            if (pluginName != null) {
                final FileItem fileItem = getFileItem(req);
                final String fileName = FilenameUtils.getName(fileItem.getName());

                final Path path = resolve(pluginName + "/media/" + fileName);

                if (ioService.exists(path)) {
                    writeResponse(response,
                                  "FAIL - ALREADY EXISTS");
                    return;
                }

                try {
                    ioService.startBatch(path.getFileSystem());
                    writeFile(ioService,
                              path,
                              fileItem);
                } finally {
                    ioService.endBatch();
                }

                newMediaEvent.fire(new MediaAdded(pluginName.substring(1),
                                                  new Media(pattern.substring(1) + pluginName.substring(1) + "/media/" + path.getFileName(),
                                                            Paths.convert(path))));

                writeResponse(response,
                              "OK");
            }
        } catch (final Exception e) {
            logError(e);
            writeResponse(response,
                          "FAIL");
        }
    }

}