Skip to content

Commit

Permalink
[#1743] Take original org.eclipse.jetty.servlet.DefaultServlet from j…
Browse files Browse the repository at this point in the history
…etty-servlet 10.0.11
  • Loading branch information
grgrzybek committed Sep 2, 2022
1 parent e2d2724 commit bafb02a
Showing 1 changed file with 43 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,42 +1,22 @@
/*
* Copyright 2020 OPS4J.
*
* 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.
*/

//
// ========================================================================
// Copyright (c) 1995-2020 Mort Bay Consulting Pty Ltd and others.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
// ========================================================================
// Copyright (c) 1995-2022 Mort Bay Consulting Pty Ltd and others.
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

package org.ops4j.pax.web.service.jetty.internal.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.StringTokenizer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
Expand All @@ -50,22 +30,18 @@
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.MimeTypes;
import org.eclipse.jetty.http.PreEncodedHttpField;
import org.eclipse.jetty.http.pathmap.MappedResource;
import org.eclipse.jetty.server.CachedContentFactory;
import org.eclipse.jetty.server.ResourceContentFactory;
import org.eclipse.jetty.server.ResourceService;
import org.eclipse.jetty.server.ResourceService.WelcomeFactory;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.servlet.ServletHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.resource.ResourceFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// CHECKSTYLE:OFF
/**
* The default servlet.
* <p>
Expand All @@ -86,7 +62,7 @@
* resources could be found. If false, then a welcome
* file must exist on disk. If "exact", then exact
* servlet matches are supported without an existing file.
* Default is true.
* Default is false.
*
* This must be false if you want directory listings,
* but have index.jsp in your welcome file list.
Expand Down Expand Up @@ -143,29 +119,27 @@
* encodingHeaderCacheSize
* Max entries in a cache of ACCEPT-ENCODING headers.
* </pre>
*
* <p>Pax Web 8: I had to copy this servlet from Jetty code to change some fields from private to protected.</p>
*/
public class DefaultServlet extends HttpServlet implements ResourceFactory, WelcomeFactory
{
public static final String CONTEXT_INIT = "org.eclipse.jetty.servlet.Default.";

private static final Logger LOG = Log.getLogger(DefaultServlet.class);
private static final Logger LOG = LoggerFactory.getLogger(DefaultServlet.class);

private static final long serialVersionUID = 4930458713846881193L;

protected final ResourceService _resourceService;
private final ResourceService _resourceService;
private ServletContext _servletContext;
private ContextHandler _contextHandler;

private boolean _welcomeServlets = false;
private boolean _welcomeExactServlets = false;

private Resource _resourceBase;
protected CachedContentFactory _cache;
private CachedContentFactory _cache;

private MimeTypes _mimeTypes;
protected String[] _welcomes;
private String[] _welcomes;
private Resource _stylesheet;
private boolean _useFileMappedBuffer = false;
private String _relativeResourceBase;
Expand Down Expand Up @@ -197,7 +171,7 @@ public void init()
_resourceService.setAcceptRanges(getInitBoolean("acceptRanges", _resourceService.isAcceptRanges()));
_resourceService.setDirAllowed(getInitBoolean("dirAllowed", _resourceService.isDirAllowed()));
_resourceService.setRedirectWelcome(getInitBoolean("redirectWelcome", _resourceService.isRedirectWelcome()));
_resourceService.setPrecompressedFormats(parsePrecompressedFormats(getInitParameter("precompressed"), getInitBoolean("gzip", false)));
_resourceService.setPrecompressedFormats(parsePrecompressedFormats(getInitParameter("precompressed"), getInitBoolean("gzip"), _resourceService.getPrecompressedFormats()));
_resourceService.setPathInfoOnly(getInitBoolean("pathInfoOnly", _resourceService.isPathInfoOnly()));
_resourceService.setEtags(getInitBoolean("etags", _resourceService.isEtags()));

Expand All @@ -224,7 +198,7 @@ public void init()
}
catch (Exception e)
{
LOG.warn(Log.EXCEPTION, e);
LOG.warn("Unable to create resourceBase from {}", rb, e);
throw new UnavailableException(e.toString());
}
}
Expand All @@ -237,7 +211,7 @@ public void init()
_stylesheet = Resource.newResource(css);
if (!_stylesheet.exists())
{
LOG.warn("!" + css);
LOG.warn("!{}", css);
_stylesheet = null;
}
}
Expand All @@ -248,8 +222,10 @@ public void init()
}
catch (Exception e)
{
LOG.warn(e.toString());
LOG.debug(e);
if (LOG.isDebugEnabled())
LOG.warn("Unable to use stylesheet: {}", css, e);
else
LOG.warn("Unable to use stylesheet: {} - {}", css, e.toString());
}

int encodingHeaderCacheSize = getInitInt("encodingHeaderCacheSize", -1);
Expand Down Expand Up @@ -289,7 +265,7 @@ public void init()
}
catch (Exception e)
{
LOG.warn(Log.EXCEPTION, e);
LOG.warn("Unable to setup CachedContentFactory", e);
throw new UnavailableException(e.toString());
}

Expand Down Expand Up @@ -325,11 +301,15 @@ public void init()
_servletHandler = _contextHandler.getChildHandlerByClass(ServletHandler.class);

if (LOG.isDebugEnabled())
LOG.debug("resource base = " + _resourceBase);
LOG.debug("resource base = {}", _resourceBase);
}

private CompressedContentFormat[] parsePrecompressedFormats(String precompressed, boolean gzip)
private CompressedContentFormat[] parsePrecompressedFormats(String precompressed, Boolean gzip, CompressedContentFormat[] dft)
{
if (precompressed == null && gzip == null)
{
return dft;
}
List<CompressedContentFormat> ret = new ArrayList<>();
if (precompressed != null && precompressed.indexOf('=') > 0)
{
Expand All @@ -339,7 +319,7 @@ private CompressedContentFormat[] parsePrecompressedFormats(String precompressed
String encoding = setting[0].trim();
String extension = setting[1].trim();
ret.add(new CompressedContentFormat(encoding, extension));
if (gzip && !ret.contains(CompressedContentFormat.GZIP))
if (gzip == Boolean.TRUE && !ret.contains(CompressedContentFormat.GZIP))
ret.add(CompressedContentFormat.GZIP);
}
}
Expand All @@ -351,7 +331,7 @@ else if (precompressed != null)
ret.add(CompressedContentFormat.GZIP);
}
}
else if (gzip)
else if (gzip == Boolean.TRUE)
{
// gzip handling is for backwards compatibility with older Jetty
ret.add(CompressedContentFormat.GZIP);
Expand Down Expand Up @@ -392,18 +372,23 @@ public String getInitParameter(String name)
return value;
}

private boolean getInitBoolean(String name, boolean dft)
private Boolean getInitBoolean(String name)
{
String value = getInitParameter(name);
if (value == null || value.length() == 0)
return dft;
return null;
return (value.startsWith("t") ||
value.startsWith("T") ||
value.startsWith("y") ||
value.startsWith("Y") ||
value.startsWith("1"));
}

private boolean getInitBoolean(String name, boolean dft)
{
return Optional.ofNullable(getInitBoolean(name)).orElse(dft);
}

private int getInitInt(String name, int dft)
{
String value = getInitParameter(name);
Expand Down Expand Up @@ -448,11 +433,11 @@ else if (_servletContext instanceof ContextHandler.Context)
}

if (LOG.isDebugEnabled())
LOG.debug("Resource " + pathInContext + "=" + r);
LOG.debug("Resource {}={}", pathInContext, r);
}
catch (IOException e)
{
LOG.ignore(e);
LOG.trace("IGNORED", e);
}

if ((r == null || !r.exists()) && pathInContext.endsWith("/jetty-dir.css"))
Expand Down Expand Up @@ -483,9 +468,6 @@ protected void doHead(HttpServletRequest request, HttpServletResponse response)
doGet(request, response);
}

/* (non-Javadoc)
* @see javax.servlet.http.HttpServlet#doTrace(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
@Override
protected void doTrace(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
Expand All @@ -500,9 +482,6 @@ protected void doOptions(HttpServletRequest request, HttpServletResponse respons
response.setHeader("Allow", "GET,HEAD,POST,OPTIONS");
}

/*
* @see javax.servlet.Servlet#destroy()
*/
@Override
public void destroy()
{
Expand All @@ -527,13 +506,12 @@ public String getWelcomeFile(String pathInContext)

if ((_welcomeServlets || _welcomeExactServlets) && welcomeServlet == null)
{
MappedResource<ServletHolder> entry = _servletHandler.getMappedServlet(welcomeInContext);
if (entry != null && entry.getResource().getServletInstance() != this &&
ServletHandler.MappedServlet entry = _servletHandler.getMappedServlet(welcomeInContext);
if (entry != null && entry.getServletHolder().getServletInstance() != this &&
(_welcomeServlets || (_welcomeExactServlets && entry.getPathSpec().getDeclaration().equals(welcomeInContext))))
welcomeServlet = welcomeInContext;
}
}
return welcomeServlet;
}
}
// CHECKSTYLE:ON

0 comments on commit bafb02a

Please sign in to comment.