Top Description Inners Fields Constructors Methods
org.eclipse.jetty.server.handler

public Class ResourceHandler

extends Wrapper
Class Inheritance
Imports
java.time.Duration, java.util.List, org.eclipse.jetty.http.CompressedContentFormat, .HttpMethod, .HttpURI, .MimeTypes, org.eclipse.jetty.http.content.FileMappingHttpContentFactory, .HttpContent, .PreCompressedHttpContentFactory, .ResourceHttpContentFactory, .ValidatingCachingHttpContentFactory, .VirtualHttpContentFactory, org.eclipse.jetty.io.ByteBufferPool, org.eclipse.jetty.server.Context, .Handler, .Request, .ResourceService, .Response, .Server, org.eclipse.jetty.util.Callback, .URIUtil, org.eclipse.jetty.util.resource.Resource, .ResourceFactory, .Resources

Resource Handler. This handle will serve static content and handle If-Modified-Since headers. No caching is done. Requests for resources that do not exist are let pass (Eg no 404's). TODO there is a lot of URI manipulation, this should be factored out in a utility class. TODO GW: Work out how this logic can be reused by the DefaultServlet... potentially for wrapped output streams Missing: - current context' mime types - Default stylesheet (needs Resource impl for classpath resources) - request ranges - a way to configure caching or not

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
public static class
ResourceHandler.ResourceContext

Utility class to create a ContextHandler containing a ResourceHandler.

Field Summary

Modifier and TypeField and Description
private Resource
private ByteBufferPool
private MimeTypes
private final ResourceService
private Resource
private boolean
private List<String>

Constructor Summary

AccessConstructor and Description
public
public

Method Summary

Modifier and TypeMethod and Description
public void
doStart()

Overrides org.eclipse.jetty.server.Handler.Abstract.doStart.

Starts the managed lifecycle beans in the order they were added.

public Resource
getBaseResource()

Returns: Returns the resourceBase.

private ByteBufferPool
public ByteBufferPool
public String

Returns:

the cacheControl header to set on all static content.
getCacheControl
()

Get the cacheControl header to set on all static content..

public int
public List<String>
getGzipEquivalentFileExtensions()

Returns: file extensions that signify that a file is gzip compressed.

public HttpContent.Factory
public MimeTypes
public List<CompressedContentFormat>
getPrecompressedFormats()

Returns: Precompressed resources formats that can be used to serve compressed variant of resources.

public ResourceService
public Resource
getStyleSheet()

Returns: Returns the stylesheet as a Resource.

public List<String>
public ResourceService.WelcomeMode
public boolean
handle(Request
the HTTP request to handle
request
,
Response
the HTTP response to handle
response
,
Callback
the callback to complete when the handling is complete
callback
)

Overrides org.eclipse.jetty.server.Handler.Wrapper.handle.

Implements org.eclipse.jetty.server.Request.Handler.handle.

Invoked to handle the passed HTTP request and response.

public boolean
isAcceptRanges()

Returns: If true, range requests and responses are supported

public boolean
isDirAllowed()

Returns: If true, directory listings are returned if no welcome file is found.

public boolean
isEtags()

Returns: True if ETag processing is done

public boolean
protected HttpContent.Factory
protected ResourceService
public void
setAcceptRanges(boolean
If true, range requests and responses are supported
acceptRanges
)

public void
setBaseResource(Resource
The resourceBase to server content from. If null the context resource base is used.
base
)

public void
setBaseResourceAsString(String
The resourceBase to server content from. If null the context resource base is used. If non-null the Resource is created from ResourceFactory#of(org.eclipse.jetty.util.component.Container) for this context.
base
)

public void
setCacheControl(String
the cacheControl header to set on all static content.
cacheControl
)

Set the cacheControl header to set on all static content..

public void
setDirAllowed(boolean
If true, directory listings are returned if no welcome file is found. Else 403 Forbidden.
dirAllowed
)

public void
setEncodingCacheSize(int encodingCacheSize)

public void
setEtags(boolean
True if ETag processing is done
etags
)

public void
setGzipEquivalentFileExtensions(List<String>
file extensions that signify that a file is gzip compressed. Eg ".svgz"
gzipEquivalentFileExtensions
)

Set file extensions that signify that a file is gzip compressed.

public void
public void
setPrecompressedFormats(CompressedContentFormat...
The list of precompresed formats to serve in encoded format if matching resource found. For example serve gzip encoded file if ".gz" suffixed resource is found.
precompressedFormats
)

public void
setPrecompressedFormats(List<CompressedContentFormat>
The list of precompresed formats to serve in encoded format if matching resource found. For example serve gzip encoded file if ".gz" suffixed resource is found.
precompressedFormats
)

public void
setStyleSheet(Resource
The location of the style sheet to be used as a String.
styleSheet
)

protected ResourceService.WelcomeFactory
public void
setUseFileMapping(boolean useFileMapping)

public void
setWelcomeFiles(String... welcomeFiles)

public void
setWelcomeFiles(List<String> welcomeFiles)

public void
Inherited from org.eclipse.jetty.server.Handler.Wrapper:
getHandlergetInvocationTypesetHandler

Field Detail

_baseResourceback to summary
private Resource _baseResource
_byteBufferPoolback to summary
private ByteBufferPool _byteBufferPool
_mimeTypesback to summary
private MimeTypes _mimeTypes
_resourceServiceback to summary
private final ResourceService _resourceService
_styleSheetback to summary
private Resource _styleSheet
_useFileMappingback to summary
private boolean _useFileMapping
_welcomesback to summary
private List<String> _welcomes

Constructor Detail

ResourceHandlerback to summary
public ResourceHandler()
ResourceHandlerback to summary
public ResourceHandler(Handler handler)

Method Detail

doStartback to summary
public void doStart() throws Exception

Overrides org.eclipse.jetty.server.Handler.Abstract.doStart.

Doc from org.eclipse.jetty.util.component.ContainerLifeCycle.doStart.

Starts the managed lifecycle beans in the order they were added.

Annotations
@Override
Exceptions
Exception:

Doc from org.eclipse.jetty.util.component.AbstractLifeCycle.doStart.

If there was a problem starting. Will cause a transition to FAILED state

getBaseResourceback to summary
public Resource getBaseResource()
Returns:Resource

Returns the resourceBase.

getByteBufferPoolback to summary
private ByteBufferPool getByteBufferPool(Context context)
getByteBufferPoolback to summary
public ByteBufferPool getByteBufferPool()
getCacheControlback to summary
public String getCacheControl()

Get the cacheControl header to set on all static content..

Returns:String

the cacheControl header to set on all static content.

getEncodingCacheSizeback to summary
public int getEncodingCacheSize()
getGzipEquivalentFileExtensionsback to summary
public List<String> getGzipEquivalentFileExtensions()
Returns:List<String>

file extensions that signify that a file is gzip compressed. Eg ".svgz"

getHttpContentFactoryback to summary
public HttpContent.Factory getHttpContentFactory()
getMimeTypesback to summary
public MimeTypes getMimeTypes()
getPrecompressedFormatsback to summary
public List<CompressedContentFormat> getPrecompressedFormats()
Returns:List<CompressedContentFormat>

Precompressed resources formats that can be used to serve compressed variant of resources.

getResourceServiceback to summary
public ResourceService getResourceService()
getStyleSheetback to summary
public Resource getStyleSheet()
Returns:Resource

Returns the stylesheet as a Resource.

getWelcomeFilesback to summary
public List<String> getWelcomeFiles()
getWelcomeModeback to summary
public ResourceService.WelcomeMode getWelcomeMode()
handleback to summary
public boolean handle(Request request, Response response, Callback callback) throws Exception

Overrides org.eclipse.jetty.server.Handler.Wrapper.handle.

Implements org.eclipse.jetty.server.Request.Handler.handle.

Doc from org.eclipse.jetty.server.Request.Handler.handle.

Invoked to handle the passed HTTP request and response.

The request is accepted by returning true, then handling must be concluded by completing the passed callback. The handling may be asynchronous, i.e. this method may return true and complete the given callback later, possibly from a different thread. If this method returns false, then the callback must not be invoked and any mutation on the response reversed.

Exceptions thrown by this method may be subsequently handled by an error Request.Handler, if present, otherwise a default HTTP 500 error is generated and the callback completed while writing the error response.

The simplest implementation is:

public boolean handle(Request request, Response response, Callback callback)
{
    callback.succeeded();
    return true;
}

A HelloWorld implementation is:

public boolean handle(Request request, Response response, Callback callback)
{
    response.write(true, ByteBuffer.wrap("Hello World\n".getBytes(StandardCharsets.UTF_8)), callback);
    return true;
}
Parameters
request:Request

the HTTP request to handle

response:Response

the HTTP response to handle

callback:Callback

the callback to complete when the handling is complete

Returns:boolean

True if and only if the request will be handled, a response generated and the callback eventually called. This may occur within the scope of the call to this method, or asynchronously some time later. If false is returned, then this method must not generate a response, nor complete the callback.

Annotations
@Override
Exceptions
Exception:
if there is a failure during the handling. Catchers cannot assume that the callback will be called and thus should attempt to complete the request as if a false had been returned.
isAcceptRangesback to summary
public boolean isAcceptRanges()
Returns:boolean

If true, range requests and responses are supported

isDirAllowedback to summary
public boolean isDirAllowed()
Returns:boolean

If true, directory listings are returned if no welcome file is found. Else 403 Forbidden.

isEtagsback to summary
public boolean isEtags()
Returns:boolean

True if ETag processing is done

isUseFileMappingback to summary
public boolean isUseFileMapping()
newHttpContentFactoryback to summary
protected HttpContent.Factory newHttpContentFactory()
newResourceServiceback to summary
protected ResourceService newResourceService()
setAcceptRangesback to summary
public void setAcceptRanges(boolean acceptRanges)
Parameters
acceptRanges:boolean

If true, range requests and responses are supported

setBaseResourceback to summary
public void setBaseResource(Resource base)
Parameters
base:Resource

The resourceBase to server content from. If null the context resource base is used.

setBaseResourceAsStringback to summary
public void setBaseResourceAsString(String base)
Parameters
base:String

The resourceBase to server content from. If null the context resource base is used. If non-null the Resource is created from ResourceFactory#of(org.eclipse.jetty.util.component.Container) for this context.

setCacheControlback to summary
public void setCacheControl(String cacheControl)

Set the cacheControl header to set on all static content..

Parameters
cacheControl:String

the cacheControl header to set on all static content.

setDirAllowedback to summary
public void setDirAllowed(boolean dirAllowed)
Parameters
dirAllowed:boolean

If true, directory listings are returned if no welcome file is found. Else 403 Forbidden.

setEncodingCacheSizeback to summary
public void setEncodingCacheSize(int encodingCacheSize)
setEtagsback to summary
public void setEtags(boolean etags)
Parameters
etags:boolean

True if ETag processing is done

setGzipEquivalentFileExtensionsback to summary
public void setGzipEquivalentFileExtensions(List<String> gzipEquivalentFileExtensions)

Set file extensions that signify that a file is gzip compressed. Eg ".svgz".

Parameters
gzipEquivalentFileExtensions:List<String>

file extensions that signify that a file is gzip compressed. Eg ".svgz"

setMimeTypesback to summary
public void setMimeTypes(MimeTypes mimeTypes)
setPrecompressedFormatsback to summary
public void setPrecompressedFormats(CompressedContentFormat... precompressedFormats)
Parameters
precompressedFormats:CompressedContentFormat[]

The list of precompresed formats to serve in encoded format if matching resource found. For example serve gzip encoded file if ".gz" suffixed resource is found.

setPrecompressedFormatsback to summary
public void setPrecompressedFormats(List<CompressedContentFormat> precompressedFormats)
Parameters
precompressedFormats:List<CompressedContentFormat>

The list of precompresed formats to serve in encoded format if matching resource found. For example serve gzip encoded file if ".gz" suffixed resource is found.

setStyleSheetback to summary
public void setStyleSheet(Resource styleSheet)
Parameters
styleSheet:Resource

The location of the style sheet to be used as a String.

setupWelcomeFactoryback to summary
protected ResourceService.WelcomeFactory setupWelcomeFactory()
setUseFileMappingback to summary
public void setUseFileMapping(boolean useFileMapping)
setWelcomeFilesback to summary
public void setWelcomeFiles(String... welcomeFiles)
setWelcomeFilesback to summary
public void setWelcomeFiles(List<String> welcomeFiles)
setWelcomeModeback to summary
public void setWelcomeMode(ResourceService.WelcomeMode welcomeMode)
org.eclipse.jetty.server.handler back to summary

private Class ResourceHandler.HandlerResourceService

extends ResourceService
Class Inheritance

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
protected void
rehandleWelcome(Request
the request
request
,
Response
the response
response
,
Callback
the callback to complete
callback
,
String
the welcome target to rehandle to
welcomeTarget
)

Overrides org.eclipse.jetty.server.ResourceService.rehandleWelcome.

Rehandles the given welcome target.

Inherited from org.eclipse.jetty.server.ResourceService:
doGetgetCacheControlgetContentgetEncodingCacheSizegetGzipEquivalentFileExtensionsgetHttpContentFactorygetPrecompressedFormatsgetWelcomeFactorygetWelcomeModehandleWelcomeActionisAcceptRangesisDirAllowedisEtagspassConditionalHeadersputHeadersredirectWelcomesendRedirectsendWelcomeserveWelcomesetAcceptRangessetCacheControlsetDirAllowedsetEncodingCacheSizesetEtagssetGzipEquivalentFileExtensionssetHttpContentFactorysetPrecompressedFormatssetWelcomeFactorysetWelcomeModetoStringwriteHttpContentwriteHttpErrorwriteHttpErrorwriteHttpError

Constructor Detail

HandlerResourceServiceback to summary
private HandlerResourceService()

Method Detail

rehandleWelcomeback to summary
protected void rehandleWelcome(Request request, Response response, Callback callback, String welcomeTarget) throws Exception

Overrides org.eclipse.jetty.server.ResourceService.rehandleWelcome.

Doc from org.eclipse.jetty.server.ResourceService.rehandleWelcome.

Rehandles the given welcome target.

Implementations should call Handler#handle(Request, Response, Callback) on a Handler that may handle the welcome target differently from the original request.

For example, a request for /ctx/ may be rewritten as /ctx/index.jsp and rehandled from the Server. In this example, the rehandling of /ctx/index.jsp may trigger a different code path so that the rewritten request is handled by a different Handler, in this example one that knows how to handle JSP resources.

Parameters
request:Request

the request

response:Response

the response

callback:Callback

the callback to complete

welcomeTarget:String

the welcome target to rehandle to

Annotations
@Override
Exceptions
Exception:
if the rehandling fails
org.eclipse.jetty.server.handler back to summary

public Class ResourceHandler.ResourceContext

extends ContextHandler
Class Inheritance

Utility class to create a ContextHandler containing a ResourceHandler.

Field Summary

Inherited from org.eclipse.jetty.server.handler.ContextHandler:
MANAGED_ATTRIBUTES

Constructor Summary

AccessConstructor and Description
public

Method Summary

Inherited from org.eclipse.jetty.server.handler.ContextHandler:
addAliasCheckaddEventListeneraddVirtualHostscheckAliascheckVirtualHostclearAliasChecksclearAttributescreateTempDirectorydestroydoStartdoStopdumpenterScopeexitScopegetAliasChecksgetAllowNullPathInContextgetAttributegetAttributeNameSetgetBaseResourcegetClassLoadergetClassPathgetContextgetContextHandlergetContextPathgetCurrentContextgetCurrentContextgetCurrentContextHandlergetDisplayNamegetErrorHandlergetMimeTypesgetProtectedTargetsgetTempDirectorygetVirtualHostshandlehandleByContextHandlerhandleMovedPermanentlyhandleUnavailableisAvailableisProtectedTargetisTempDirectoryPersistentnewContextnotifyEnterScopenotifyExitScoperemoveAttributeremoveEventListenerremoveVirtualHostssetAliasCheckssetAllowNullPathInContextsetAttributesetAvailablesetBaseResourcesetBaseResourceAsPathsetBaseResourceAsStringsetClassLoadersetContextPathsetDisplayNamesetErrorHandlersetProtectedTargetssetServersetTempDirectorysetTempDirectoryPersistentsetVirtualHoststoStringwrapRequestwrapResponse

Constructor Detail

ResourceContextback to summary
public ResourceContext()