public class ResourceHttpRequestHandler extends WebContentGenerator implements HttpRequestHandler, EmbeddedValueResolverAware, InitializingBean, CorsConfigurationSource
HttpRequestHandler
"locations" プロパティは、静的リソースがこのハンドラーによって提供されることを許可されている Spring Resource
ロケーションのリストを取ります。リソースは、クラスパスの場所から提供できます。"classpath:/META-INF/public-web-resources/"。jar ファイルで .js、.css などのリソースを便利にパッケージ化して提供できます。
このリクエストハンドラーは、resourcesResolver
および resourceTransformer
チェーンで構成して、提供されるリソースの任意の解決と変換をサポートすることもできます。デフォルトでは、PathResourceResolver
は設定された「場所」に基づいてリソースを単に検索します。アプリケーションは、URL にあるバージョンのリソースの URL を解決および準備できる VersionResourceResolver
などの追加のリゾルバーおよびトランスフォーマーを構成できます。
また、このハンドラーは Last-Modified
ヘッダー(存在する場合)を適切に評価するため、304
ステータスコードが適切に返され、クライアントによってすでにキャッシュされているリソースの不要なオーバーヘッドが回避されます。
HEADER_CACHE_CONTROL, METHOD_GET, METHOD_HEAD, METHOD_POST
コンストラクターと説明 |
---|
ResourceHttpRequestHandler() |
applyCacheControl, applyCacheSeconds, applyCacheSeconds, cacheForSeconds, cacheForSeconds, checkAndPrepare, checkAndPrepare, checkRequest, getAllowHeader, getCacheControl, getCacheSeconds, getSupportedMethods, getVaryByRequestHeaders, isAlwaysMustRevalidate, isRequireSession, isUseCacheControlHeader, isUseCacheControlNoStore, isUseExpiresHeader, prepareResponse, preventCaching, setAlwaysMustRevalidate, setCacheControl, setCacheSeconds, setRequireSession, setSupportedMethods, setUseCacheControlHeader, setUseCacheControlNoStore, setUseExpiresHeader, setVaryByRequestHeaders
getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext
getApplicationContext, getMessageSourceAccessor, initApplicationContext, obtainApplicationContext, requiredContextClass, setApplicationContext
cloneSE, equalsSE, finalizeSE, getClassSE, hashCodeSE, notifySE, notifyAllSE, waitSE, waitSE, waitSE
public void setLocationValues(ListSE<StringSE> locationValues)
setLocations(List)
の代替。相対パスを追加するときに使用する文字セットを示す特別なプレフィックスを持つ UrlResource
(たとえば、ファイルまたは HTTP URL)をサポートします。たとえば、"[charset=Windows-31J]https://example.org/path"
。public void setLocations(ListSE<Resource> locations)
Resource
ロケーションの List
を設定します。public ListSE<Resource> getLocations()
Resource
ロケーションの構成済み List
を返します。 ロードされたリソースベースの場所の代わりに locationValues
が提供される場合、このメソッドは afterPropertiesSet()
による初期化の後まで空を返すことに注意してください。
public void setResourceResolvers(@Nullable ListSE<ResourceResolver> resourceResolvers)
ResourceResolvers
のリストを構成します。 デフォルトでは、PathResourceResolver
が構成されています。このプロパティを使用する場合、PathResourceResolver
を最後のリゾルバーとして追加することをお勧めします。
public ListSE<ResourceResolver> getResourceResolvers()
public void setResourceTransformers(@Nullable ListSE<ResourceTransformer> resourceTransformers)
ResourceTransformers
のリストを構成します。デフォルトでは、使用するトランスフォーマは構成されていません。
public ListSE<ResourceTransformer> getResourceTransformers()
public void setResourceHttpMessageConverter(@Nullable ResourceHttpMessageConverter messageConverter)
ResourceHttpMessageConverter
を構成します。 デフォルトでは、ResourceHttpMessageConverter
が構成されます。
@Nullable public ResourceHttpMessageConverter getResourceHttpMessageConverter()
public void setResourceRegionHttpMessageConverter(@Nullable ResourceRegionHttpMessageConverter messageConverter)
ResourceRegionHttpMessageConverter
を構成します。 デフォルトでは、ResourceRegionHttpMessageConverter
が構成されます。
@Nullable public ResourceRegionHttpMessageConverter getResourceRegionHttpMessageConverter()
public void setContentNegotiationManager(@Nullable ContentNegotiationManager contentNegotiationManager)
ContentNegotiationManager
を設定して、提供されているリソースのメディア型を決定します。マネージャーにパス拡張子戦略が含まれている場合、登録されているファイル拡張子がチェックされます。@Nullable public ContentNegotiationManager getContentNegotiationManager()
public void setCorsConfiguration(CorsConfiguration corsConfiguration)
デフォルトでは、これはクロスオリジンリクエストを許可するように設定されていません。
@Nullable public CorsConfiguration getCorsConfiguration(HttpServletRequestEE request)
CorsConfigurationSource
の getCorsConfiguration
CorsConfiguration
、または存在しない場合は null
public void setUrlPathHelper(@Nullable UrlPathHelper urlPathHelper)
UrlPathHelper
への参照を提供します。これは、デコードされているかどうかなど、ルックアップパスに関する情報を導き出すのに役立ちます。@Nullable public UrlPathHelper getUrlPathHelper()
UrlPathHelper
。public void setEmbeddedValueResolver(StringValueResolver resolver)
EmbeddedValueResolverAware
EmbeddedValueResolverAware
の setEmbeddedValueResolver
public void afterPropertiesSet() throws ExceptionSE
InitializingBean
BeanFactoryAware
、ApplicationContextAware
などを満たした後、包含 BeanFactory
によって呼び出されます。このメソッドにより、Bean インスタンスは、すべての Bean プロパティが設定されたときに、その全体的な構成の検証と最終的な初期化を実行できます。
InitializingBean
の afterPropertiesSet
ExceptionSE
- 構成の誤り(必須プロパティの設定の失敗など)の場合、またはその他の理由で初期化が失敗した場合 protected void initAllowedLocations()
PathResourceResolver
を探し、そのクラスで構成された locations
と一致するように allowedLocations
プロパティ(空の場合)を設定します。protected PathExtensionContentNegotiationStrategy initContentNegotiationStrategy()
ContentNegotiationManager
セットアップと ServletContext
の可用性に応じて、コンテンツネゴシエーション戦略を初期化します。public void handleRequest(HttpServletRequestEE request, HttpServletResponseEE response) throws ServletExceptionEE, IOExceptionSE
設定された場所のリストでリクエストされたリソースの存在を確認します。リソースが存在しない場合、404
レスポンスがクライアントに返されます。リソースが存在する場合、Last-Modified
ヘッダーの存在についてリクエストがチェックされ、その値は指定されたリソースの最終変更タイムスタンプと比較され、Last-Modified
値が大きい場合は 304
ステータスコードが返されます。リソースが Last-Modified
値よりも新しい場合、またはヘッダーが存在しない場合、リソースのコンテンツリソースは、キャッシュヘッダーが 1 年後に期限切れになるように設定されたレスポンスに書き込まれます。
HttpRequestHandler
の handleRequest
request
- 現在の HTTP リクエスト response
- 現在の HTTP レスポンス ServletExceptionEE
- 一般的なエラーの場合 IOExceptionSE
- I/O エラーの場合 @Nullable protected Resource getResource(HttpServletRequestEE request) throws IOExceptionSE
protected StringSE processPath(StringSE path)
デフォルトの実装は次のものに置き換わります。
" / // foo/bar"
は "/foo/bar"
になります。protected boolean isInvalidPath(StringSE path)
StringUtils.cleanPath(java.lang.String)
の呼び出し後の "../" を含むパス。valid URL
を表すパス、または先頭のスラッシュが削除された後のパスを表すパス。注意 : この方法では、先頭の重複する "/" または制御文字(空白など)が削除され、パスが単一の "/" で予測どおりに開始するか、パスが 1 つもないことが想定されています。
path
- 検証するパス true
、そうでない場合は false
@Nullable protected MediaType getMediaType(HttpServletRequestEE request, Resource resource)
ServletPathExtensionContentNegotiationStrategy.getMediaTypeForResource(org.springframework.core.io.Resource)
を介して Resource のファイル拡張子に基づいて MediaType を決定しようとします。request
- 現在のリクエスト resource
- チェックするリソース null
protected void setHeaders(HttpServletResponseEE response, Resource resource, @Nullable MediaType mediaType) throws IOExceptionSE
response
- 現在のサーブレットレスポンス resource
- 特定されたリソース (非 null
)mediaType
- リソースのメディア型 (非 null
)IOExceptionSE
- ヘッダーの設定中にエラーが発生した場合 public StringSE toString()
ObjectSE
の toStringSE