|
Jetty example source code file (j2mecdc.patch)
The Jetty j2mecdc.patch source code
Index: modules/jetty/src/main/java/org/mortbay/jetty/servlet/FileServlet.java
===================================================================
--- modules/jetty/src/main/java/org/mortbay/jetty/servlet/FileServlet.java (revision 0)
+++ modules/jetty/src/main/java/org/mortbay/jetty/servlet/FileServlet.java (revision 0)
@@ -0,0 +1,541 @@
+// ========================================================================
+// Copyright 199-2004 Mort Bay Consulting Pty. Ltd.
+// ------------------------------------------------------------------------
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// http://www.apache.org/licenses/LICENSE-2.0
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+// ========================================================================
+
+package org.mortbay.jetty.servlet;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.MalformedURLException;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+import javax.servlet.UnavailableException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.mortbay.io.Buffer;
+import org.mortbay.io.ByteArrayBuffer;
+import org.mortbay.io.IO;
+import org.mortbay.io.View;
+import org.mortbay.io.WriterOutputStream;
+import org.mortbay.jetty.Connector;
+import org.mortbay.jetty.HttpConnection;
+import org.mortbay.jetty.HttpContent;
+import org.mortbay.jetty.HttpFields;
+import org.mortbay.jetty.HttpHeaders;
+import org.mortbay.jetty.HttpMethods;
+import org.mortbay.jetty.MimeTypes;
+import org.mortbay.jetty.Response;
+import org.mortbay.log.Log;
+import org.mortbay.util.URIUtil;
+
+
+
+/* ------------------------------------------------------------ */
+/** The default servlet.
+ * This servlet, normally mapped to /, provides the handling for static
+ * content, OPTION and TRACE methods for the context.
+ * The following initParameters are supported:
+ * <PRE>
+ * dirAllowed If true, directory listings are returned if no
+ * welcome file is found. Else 403 Forbidden.
+ *
+ * redirectWelcome If true, welcome files are redirected rather than
+ * forwarded to.
+ *
+ * fileBase Set to the source of the files
+ *
+ * </PRE>
+ *
+ *
+ * @author Greg Wilkins (gregw)
+ * @author Nigel Canonizado
+ */
+public class FileServlet extends HttpServlet
+{
+ private boolean _dirAllowed=true;
+ private boolean _redirectWelcome=true;
+
+ private MimeTypes _mimeTypes;
+ private String[] _welcomes;
+ private File docroot;
+
+
+ /* ------------------------------------------------------------ */
+ public void init()
+ throws UnavailableException
+ {
+ _mimeTypes = new MimeTypes();
+ _welcomes=new String[] {"index.jsp","index.html"};
+
+ _dirAllowed=getInitBoolean("dirAllowed",_dirAllowed);
+ _redirectWelcome=getInitBoolean("redirectWelcome",_redirectWelcome);
+
+
+ String fb=getInitParameter("fileBase");
+ docroot = new File(fb==null?".":fb);
+
+ }
+
+ /* ------------------------------------------------------------ */
+ private boolean getInitBoolean(String name, boolean dft)
+ {
+ String value=getInitParameter(name);
+ if (value==null || value.length()==0)
+ return dft;
+ return (value.startsWith("t")||
+ value.startsWith("T")||
+ value.startsWith("y")||
+ value.startsWith("Y")||
+ value.startsWith("1"));
+ }
+
+
+ /* ------------------------------------------------------------ */
+ /** get Resource to serve.
+ * Map a path to a resource. The default implementation calls
+ * HttpContext.getResource but derived servlets may provide
+ * their own mapping.
+ * @param pathInContext The path to find a resource for.
+ * @return The resource to serve.
+ */
+ public File getResource(String pathInContext)
+ {
+ return new File(docroot,pathInContext);
+ }
+
+ /* ------------------------------------------------------------ */
+ protected void doGet(HttpServletRequest request, HttpServletResponse response)
+ throws ServletException, IOException
+ {
+ String servletPath=null;
+ String pathInfo=null;
+ Boolean included =(Boolean)request.getAttribute(Dispatcher.__INCLUDE_JETTY);
+ if (included!=null && included.booleanValue())
+ {
+ servletPath=(String)request.getAttribute(Dispatcher.__INCLUDE_SERVLET_PATH);
+ pathInfo=(String)request.getAttribute(Dispatcher.__INCLUDE_PATH_INFO);
+ if (servletPath==null)
+ {
+ servletPath=request.getServletPath();
+ pathInfo=request.getPathInfo();
+ }
+ }
+ else
+ {
+ included=Boolean.FALSE;
+ servletPath=request.getServletPath();
+ pathInfo=request.getPathInfo();
+
+ }
+
+ String pathInContext=URIUtil.addPaths(servletPath,pathInfo);
+ boolean endsWithSlash=pathInContext.endsWith("/");
+
+ // Find the resource and content
+ String path=null;
+ File resource=null;
+ Content content=null;
+ try
+ {
+ path=pathInContext;
+ resource=getResource(pathInContext);
+
+
+ // Handle resource
+ if (resource==null || !resource.exists())
+ response.sendError(HttpServletResponse.SC_NOT_FOUND);
+ else if (!resource.isDirectory())
+ {
+ // ensure we have content
+ if (content==null)
+ {
+ content=getContent(path,resource);
+ }
+
+ if (included.booleanValue() || passConditionalHeaders(request,response, resource,content))
+ sendData(request,response,included.booleanValue(),resource,content);
+ }
+ else
+ {
+ String welcome=null;
+
+ if (!endsWithSlash && !pathInContext.equals("/"))
+ {
+ StringBuffer buf=request.getRequestURL();
+ buf.append('/');
+ String q=request.getQueryString();
+ if (q!=null&&q.length()!=0)
+ {
+ buf.append('?');
+ buf.append(q);
+ }
+ response.setContentLength(0);
+ response.sendRedirect(response.encodeRedirectURL(buf.toString()));
+ }
+ // else look for a welcome file
+ else if (null!=(welcome=getWelcomeFile(resource)))
+ {
+ String ipath=URIUtil.addPaths(pathInContext,welcome);
+ if (_redirectWelcome)
+ {
+ // Redirect to the index
+ response.setContentLength(0);
+ String q=request.getQueryString();
+ if (q!=null&&q.length()!=0)
+ response.sendRedirect(URIUtil.addPaths( request.getContextPath(),ipath)+"?"+q);
+ else
+ response.sendRedirect(URIUtil.addPaths( request.getContextPath(),ipath));
+ }
+ else
+ {
+ // Forward to the index
+ RequestDispatcher dispatcher=request.getRequestDispatcher(ipath);
+ if (dispatcher!=null)
+ {
+ if (included.booleanValue())
+ dispatcher.include(request,response);
+ else
+ dispatcher.forward(request,response);
+ }
+ }
+ }
+ else
+ {
+ content=getContent(pathInContext,resource);
+ if (included.booleanValue() || passConditionalHeaders(request,response, resource,content))
+ sendDirectory(request,response,resource,pathInContext.length()>1);
+ }
+ }
+ }
+ catch(IllegalArgumentException e)
+ {
+ Log.warn(Log.EXCEPTION,e);
+ }
+ finally
+ {
+ }
+
+ }
+
+ /* ------------------------------------------------------------ */
+ protected void doPost(HttpServletRequest request, HttpServletResponse response)
+ throws ServletException, IOException
+ {
+ doGet(request,response);
+ }
+
+ /* ------------------------------------------------------------ */
+ private Content getContent(String pathInContext, File resource)
+ throws IOException
+ {
+ Content content=new Content(resource);
+ Buffer mime_type=_mimeTypes.getMimeByExtension(pathInContext);
+ Connector connector = HttpConnection.getCurrentConnection().getConnector();
+ if (mime_type!=null) content.setContentType(mime_type);
+
+ if (!resource.isDirectory())
+ {
+ Buffer buffer=null;
+ long length=resource.length();
+
+ buffer = new ByteArrayBuffer((int)length);
+ byte[] array = buffer.array();
+ InputStream in = new FileInputStream(resource);
+
+ int l = 0;
+ while (l < length)
+ {
+ int r = in.read(array,l,array.length-l);
+ if (r < 0)
+ throw new IOException("unexpected EOF");
+ l+=r;
+ }
+ buffer.setPutIndex(l);
+
+ if (buffer!=null)
+ {
+ content.setBuffer(buffer);
+ if (Log.isDebugEnabled())
+ Log.debug("content buffer is "+buffer.getClass());
+ }
+ }
+ return content;
+ }
+
+ /* ------------------------------------------------------------ */
+ /**
+ * Finds a matching welcome file for the supplied {@link Resource}. This will be the first entry in the list of
+ * configured {@link #_welcomes welcome files} that existing within the directory referenced by the <code>Resource.
+ * If the resource is not a directory, or no matching file is found, then <code>null is returned.
+ * The list of welcome files is read from the {@link ContextHandler} for this servlet, or
+ * <code>"index.jsp" , "index.html" if that is |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.