Local mergeStringArrays variant for Portlets (superseding StringUtils)

This commit is contained in:
Juergen Hoeller 2018-02-22 15:48:35 +01:00
parent 8b5563ecab
commit 37ee264fbf
3 changed files with 36 additions and 10 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -771,8 +771,8 @@ public class AnnotationMethodHandlerAdapter extends PortletContentGenerator
for (RequestMethod method : methods) {
this.methods.add(method.name());
}
this.params = StringUtils.mergeStringArrays(this.params, params);
this.headers = StringUtils.mergeStringArrays(this.headers, headers);
this.params = PortletAnnotationMappingUtils.mergeStringArrays(this.params, params);
this.headers = PortletAnnotationMappingUtils.mergeStringArrays(this.headers, headers);
}
public void initPhaseMapping(String phase, String value, String[] params) {
@ -782,7 +782,7 @@ public class AnnotationMethodHandlerAdapter extends PortletContentGenerator
}
this.phase = phase;
this.value = value;
this.params = StringUtils.mergeStringArrays(this.params, params);
this.params = PortletAnnotationMappingUtils.mergeStringArrays(this.params, params);
}
public boolean match(PortletRequest request) {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -154,7 +154,7 @@ public class DefaultAnnotationHandlerMapping extends AbstractMapBasedHandlerMapp
String[] modeKeys = new String[0];
String[] params = new String[0];
if (typeMapping != null) {
params = StringUtils.mergeStringArrays(typeMapping.params(), params);
params = PortletAnnotationMappingUtils.mergeStringArrays(typeMapping.params(), params);
}
ActionMapping actionMapping = AnnotationUtils.findAnnotation(method, ActionMapping.class);
RenderMapping renderMapping = AnnotationUtils.findAnnotation(method, RenderMapping.class);
@ -162,11 +162,11 @@ public class DefaultAnnotationHandlerMapping extends AbstractMapBasedHandlerMapp
EventMapping eventMapping = AnnotationUtils.findAnnotation(method, EventMapping.class);
RequestMapping requestMapping = AnnotationUtils.findAnnotation(method, RequestMapping.class);
if (actionMapping != null) {
params = StringUtils.mergeStringArrays(params, actionMapping.params());
params = PortletAnnotationMappingUtils.mergeStringArrays(params, actionMapping.params());
predicate = new ActionMappingPredicate(actionMapping.name(), params);
}
else if (renderMapping != null) {
params = StringUtils.mergeStringArrays(params, renderMapping.params());
params = PortletAnnotationMappingUtils.mergeStringArrays(params, renderMapping.params());
predicate = new RenderMappingPredicate(renderMapping.windowState(), params);
}
else if (resourceMapping != null) {
@ -183,7 +183,7 @@ public class DefaultAnnotationHandlerMapping extends AbstractMapBasedHandlerMapp
Arrays.asList(modeKeys) + " versus " + Arrays.asList(typeMapping.value()));
}
}
params = StringUtils.mergeStringArrays(params, requestMapping.params());
params = PortletAnnotationMappingUtils.mergeStringArrays(params, requestMapping.params());
if (predicate == null) {
predicate = new MethodLevelMappingPredicate(params);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,7 +16,9 @@
package org.springframework.web.portlet.mvc.annotation;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import javax.portlet.ClientDataRequest;
@ -24,6 +26,7 @@ import javax.portlet.PortletRequest;
import org.springframework.http.MediaType;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.portlet.util.PortletUtils;
@ -35,6 +38,29 @@ import org.springframework.web.portlet.util.PortletUtils;
*/
abstract class PortletAnnotationMappingUtils {
/**
* Merge the given {@code String} arrays into one, with each element only included once.
* <p>The order of elements in the original arrays is preserved (with the exception of
* overlapping elements, which are only included on their first occurrence).
* @param array1 the first array (can be {@code null})
* @param array2 the second array (can be {@code null})
* @return the new array ({@code null} if both given arrays were {@code null})
* @since 4.3.15 (superseding {@link StringUtils#mergeStringArrays})
*/
public static String[] mergeStringArrays(String[] array1, String[] array2) {
if (ObjectUtils.isEmpty(array1)) {
return array2;
}
if (ObjectUtils.isEmpty(array2)) {
return array1;
}
Set<String> result = new LinkedHashSet<String>();
result.addAll(Arrays.asList(array1));
result.addAll(Arrays.asList(array2));
return StringUtils.toStringArray(result);
}
/**
* Check whether the given portlet modes matches the specified type-level modes.
* @param modes the mapped portlet modes to check