Polishing

This commit is contained in:
Juergen Hoeller 2015-12-11 21:56:26 +01:00
parent 356eaef20e
commit 0968e47b04
3 changed files with 18 additions and 14 deletions

View File

@ -18,7 +18,6 @@ package org.springframework.util;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Base64; import java.util.Base64;
import javax.xml.bind.DatatypeConverter; import javax.xml.bind.DatatypeConverter;
import org.springframework.lang.UsesJava8; import org.springframework.lang.UsesJava8;
@ -106,6 +105,7 @@ public abstract class Base64Utils {
* @return the encoded byte array (or {@code null} if the input was {@code null}) * @return the encoded byte array (or {@code null} if the input was {@code null})
* @throws IllegalStateException if Base64 encoding between byte arrays is not * @throws IllegalStateException if Base64 encoding between byte arrays is not
* supported, i.e. neither Java 8 nor Apache Commons Codec is present at runtime * supported, i.e. neither Java 8 nor Apache Commons Codec is present at runtime
* @since 4.2.4
*/ */
public static byte[] encodeUrlSafe(byte[] src) { public static byte[] encodeUrlSafe(byte[] src) {
assertDelegateAvailable(); assertDelegateAvailable();
@ -119,6 +119,7 @@ public abstract class Base64Utils {
* @return the original byte array (or {@code null} if the input was {@code null}) * @return the original byte array (or {@code null} if the input was {@code null})
* @throws IllegalStateException if Base64 encoding between byte arrays is not * @throws IllegalStateException if Base64 encoding between byte arrays is not
* supported, i.e. neither Java 8 nor Apache Commons Codec is present at runtime * supported, i.e. neither Java 8 nor Apache Commons Codec is present at runtime
* @since 4.2.4
*/ */
public static byte[] decodeUrlSafe(byte[] src) { public static byte[] decodeUrlSafe(byte[] src) {
assertDelegateAvailable(); assertDelegateAvailable();
@ -252,9 +253,11 @@ public abstract class Base64Utils {
static class CommonsCodecBase64Delegate implements Base64Delegate { static class CommonsCodecBase64Delegate implements Base64Delegate {
private final org.apache.commons.codec.binary.Base64 base64 = new org.apache.commons.codec.binary.Base64(); private final org.apache.commons.codec.binary.Base64 base64 =
new org.apache.commons.codec.binary.Base64();
private final org.apache.commons.codec.binary.Base64 base64UrlSafe = new org.apache.commons.codec.binary.Base64(0, null, true); private final org.apache.commons.codec.binary.Base64 base64UrlSafe =
new org.apache.commons.codec.binary.Base64(0, null, true);
@Override @Override
public byte[] encode(byte[] src) { public byte[] encode(byte[] src) {

View File

@ -54,9 +54,9 @@ public class ResourceUrlEncodingFilter extends OncePerRequestFilter {
private static class ResourceUrlEncodingResponseWrapper extends HttpServletResponseWrapper { private static class ResourceUrlEncodingResponseWrapper extends HttpServletResponseWrapper {
private HttpServletRequest request; private final HttpServletRequest request;
/* Cache the index of the path within the DispatcherServlet mapping. */ /* Cache the index of the path within the DispatcherServlet mapping */
private Integer indexLookupPath; private Integer indexLookupPath;
public ResourceUrlEncodingResponseWrapper(HttpServletRequest request, HttpServletResponse wrapped) { public ResourceUrlEncodingResponseWrapper(HttpServletRequest request, HttpServletResponse wrapped) {
@ -71,6 +71,7 @@ public class ResourceUrlEncodingFilter extends OncePerRequestFilter {
logger.debug("Request attribute exposing ResourceUrlProvider not found"); logger.debug("Request attribute exposing ResourceUrlProvider not found");
return super.encodeURL(url); return super.encodeURL(url);
} }
initIndexLookupPath(resourceUrlProvider); initIndexLookupPath(resourceUrlProvider);
if (url.length() >= this.indexLookupPath) { if (url.length() >= this.indexLookupPath) {
String prefix = url.substring(0, this.indexLookupPath); String prefix = url.substring(0, this.indexLookupPath);
@ -82,12 +83,13 @@ public class ResourceUrlEncodingFilter extends OncePerRequestFilter {
return super.encodeURL(prefix + lookupPath + suffix); return super.encodeURL(prefix + lookupPath + suffix);
} }
} }
return super.encodeURL(url); return super.encodeURL(url);
} }
private ResourceUrlProvider getResourceUrlProvider() { private ResourceUrlProvider getResourceUrlProvider() {
String name = ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR; return (ResourceUrlProvider) this.request.getAttribute(
return (ResourceUrlProvider) this.request.getAttribute(name); ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR);
} }
private void initIndexLookupPath(ResourceUrlProvider urlProvider) { private void initIndexLookupPath(ResourceUrlProvider urlProvider) {
@ -107,7 +109,7 @@ public class ResourceUrlEncodingFilter extends OncePerRequestFilter {
private int getQueryParamsIndex(String url) { private int getQueryParamsIndex(String url) {
int index = url.indexOf("?"); int index = url.indexOf("?");
return index > 0 ? index : url.length(); return (index > 0 ? index : url.length());
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -34,20 +34,19 @@ public class ResourceUrlProviderExposingInterceptor extends HandlerInterceptorAd
/** /**
* Name of the request attribute that holds the {@link ResourceUrlProvider}. * Name of the request attribute that holds the {@link ResourceUrlProvider}.
*/ */
public static final String RESOURCE_URL_PROVIDER_ATTR = ResourceUrlProvider.class.getName().toString(); public static final String RESOURCE_URL_PROVIDER_ATTR = ResourceUrlProvider.class.getName();
private final ResourceUrlProvider resourceUrlProvider; private final ResourceUrlProvider resourceUrlProvider;
public ResourceUrlProviderExposingInterceptor(ResourceUrlProvider resourceUrlProvider) { public ResourceUrlProviderExposingInterceptor(ResourceUrlProvider resourceUrlProvider) {
Assert.notNull(resourceUrlProvider, "'resourceUrlProvider' is required"); Assert.notNull(resourceUrlProvider, "ResourceUrlProvider is required");
this.resourceUrlProvider = resourceUrlProvider; this.resourceUrlProvider = resourceUrlProvider;
} }
@Override @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
Object handler) throws Exception { throws Exception {
request.setAttribute(RESOURCE_URL_PROVIDER_ATTR, this.resourceUrlProvider); request.setAttribute(RESOURCE_URL_PROVIDER_ATTR, this.resourceUrlProvider);
return true; return true;