001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019 */
020package org.sonar.server.plugins;
021
022import com.google.common.collect.Lists;
023import org.apache.commons.io.IOUtils;
024import org.apache.commons.lang.StringUtils;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027
028import java.io.IOException;
029import java.io.InputStream;
030import java.io.OutputStream;
031import java.io.PrintWriter;
032import java.util.List;
033import java.util.Set;
034
035import javax.servlet.ServletException;
036import javax.servlet.http.HttpServlet;
037import javax.servlet.http.HttpServletRequest;
038import javax.servlet.http.HttpServletResponse;
039
040/**
041 * This servlet allows to load libraries from directory "WEB-INF/lib" in order to provide them for batch-bootstrapper.
042 * Most probably this is not a best solution.
043 */
044public class BatchResourcesServlet extends HttpServlet {
045
046  private static final Logger LOG = LoggerFactory.getLogger(BatchResourcesServlet.class);
047  private static final long serialVersionUID = -2100128371794649028L;
048
049  @Override
050  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
051    String resource = getResource(request);
052    if (StringUtils.isEmpty(resource)) {
053      PrintWriter writer = null;
054      try {
055        response.setContentType("text/html");
056        writer = response.getWriter();
057        writer.print(StringUtils.join(getLibs(), ','));
058      } catch (IOException e) {
059        LOG.error("Unable to provide list of batch resources", e);
060        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
061      } finally {
062        IOUtils.closeQuietly(writer);
063      }
064    } else {
065      InputStream in = null;
066      OutputStream out = null;
067      try {
068        in = getServletContext().getResourceAsStream("/WEB-INF/lib/" + resource);
069        if (in == null) {
070          // TODO
071        } else {
072          out = response.getOutputStream();
073          IOUtils.copy(in, out);
074        }
075      } catch (Exception e) {
076        LOG.error("Unable to load batch resource '" + resource + "'", e);
077        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
078      } finally {
079        IOUtils.closeQuietly(in);
080        IOUtils.closeQuietly(out);
081      }
082    }
083  }
084
085  List<String> getLibs() {
086    List<String> libs = Lists.newArrayList();
087    Set paths = getServletContext().getResourcePaths("/WEB-INF/lib");
088    for (Object obj : paths) {
089      String path = (String) obj;
090      if (StringUtils.endsWith(path, ".jar")) {
091        String filename = StringUtils.removeStart(path, "/WEB-INF/lib/");
092        if (!isIgnored(filename)) {
093          libs.add(filename);
094        }
095      }
096    }
097    return libs;
098  }
099
100  private static final String[] IGNORE = { "derby", "jtds", "mysql", "postgresql", "jruby", "jfreechart", "eastwood", "jetty" };
101
102  /**
103   * Dirty hack to disable downloading for certain files.
104   */
105  static boolean isIgnored(String filename) {
106    for (String prefix : IGNORE) {
107      if (StringUtils.startsWith(filename, prefix)) {
108        return true;
109      }
110    }
111    return false;
112  }
113
114  /**
115   * @return part of request URL after servlet path
116   */
117  String getResource(HttpServletRequest request) {
118    return StringUtils.substringAfter(request.getRequestURI(), request.getContextPath() + request.getServletPath() + "/");
119  }
120
121}