raw.java revision 3c2aee4d6fd37062518aa6de5ad12d4967ebabc0
1057N/A/*
1057N/A * CDDL HEADER START
1057N/A *
1057N/A * The contents of this file are subject to the terms of the
1589N/A * Common Development and Distribution License (the "License").
1057N/A * You may not use this file except in compliance with the License.
660N/A *
1057N/A * See LICENSE.txt included in this distribution for the specific
1057N/A * language governing permissions and limitations under the License.
1057N/A *
1057N/A * When distributing Covered Code, include this CDDL HEADER in each
1057N/A * file and include the License file at LICENSE.txt.
1057N/A * If applicable, add the following below this CDDL HEADER, with the
1057N/A * fields enclosed by brackets "[]" replaced with your own identifying
1057N/A * information: Portions Copyright [yyyy] [name of copyright owner]
1057N/A *
1057N/A * CDDL HEADER END
1057N/A */
1057N/A
1057N/A/*
660N/A * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
3311N/A * Use is subject to license terms.
1109N/A */
2506N/A
3404N/A/*
3404N/A * ident "@(#)raw.java 1.3 06/02/22 SMI"
3404N/A */
2437N/A
2437N/Apackage org.opensolaris.opengrok.web;
2437N/A
3356N/Aimport java.io.*;
3356N/Aimport javax.servlet.*;
2506N/Aimport javax.servlet.http.*;
3404N/Aimport org.opensolaris.opengrok.history.HistoryGuru;
2737N/Aimport org.opensolaris.opengrok.analysis.*;
2737N/Aimport org.opensolaris.opengrok.index.IgnoredNames;
2557N/A/**
2557N/A * Gets different versions of a file
2557N/A *
3421N/A * @author Chandan
3404N/A */
2557N/Apublic class raw extends HttpServlet {
3421N/A protected long getLastModified(HttpServletRequest request) {
3404N/A String path = request.getPathInfo();
3404N/A if(path == null) {
3404N/A path = "";
3404N/A }
2557N/A String rawSource = getServletContext().getInitParameter("SRC_ROOT");
2557N/A String resourcePath = rawSource + path;
3421N/A File resourceFile = new File(resourcePath);
3404N/A resourcePath = resourceFile.getAbsolutePath();
2437N/A
3421N/A long ret;
3404N/A
3404N/A if (resourcePath.length() < rawSource.length()
3404N/A || !resourcePath.startsWith(rawSource)
3404N/A || !resourceFile.canRead()
2557N/A || IgnoredNames.ignore(resourceFile)
2557N/A || resourceFile.isDirectory()) {
2557N/A ret = 0;
3421N/A } else {
3404N/A ret = resourceFile.lastModified();
2557N/A }
3421N/A return ret;
3404N/A }
3404N/A
3404N/A public void doGet(HttpServletRequest request,
3404N/A HttpServletResponse response)
2437N/A throws IOException, ServletException {
3404N/A
3404N/A String context = request.getContextPath();
3404N/A String reqURI = request.getRequestURI();
3404N/A String path = request.getPathInfo();
3404N/A if(path == null) {
3404N/A path = "";
3404N/A }
3404N/A String rawSource = getServletContext().getInitParameter("SRC_ROOT");
3404N/A String resourcePath = rawSource + path;
3404N/A File resourceFile = new File(resourcePath);
3404N/A resourcePath = resourceFile.getAbsolutePath();
2437N/A String basename = resourceFile.getName();
2437N/A if (resourcePath.length() < rawSource.length()
2437N/A || !resourcePath.startsWith(rawSource)
2437N/A || !resourceFile.canRead()
2506N/A || IgnoredNames.ignore(basename)) {
2506N/A response.sendError(404);
3404N/A } else if (resourceFile.isDirectory()) {
2095N/A if(!reqURI.endsWith("/")) {
2095N/A response.sendRedirect(context + "/xref" + path + "/");
2095N/A } else {
3404N/A response.sendRedirect(context + "/xref" + path);
3404N/A }
3404N/A } else {
3404N/A InputStream in = null;
3404N/A String rev;
3404N/A if ((rev = request.getParameter("r")) != null && !rev.equals("")) {
3404N/A try{
3404N/A in = HistoryGuru.getInstance().getRevision(resourceFile.getParent(), basename, rev);
3404N/A } catch (Exception e) {
3404N/A response.sendError(404, "Revision not found");
3404N/A }
3404N/A } else {
3404N/A in = new BufferedInputStream(new FileInputStream(resourceFile));
3404N/A }
3404N/A if (in != null) {
3404N/A try{
2557N/A String contentType = null;
2557N/A if ((contentType = AnalyzerGuru.getContentType(in, path)) != null) {
2557N/A response.setContentType(contentType);
2557N/A } else if (getServletContext().getMimeType(basename) != null) {
2557N/A response.setContentType(getServletContext().getMimeType(basename));
2557N/A }
2557N/A int len = 0;
3404N/A byte[] buf = new byte[8192];
2095N/A OutputStream out = response.getOutputStream();
2095N/A while ((len = in.read(buf)) > 0) {
2095N/A out.write(buf, 0, len);
2095N/A }
2095N/A in.close();
2437N/A } catch (IOException e) {
2506N/A response.sendError(404, "Not found");
2506N/A }
2506N/A }
3404N/A }
3404N/A }
3404N/A}
3404N/A