mirror of
https://github.com/apache/olingo-odata4.git
synced 2025-03-04 23:59:09 +00:00
[OLINGO-175, OLINGO-205] provided parameter aliases support
This commit is contained in:
parent
e0d1b6ffac
commit
4931b30b41
@ -18,7 +18,6 @@
|
||||
*/
|
||||
package org.apache.olingo.client.api.uri;
|
||||
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Map;
|
||||
|
||||
@ -41,10 +40,19 @@ public interface CommonURIBuilder<UB extends CommonURIBuilder<?>> {
|
||||
*
|
||||
* @param option query option.
|
||||
* @param value query option value.
|
||||
* @return current URIBuilder instance
|
||||
* @return current URIBuilder instance.
|
||||
*/
|
||||
UB addQueryOption(String option, String value);
|
||||
|
||||
/**
|
||||
* Adds the specified (custom) parameter alias to the URI.
|
||||
*
|
||||
* @param alias parameter alias.
|
||||
* @param exp expression value.
|
||||
* @return current URIBuilder instance.
|
||||
*/
|
||||
UB addParameterAlias(final String alias, final String exp);
|
||||
|
||||
/**
|
||||
* Appends EntitySet segment to the URI.
|
||||
*
|
||||
@ -220,5 +228,4 @@ public interface CommonURIBuilder<UB extends CommonURIBuilder<?>> {
|
||||
* @return OData URI.
|
||||
*/
|
||||
URI build();
|
||||
|
||||
}
|
||||
|
@ -18,8 +18,10 @@
|
||||
*/
|
||||
package org.apache.olingo.client.core.uri;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.net.URLDecoder;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedHashMap;
|
||||
@ -72,6 +74,11 @@ public abstract class AbstractURIBuilder<UB extends CommonURIBuilder<?>> impleme
|
||||
*/
|
||||
protected final Map<String, String> queryOptions = new LinkedHashMap<String, String>();
|
||||
|
||||
/**
|
||||
* Insertion-order map of parameter aliases.
|
||||
*/
|
||||
protected final Map<String, String> parameters = new LinkedHashMap<String, String>();
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
@ -96,6 +103,12 @@ public abstract class AbstractURIBuilder<UB extends CommonURIBuilder<?>> impleme
|
||||
return getThis();
|
||||
}
|
||||
|
||||
@Override
|
||||
public UB addParameterAlias(final String alias, final String exp) {
|
||||
parameters.put(alias, exp);
|
||||
return getThis();
|
||||
}
|
||||
|
||||
@Override
|
||||
public UB appendEntitySetSegment(final String segmentValue) {
|
||||
segments.add(new Segment(SegmentType.ENTITYSET, segmentValue));
|
||||
@ -193,7 +206,12 @@ public abstract class AbstractURIBuilder<UB extends CommonURIBuilder<?>> impleme
|
||||
|
||||
@Override
|
||||
public UB filter(final URIFilter filter) {
|
||||
return addQueryOption(QueryOption.FILTER, filter.build());
|
||||
try {
|
||||
// decode in order to support @ in parameter aliases
|
||||
return addQueryOption(QueryOption.FILTER, URLDecoder.decode(filter.build(), "UTF-8"));
|
||||
} catch (UnsupportedEncodingException ex) {
|
||||
return addQueryOption(QueryOption.FILTER, filter.build());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -271,6 +289,10 @@ public abstract class AbstractURIBuilder<UB extends CommonURIBuilder<?>> impleme
|
||||
builder.addParameter("$" + option.getKey(), option.getValue());
|
||||
}
|
||||
|
||||
for (Map.Entry<String, String> parameter : parameters.entrySet()) {
|
||||
builder.addParameter("@" + parameter.getKey(), parameter.getValue());
|
||||
}
|
||||
|
||||
return builder.build().normalize();
|
||||
} catch (URISyntaxException e) {
|
||||
throw new IllegalArgumentException("Could not build valid URI", e);
|
||||
|
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* 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.client.core.uri;
|
||||
|
||||
/**
|
||||
* Object to be used to communicate parameter alias into filter or orderBy expression.
|
||||
*/
|
||||
public class ParameterAlias {
|
||||
|
||||
/**
|
||||
* Parameter alias.
|
||||
*/
|
||||
final String alias;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param alias parameter alias.
|
||||
*/
|
||||
public ParameterAlias(final String alias) {
|
||||
this.alias = alias;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets parameter alias.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getAlias() {
|
||||
return alias;
|
||||
}
|
||||
}
|
@ -225,12 +225,12 @@ public final class URIUtils {
|
||||
return version == ODataServiceVersion.V30
|
||||
? prefix(version, EdmPrimitiveTypeKind.DateTime)
|
||||
+ URLEncoder.encode(EdmDateTime.getInstance().
|
||||
valueToString(timestamp, null, null, Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null),
|
||||
Constants.UTF8)
|
||||
valueToString(timestamp, null, null, Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null),
|
||||
Constants.UTF8)
|
||||
+ suffix(version, EdmPrimitiveTypeKind.DateTime)
|
||||
: URLEncoder.encode(EdmDateTimeOffset.getInstance().
|
||||
valueToString(timestamp, null, null, Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null),
|
||||
Constants.UTF8);
|
||||
valueToString(timestamp, null, null, Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null),
|
||||
Constants.UTF8);
|
||||
}
|
||||
|
||||
private static String calendar(final ODataServiceVersion version, final Calendar calendar)
|
||||
@ -241,8 +241,8 @@ public final class URIUtils {
|
||||
if (version == ODataServiceVersion.V30) {
|
||||
result = prefix(version, EdmPrimitiveTypeKind.DateTime)
|
||||
+ URLEncoder.encode(EdmDateTime.getInstance().
|
||||
valueToString(calendar, null, null, Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null),
|
||||
Constants.UTF8)
|
||||
valueToString(calendar, null, null, Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null),
|
||||
Constants.UTF8)
|
||||
+ suffix(version, EdmPrimitiveTypeKind.DateTime);
|
||||
} else {
|
||||
if (calendar.get(Calendar.YEAR) == 0 && calendar.get(Calendar.MONTH) == 0
|
||||
@ -260,8 +260,8 @@ public final class URIUtils {
|
||||
} else {
|
||||
result = prefix(version, EdmPrimitiveTypeKind.DateTimeOffset)
|
||||
+ URLEncoder.encode(EdmDateTimeOffset.getInstance().
|
||||
valueToString(calendar, null, null, Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null),
|
||||
Constants.UTF8)
|
||||
valueToString(calendar, null, null, Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null),
|
||||
Constants.UTF8)
|
||||
+ suffix(version, EdmPrimitiveTypeKind.DateTimeOffset);
|
||||
}
|
||||
|
||||
@ -273,11 +273,11 @@ public final class URIUtils {
|
||||
|
||||
return version == ODataServiceVersion.V30
|
||||
? EdmTime.getInstance().toUriLiteral(URLEncoder.encode(EdmTime.getInstance().
|
||||
valueToString(duration, null, null,
|
||||
Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null), Constants.UTF8))
|
||||
valueToString(duration, null, null,
|
||||
Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null), Constants.UTF8))
|
||||
: EdmDuration.getInstance().toUriLiteral(URLEncoder.encode(EdmDuration.getInstance().
|
||||
valueToString(duration, null, null,
|
||||
Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null), Constants.UTF8));
|
||||
valueToString(duration, null, null,
|
||||
Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null), Constants.UTF8));
|
||||
}
|
||||
|
||||
private static String quoteString(final String string, final boolean singleQuoteEscape)
|
||||
@ -338,7 +338,9 @@ public final class URIUtils {
|
||||
|
||||
value = buffer.toString();
|
||||
} else {
|
||||
value = (obj instanceof Boolean)
|
||||
value = (obj instanceof ParameterAlias)
|
||||
? "@" + ((ParameterAlias) obj).getAlias().toString()
|
||||
: (obj instanceof Boolean)
|
||||
? BooleanUtils.toStringTrueFalse((Boolean) obj)
|
||||
: (obj instanceof UUID)
|
||||
? prefix(version, EdmPrimitiveTypeKind.Guid)
|
||||
@ -354,24 +356,24 @@ public final class URIUtils {
|
||||
? duration(version, (Duration) obj)
|
||||
: (obj instanceof BigDecimal)
|
||||
? EdmDecimal.getInstance().valueToString(obj, null, null,
|
||||
Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null)
|
||||
Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null)
|
||||
+ suffix(version, EdmPrimitiveTypeKind.Decimal)
|
||||
: (obj instanceof Double)
|
||||
? EdmDouble.getInstance().valueToString(obj, null, null,
|
||||
Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null)
|
||||
Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null)
|
||||
+ suffix(version, EdmPrimitiveTypeKind.Double)
|
||||
: (obj instanceof Float)
|
||||
? EdmSingle.getInstance().valueToString(obj, null, null,
|
||||
Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null)
|
||||
Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null)
|
||||
+ suffix(version, EdmPrimitiveTypeKind.Single)
|
||||
: (obj instanceof Long)
|
||||
? EdmInt64.getInstance().valueToString(obj, null, null,
|
||||
Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null)
|
||||
Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null)
|
||||
+ suffix(version, EdmPrimitiveTypeKind.Int64)
|
||||
: (obj instanceof Geospatial)
|
||||
? URLEncoder.encode(EdmPrimitiveTypeFactory.getInstance(((Geospatial) obj).getEdmPrimitiveTypeKind()).
|
||||
valueToString(obj, null, null, Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null),
|
||||
Constants.UTF8)
|
||||
valueToString(obj, null, null, Constants.DEFAULT_PRECISION, Constants.DEFAULT_SCALE, null),
|
||||
Constants.UTF8)
|
||||
: (obj instanceof String)
|
||||
? quoteString((String) obj, singleQuoteEscape)
|
||||
: obj.toString();
|
||||
|
@ -29,6 +29,7 @@ import java.util.Map;
|
||||
import org.apache.olingo.client.api.v3.ODataClient;
|
||||
import org.apache.olingo.client.api.uri.v3.URIBuilder;
|
||||
import org.apache.olingo.client.core.AbstractTest;
|
||||
import org.apache.olingo.client.core.uri.ParameterAlias;
|
||||
import org.junit.Test;
|
||||
|
||||
public class URIBuilderTest extends AbstractTest {
|
||||
@ -100,6 +101,18 @@ public class URIBuilderTest extends AbstractTest {
|
||||
uriBuilder.build());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void filterWithParameter() throws URISyntaxException {
|
||||
// http://host/service.svc/Employees?$filter=Region eq @p1&@p1='WA'
|
||||
final URIBuilder uriBuilder = getClient().getURIBuilder(SERVICE_ROOT).appendEntitySetSegment("Employees").
|
||||
filter(getClient().getFilterFactory().eq("Region", new ParameterAlias("p1"))).
|
||||
addParameterAlias("p1", "'WA'");
|
||||
|
||||
assertEquals(new org.apache.http.client.utils.URIBuilder(SERVICE_ROOT + "/Employees").
|
||||
addParameter("$filter", "(Region eq @p1)").addParameter("@p1", "'WA'").build(),
|
||||
uriBuilder.build());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void unboundAction() throws URISyntaxException {
|
||||
final URIBuilder uriBuilder = getClient().getURIBuilder(SERVICE_ROOT).
|
||||
|
Loading…
x
Reference in New Issue
Block a user