OLINGO-907:corrected the usage to treat as individual query parameter and then combine with others to build the complete url

This commit is contained in:
Ramesh Reddy 2016-03-21 15:45:08 -05:00
parent dbf7fd386c
commit b6c7d401e2
3 changed files with 58 additions and 8 deletions

View File

@ -18,15 +18,16 @@
*/
package org.apache.olingo.server.core;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.StringTokenizer;
import org.apache.olingo.commons.api.ex.ODataException;
import org.apache.olingo.commons.api.format.ContentType;
import org.apache.olingo.commons.api.http.HttpHeader;
import org.apache.olingo.commons.api.http.HttpStatusCode;
import org.apache.olingo.commons.core.Decoder;
import org.apache.olingo.server.api.OData;
import org.apache.olingo.server.api.ODataApplicationException;
import org.apache.olingo.server.api.ODataLibraryException;
@ -83,8 +84,35 @@ public class ServiceDispatcher extends RequestURLHierarchyVisitor {
contentType = ContentNegotiator.doContentNegotiation(null,
odRequest, this.customContentSupport, RepresentationType.ERROR);
String path = odRequest.getRawODataPath();
String query = odRequest.getRawQueryPath();
if(path.indexOf("$entity") != -1) {
StringBuilder sb = new StringBuilder();
StringTokenizer st = new StringTokenizer(query, "&");
while(st.hasMoreTokens()) {
String token = st.nextToken();
if (token.startsWith("$id=")) {
try {
path = new URL(Decoder.decode(token.substring(4))).getPath();
int index = path.indexOf('/', 1);
if (index != -1) {
path = path.substring(index);
}
} catch (Exception e) {
path = Decoder.decode(token.substring(4));
}
} else {
if (sb.length() > 0) {
sb.append("&");
}
sb.append(token);
}
}
query = sb.toString();
}
UriInfo uriInfo = new Parser(this.metadata.getEdm(), odata)
.parseUri(odRequest.getRawODataPath(), odRequest.getRawQueryPath(), null);
.parseUri(path, query, null);
contentType = ContentNegotiator.doContentNegotiation(uriInfo.getFormatOption(),
odRequest, this.customContentSupport, RepresentationType.ERROR);
@ -244,6 +272,7 @@ public class ServiceDispatcher extends RequestURLHierarchyVisitor {
DataRequest dataRequest = new DataRequest(this.odata, this.metadata);
this.request = dataRequest;
/*
// this can relative or absolute form
String id = info.getIdOption().getValue();
try {
@ -252,6 +281,7 @@ public class ServiceDispatcher extends RequestURLHierarchyVisitor {
} catch (MalformedURLException e) {
this.idOption = id;
}
*/
super.visit(info);
}

View File

@ -43,6 +43,7 @@ import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.olingo.commons.api.http.HttpMethod;
import org.apache.olingo.commons.core.Encoder;
import org.apache.olingo.server.api.OData;
import org.apache.olingo.server.api.ODataHttpHandler;
import org.apache.olingo.server.api.ServiceMetadata;
@ -444,4 +445,22 @@ public class ServiceDispatcherTest {
}
});
}
@Test
public void test$id() throws Exception {
final ServiceHandler handler = Mockito.mock(ServiceHandler.class);
helpGETTest(handler, "trippin/$entity?$id="+Encoder.encode("http://localhost:" + TOMCAT_PORT
+ "/trippin/People('russelwhyte')")+"&"+Encoder.encode("$")+"select=FirstName", new TestResult() {
@Override
public void validate() throws Exception {
ArgumentCaptor<DataRequest> arg1 = ArgumentCaptor.forClass(DataRequest.class);
ArgumentCaptor<EntityResponse> arg2 = ArgumentCaptor.forClass(EntityResponse.class);
Mockito.verify(handler).read(arg1.capture(), arg2.capture());
DataRequest request = arg1.getValue();
assertEquals("application/json;odata.metadata=minimal", request.getResponseContentType()
.toContentTypeString());
}
});
}
}

View File

@ -396,16 +396,17 @@ public class TripPinServiceTest {
@Test
public void testEntityId() throws Exception {
HttpResponse response = httpGET(baseURL+"/$entity?$id="+baseURL + "/People('kristakemp')", 200);
HttpResponse response = httpGET(baseURL+"/$entity?$id="+baseURL
+ "/People('kristakemp')&$select=FirstName", 200);
JsonNode node = getJSONNode(response);
assertEquals("$metadata#People/$entity", node.get("@odata.context").asText());
assertEquals("kristakemp", node.get("UserName").asText());
assertEquals("$metadata#People(FirstName)/$entity", node.get("@odata.context").asText());
assertEquals("Krista", node.get("FirstName").asText());
// using relative URL
response = httpGET(baseURL+"/$entity?$id="+"People('kristakemp')", 200);
response = httpGET(baseURL+"/$entity?$id="+"People('kristakemp')&$select=FirstName", 200);
node = getJSONNode(response);
assertEquals("$metadata#People/$entity", node.get("@odata.context").asText());
assertEquals("kristakemp", node.get("UserName").asText());
assertEquals("$metadata#People(FirstName)/$entity", node.get("@odata.context").asText());
assertEquals("Krista", node.get("FirstName").asText());
}
@Test