[OLINGO-328] improve AcceptType for multi value support
This commit is contained in:
parent
af3f998b7f
commit
0ffc26d1b2
|
@ -50,7 +50,7 @@ public class AcceptType {
|
||||||
private static final String PARAMETER_Q = "q";
|
private static final String PARAMETER_Q = "q";
|
||||||
private static final Pattern Q_PARAMETER_VALUE_PATTERN = Pattern.compile("1|0|1\\.0{1,3}|0\\.\\d{1,3}");
|
private static final Pattern Q_PARAMETER_VALUE_PATTERN = Pattern.compile("1|0|1\\.0{1,3}|0\\.\\d{1,3}");
|
||||||
|
|
||||||
public static final AcceptType WILDCARD = create(MEDIA_TYPE_WILDCARD, MEDIA_TYPE_WILDCARD, null, 1F);
|
public static final AcceptType WILDCARD = create(MEDIA_TYPE_WILDCARD, MEDIA_TYPE_WILDCARD, createParameterMap(), 1F);
|
||||||
|
|
||||||
private final String type;
|
private final String type;
|
||||||
private final String subtype;
|
private final String subtype;
|
||||||
|
@ -66,7 +66,7 @@ public class AcceptType {
|
||||||
this.quality = quality;
|
this.quality = quality;
|
||||||
}
|
}
|
||||||
|
|
||||||
private TreeMap<String, String> createParameterMap() {
|
private static TreeMap<String, String> createParameterMap() {
|
||||||
return new TreeMap<String, String>(new Comparator<String>() {
|
return new TreeMap<String, String>(new Comparator<String>() {
|
||||||
@Override
|
@Override
|
||||||
public int compare(final String o1, final String o2) {
|
public int compare(final String o1, final String o2) {
|
||||||
|
@ -123,11 +123,15 @@ public class AcceptType {
|
||||||
* @return a new <code>AcceptType</code> object
|
* @return a new <code>AcceptType</code> object
|
||||||
* @throws IllegalArgumentException if input string is not parseable
|
* @throws IllegalArgumentException if input string is not parseable
|
||||||
*/
|
*/
|
||||||
public static AcceptType create(final String format) {
|
public static List<AcceptType> create(final String format) {
|
||||||
if (format == null) {
|
List<AcceptType> result = new ArrayList<AcceptType>();
|
||||||
throw new IllegalArgumentException("Parameter format MUST NOT be NULL.");
|
|
||||||
|
String[] values = format.split(",");
|
||||||
|
for (String value : values) {
|
||||||
|
result.add(new AcceptType(value.trim()));
|
||||||
}
|
}
|
||||||
return new AcceptType(format);
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -136,7 +140,7 @@ public class AcceptType {
|
||||||
* @param format
|
* @param format
|
||||||
* @return a new <code>ContentType</code> object
|
* @return a new <code>ContentType</code> object
|
||||||
*/
|
*/
|
||||||
public static AcceptType parse(final String format) {
|
public static List<AcceptType> parse(final String format) {
|
||||||
try {
|
try {
|
||||||
return AcceptType.create(format);
|
return AcceptType.create(format);
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
|
@ -222,7 +226,7 @@ public class AcceptType {
|
||||||
public static List<AcceptType> create(final List<String> acceptTypeStrings) {
|
public static List<AcceptType> create(final List<String> acceptTypeStrings) {
|
||||||
List<AcceptType> acceptTypes = new ArrayList<AcceptType>(acceptTypeStrings.size());
|
List<AcceptType> acceptTypes = new ArrayList<AcceptType>(acceptTypeStrings.size());
|
||||||
for (String contentTypeString : acceptTypeStrings) {
|
for (String contentTypeString : acceptTypeStrings) {
|
||||||
acceptTypes.add(create(contentTypeString));
|
acceptTypes.addAll(create(contentTypeString));
|
||||||
}
|
}
|
||||||
return acceptTypes;
|
return acceptTypes;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,46 @@
|
||||||
|
/*
|
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one
|
||||||
|
* or more contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. The ASF licenses this file
|
||||||
|
* to you 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.
|
||||||
|
*/
|
||||||
|
package org.apache.olingo.commons.api.format;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class AcceptTypeTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMultiValueCreate() {
|
||||||
|
List<AcceptType> atl = AcceptType.create("1/1,2/2 , 3/3 ");
|
||||||
|
|
||||||
|
assertEquals(3, atl.size());
|
||||||
|
assertEquals("1/1", atl.get(0).toString());
|
||||||
|
assertEquals("2/2", atl.get(1).toString());
|
||||||
|
assertEquals("3/3", atl.get(2).toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSingleValueCreate() {
|
||||||
|
List<AcceptType> atl = AcceptType.create(" a/a ");
|
||||||
|
|
||||||
|
assertEquals(1, atl.size());
|
||||||
|
assertEquals("a/a", atl.get(0).toString());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue