Repository: olingo-odata4 Updated Branches: refs/heads/master 2e7883f61 -> 17f173a94
[OLINGO-266] processor registry Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/a9ed4655 Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/a9ed4655 Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/a9ed4655 Branch: refs/heads/master Commit: a9ed4655168fa457da94a5d0c065d25e32ac7024 Parents: 5fb2b80 Author: Stephan Klevenz <[email protected]> Authored: Thu May 22 12:34:23 2014 +0200 Committer: Stephan Klevenz <[email protected]> Committed: Thu May 22 12:34:23 2014 +0200 ---------------------------------------------------------------------- .../olingo/server/api/ODataHttpHandler.java | 4 + .../apache/olingo/server/api/ODataRequest.java | 103 +++++++++++++++++++ .../apache/olingo/server/api/ODataResponse.java | 56 ++++++++++ .../server/api/processor/DeleteMeProcessor.java | 23 +++++ .../server/api/processor/MetadataProcessor.java | 28 +++++ .../olingo/server/api/processor/Processor.java | 28 +++++ .../api/processor/ServiceDocumentProcessor.java | 29 ++++++ .../olingo/server/core/DefaultProcessor.java | 70 +++++++++++++ .../apache/olingo/server/core/ODataHandler.java | 68 ++++++++---- .../server/core/ODataHttpHandlerImpl.java | 21 ++-- .../apache/olingo/server/core/ODataRequest.java | 103 ------------------- .../olingo/server/core/ODataResponse.java | 56 ---------- .../server/core/uri/validator/UriValidator.java | 28 +++-- .../server/core/ODataHttpHandlerImplTest.java | 1 + .../server/tecsvc/TechnicalProcessor.java | 39 +++++++ .../olingo/server/tecsvc/TechnicalServlet.java | 6 ++ .../olingo/server/core/ODataHandlerTest.java | 15 ++- .../core/uri/testutil/ResourceValidator.java | 3 +- .../core/uri/testutil/TestUriValidator.java | 3 +- .../core/uri/validator/UriValidatorTest.java | 37 +++---- 20 files changed, 501 insertions(+), 220 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/a9ed4655/lib/server-api/src/main/java/org/apache/olingo/server/api/ODataHttpHandler.java ---------------------------------------------------------------------- diff --git a/lib/server-api/src/main/java/org/apache/olingo/server/api/ODataHttpHandler.java b/lib/server-api/src/main/java/org/apache/olingo/server/api/ODataHttpHandler.java index 82210b8..8372f80 100644 --- a/lib/server-api/src/main/java/org/apache/olingo/server/api/ODataHttpHandler.java +++ b/lib/server-api/src/main/java/org/apache/olingo/server/api/ODataHttpHandler.java @@ -21,8 +21,12 @@ package org.apache.olingo.server.api; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.apache.olingo.server.api.processor.Processor; + public interface ODataHttpHandler { void process(HttpServletRequest request, HttpServletResponse response); + void register(Processor processor); + } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/a9ed4655/lib/server-api/src/main/java/org/apache/olingo/server/api/ODataRequest.java ---------------------------------------------------------------------- diff --git a/lib/server-api/src/main/java/org/apache/olingo/server/api/ODataRequest.java b/lib/server-api/src/main/java/org/apache/olingo/server/api/ODataRequest.java new file mode 100644 index 0000000..7bae3be --- /dev/null +++ b/lib/server-api/src/main/java/org/apache/olingo/server/api/ODataRequest.java @@ -0,0 +1,103 @@ +/* + * 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.server.api; + +import java.io.InputStream; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.olingo.commons.api.http.HttpMethod; + +public class ODataRequest { + private HttpMethod method; + private Map<String, List<String>> headers = new HashMap<String, List<String>>(); + private InputStream body; + private String rawQueryPath; + private String rawRequestUri; + private String rawODataPath; + private String rawBaseUri; + private String rawServiceResolutionUri; + + public HttpMethod getMethod() { + return method; + } + + public void setMethod(final HttpMethod method) { + this.method = method; + } + + public Map<String, List<String>> getHeaders() { + return Collections.unmodifiableMap(headers); + } + + public void setHeaders(final Map<String, List<String>> headers) { + this.headers = headers; + } + + public InputStream getBody() { + return body; + } + + public void setBody(final InputStream body) { + this.body = body; + } + + public String getRawQueryPath() { + return rawQueryPath; + } + + public void setRawQueryPath(String rawQueryPath) { + this.rawQueryPath = rawQueryPath; + } + + public String getRawBaseUri() { + return rawBaseUri; + } + + public String getRawRequestUri() { + return rawRequestUri; + } + + public String getRawODataPath() { + return rawODataPath; + } + + public void setRawRequestUri(String rawRequestUri) { + this.rawRequestUri = rawRequestUri; + } + + public void setRawODataPath(String rawODataPath) { + this.rawODataPath = rawODataPath; + + } + + public void setRawBaseUri(String rawBaseUri) { + this.rawBaseUri = rawBaseUri; + } + + public String getRawServiceResolutionUri() { + return rawServiceResolutionUri; + } + + public void setRawServiceResolutionUri(String rawServiceResolutionUri) { + this.rawServiceResolutionUri = rawServiceResolutionUri; + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/a9ed4655/lib/server-api/src/main/java/org/apache/olingo/server/api/ODataResponse.java ---------------------------------------------------------------------- diff --git a/lib/server-api/src/main/java/org/apache/olingo/server/api/ODataResponse.java b/lib/server-api/src/main/java/org/apache/olingo/server/api/ODataResponse.java new file mode 100644 index 0000000..4de7aaa --- /dev/null +++ b/lib/server-api/src/main/java/org/apache/olingo/server/api/ODataResponse.java @@ -0,0 +1,56 @@ +/* + * 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.server.api; + +import java.io.InputStream; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +public class ODataResponse { + + private int statusCode; + private Map<String, String> headers = new HashMap<String, String>(); + private InputStream content; + + public void setStatusCode(final int statusCode) { + this.statusCode = statusCode; + } + + public void setHeader(final String name, final String value) { + headers.put(name, value); + } + + public void setContent(final InputStream content) { + this.content = content; + } + + public int getStatusCode() { + return statusCode; + } + + public Map<String, String> getHeaders() { + return Collections.unmodifiableMap(headers); + } + + public InputStream getContent() { + return content; + } + +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/a9ed4655/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/DeleteMeProcessor.java ---------------------------------------------------------------------- diff --git a/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/DeleteMeProcessor.java b/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/DeleteMeProcessor.java new file mode 100644 index 0000000..783e709 --- /dev/null +++ b/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/DeleteMeProcessor.java @@ -0,0 +1,23 @@ +/* + * 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.server.api.processor; + +public interface DeleteMeProcessor extends Processor { + +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/a9ed4655/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/MetadataProcessor.java ---------------------------------------------------------------------- diff --git a/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/MetadataProcessor.java b/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/MetadataProcessor.java new file mode 100644 index 0000000..64d5c16 --- /dev/null +++ b/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/MetadataProcessor.java @@ -0,0 +1,28 @@ +/* + * 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.server.api.processor; + +import org.apache.olingo.server.api.ODataRequest; +import org.apache.olingo.server.api.ODataResponse; +import org.apache.olingo.server.api.uri.UriInfo; + +public interface MetadataProcessor extends Processor{ + + void readMetadata(ODataRequest request, ODataResponse response, UriInfo uriInfo, String format); +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/a9ed4655/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/Processor.java ---------------------------------------------------------------------- diff --git a/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/Processor.java b/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/Processor.java new file mode 100644 index 0000000..014fba7 --- /dev/null +++ b/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/Processor.java @@ -0,0 +1,28 @@ +/* + * 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.server.api.processor; + +import org.apache.olingo.commons.api.edm.Edm; +import org.apache.olingo.server.api.OData; + +public interface Processor { + + void init(OData odata, Edm edm); + +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/a9ed4655/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/ServiceDocumentProcessor.java ---------------------------------------------------------------------- diff --git a/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/ServiceDocumentProcessor.java b/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/ServiceDocumentProcessor.java new file mode 100644 index 0000000..a1ef668 --- /dev/null +++ b/lib/server-api/src/main/java/org/apache/olingo/server/api/processor/ServiceDocumentProcessor.java @@ -0,0 +1,29 @@ +/* + * 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.server.api.processor; + +import org.apache.olingo.server.api.ODataRequest; +import org.apache.olingo.server.api.ODataResponse; +import org.apache.olingo.server.api.uri.UriInfo; + +public interface ServiceDocumentProcessor extends Processor { + + void readServiceDocument(ODataRequest request, ODataResponse response, UriInfo uriInfo, String format); + +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/a9ed4655/lib/server-core/src/main/java/org/apache/olingo/server/core/DefaultProcessor.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/DefaultProcessor.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/DefaultProcessor.java new file mode 100644 index 0000000..1bf9dac --- /dev/null +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/DefaultProcessor.java @@ -0,0 +1,70 @@ +/* + * 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.server.core; + +import java.io.InputStream; + +import org.apache.olingo.commons.api.edm.Edm; +import org.apache.olingo.server.api.OData; +import org.apache.olingo.server.api.ODataRequest; +import org.apache.olingo.server.api.ODataResponse; +import org.apache.olingo.server.api.processor.MetadataProcessor; +import org.apache.olingo.server.api.processor.ServiceDocumentProcessor; +import org.apache.olingo.server.api.serializer.ODataFormat; +import org.apache.olingo.server.api.serializer.ODataSerializer; +import org.apache.olingo.server.api.uri.UriInfo; + +public class DefaultProcessor implements MetadataProcessor, ServiceDocumentProcessor { + + private OData odata; + private Edm edm; + + @Override + public void init(OData odata, Edm edm) { + this.odata = odata; + this.edm = edm; + } + + @Override + public void readServiceDocument(ODataRequest request, ODataResponse response, UriInfo uriInfo, String format) { + ODataSerializer serializer; + InputStream responseEntity; + + serializer = odata.createSerializer(ODataFormat.JSON); + responseEntity = serializer.serviceDocument(edm, request.getRawBaseUri()); + + response.setStatusCode(200); + response.setHeader("Content-Type", "application/json"); + response.setContent(responseEntity); + + } + + @Override + public void readMetadata(ODataRequest request, ODataResponse response, UriInfo uriInfo, String format) { + ODataSerializer serializer; + InputStream responseEntity; + + serializer = odata.createSerializer(ODataFormat.XML); + responseEntity = serializer.metadataDocument(edm); + response.setStatusCode(200); + response.setHeader("Content-Type", "application/xml"); + response.setContent(responseEntity); + } + +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/a9ed4655/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java index de8ef23..d2d9b63 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandler.java @@ -18,52 +18,56 @@ */ package org.apache.olingo.server.core; -import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; import org.apache.olingo.commons.api.ODataRuntimeException; import org.apache.olingo.commons.api.edm.Edm; import org.apache.olingo.server.api.OData; -import org.apache.olingo.server.api.serializer.ODataFormat; -import org.apache.olingo.server.api.serializer.ODataSerializer; +import org.apache.olingo.server.api.ODataRequest; +import org.apache.olingo.server.api.ODataResponse; +import org.apache.olingo.server.api.processor.MetadataProcessor; +import org.apache.olingo.server.api.processor.Processor; +import org.apache.olingo.server.api.processor.ServiceDocumentProcessor; import org.apache.olingo.server.api.uri.UriInfo; import org.apache.olingo.server.core.uri.parser.Parser; +import org.apache.olingo.server.core.uri.validator.UriValidator; + +import sun.reflect.generics.reflectiveObjects.NotImplementedException; public class ODataHandler { - private final OData server; + private final OData odata; private final Edm edm; + private Map<Class<? extends Processor>, Processor> processors = new HashMap<Class<? extends Processor>, Processor>(); public ODataHandler(final OData server, final Edm edm) { - this.server = server; + this.odata = server; this.edm = edm; + + register(new DefaultProcessor()); } - public ODataResponse process(final ODataRequest odRequest) { + public ODataResponse process(final ODataRequest request) { try { ODataResponse response = new ODataResponse(); Parser parser = new Parser(); String odUri = - odRequest.getRawODataPath() + (odRequest.getRawQueryPath() == null ? "" : "?" + odRequest.getRawQueryPath()); + request.getRawODataPath() + (request.getRawQueryPath() == null ? "" : "?" + request.getRawQueryPath()); UriInfo uriInfo = parser.parseUri(odUri, edm); - ODataSerializer serializer; - InputStream responseEntity; + UriValidator validator = new UriValidator(); + validator.validate(uriInfo, request.getMethod()); + switch (uriInfo.getKind()) { case metadata: - serializer = server.createSerializer(ODataFormat.XML); - responseEntity = serializer.metadataDocument(edm); - response.setStatusCode(200); - response.setHeader("Content-Type", "application/xml"); - response.setContent(responseEntity); + MetadataProcessor mp = selectProcessor(MetadataProcessor.class); + mp.readMetadata(request, response, uriInfo, "application/xml"); break; case service: - serializer = server.createSerializer(ODataFormat.JSON); - responseEntity = serializer.serviceDocument(edm, odRequest.getRawBaseUri()); - - response.setStatusCode(200); - response.setHeader("Content-Type", "application/json"); - response.setContent(responseEntity); + ServiceDocumentProcessor sdp = selectProcessor(ServiceDocumentProcessor.class); + sdp.readServiceDocument(request, response, uriInfo, "application/json"); break; default: throw new ODataRuntimeException("not implemented"); @@ -75,4 +79,28 @@ public class ODataHandler { throw new RuntimeException(e); } } + + private <T extends Processor> T selectProcessor(Class<T> cls) { + @SuppressWarnings("unchecked") + T p = (T) processors.get(cls); + + if (p == null) { + throw new NotImplementedException(); + } + + return p; + } + + public void register(Processor processor) { + + processor.init(odata, edm); + + for (Class<?> cls : processor.getClass().getInterfaces()) { + if (Processor.class.isAssignableFrom(cls)) { + @SuppressWarnings("unchecked") + Class<? extends Processor> procClass = (Class<? extends Processor>) cls; + processors.put(procClass, processor); + } + } + } } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/a9ed4655/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHttpHandlerImpl.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHttpHandlerImpl.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHttpHandlerImpl.java index be61a2b..a212ea8 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHttpHandlerImpl.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHttpHandlerImpl.java @@ -36,6 +36,9 @@ import org.apache.olingo.commons.api.edm.Edm; import org.apache.olingo.commons.api.http.HttpMethod; import org.apache.olingo.server.api.OData; import org.apache.olingo.server.api.ODataHttpHandler; +import org.apache.olingo.server.api.ODataRequest; +import org.apache.olingo.server.api.ODataResponse; +import org.apache.olingo.server.api.processor.Processor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -43,19 +46,20 @@ public class ODataHttpHandlerImpl implements ODataHttpHandler { private static final Logger LOG = LoggerFactory.getLogger(ODataHttpHandlerImpl.class); - private Edm edm; - private OData server; +// private Edm edm; +// private OData server; + private ODataHandler handler; public ODataHttpHandlerImpl(final OData server, final Edm edm) { - this.edm = edm; - this.server = server; +// this.edm = edm; +// this.server = server; + handler = new ODataHandler(server, edm); } @Override public void process(final HttpServletRequest request, final HttpServletResponse response) { ODataRequest odRequest = createODataRequest(request, 0); - - ODataHandler handler = new ODataHandler(server, edm); + ODataResponse odResponse = handler.process(odRequest); convertToHttp(response, odResponse); @@ -199,4 +203,9 @@ public class ODataHttpHandlerImpl implements ODataHttpHandler { } odRequest.setHeaders(requestHeaders); } + + @Override + public void register(Processor processor) { + handler.register(processor); + } } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/a9ed4655/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataRequest.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataRequest.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataRequest.java deleted file mode 100644 index 2a943e4..0000000 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataRequest.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * 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.server.core; - -import java.io.InputStream; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.apache.olingo.commons.api.http.HttpMethod; - -public class ODataRequest { - private HttpMethod method; - private Map<String, List<String>> headers = new HashMap<String, List<String>>(); - private InputStream body; - private String rawQueryPath; - private String rawRequestUri; - private String rawODataPath; - private String rawBaseUri; - private String rawServiceResolutionUri; - - public HttpMethod getMethod() { - return method; - } - - public void setMethod(final HttpMethod method) { - this.method = method; - } - - public Map<String, List<String>> getHeaders() { - return Collections.unmodifiableMap(headers); - } - - public void setHeaders(final Map<String, List<String>> headers) { - this.headers = headers; - } - - public InputStream getBody() { - return body; - } - - public void setBody(final InputStream body) { - this.body = body; - } - - public String getRawQueryPath() { - return rawQueryPath; - } - - public void setRawQueryPath(String rawQueryPath) { - this.rawQueryPath = rawQueryPath; - } - - public String getRawBaseUri() { - return rawBaseUri; - } - - public String getRawRequestUri() { - return rawRequestUri; - } - - public String getRawODataPath() { - return rawODataPath; - } - - public void setRawRequestUri(String rawRequestUri) { - this.rawRequestUri = rawRequestUri; - } - - public void setRawODataPath(String rawODataPath) { - this.rawODataPath = rawODataPath; - - } - - public void setRawBaseUri(String rawBaseUri) { - this.rawBaseUri = rawBaseUri; - } - - public String getRawServiceResolutionUri() { - return rawServiceResolutionUri; - } - - public void setRawServiceResolutionUri(String rawServiceResolutionUri) { - this.rawServiceResolutionUri = rawServiceResolutionUri; - } -} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/a9ed4655/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataResponse.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataResponse.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataResponse.java deleted file mode 100644 index 79c3ac8..0000000 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataResponse.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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.server.core; - -import java.io.InputStream; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -public class ODataResponse { - - private int statusCode; - private Map<String, String> headers = new HashMap<String, String>(); - private InputStream content; - - public void setStatusCode(final int statusCode) { - this.statusCode = statusCode; - } - - public void setHeader(final String name, final String value) { - headers.put(name, value); - } - - public void setContent(final InputStream content) { - this.content = content; - } - - public int getStatusCode() { - return statusCode; - } - - public Map<String, String> getHeaders() { - return Collections.unmodifiableMap(headers); - } - - public InputStream getContent() { - return content; - } - -} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/a9ed4655/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/validator/UriValidator.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/validator/UriValidator.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/validator/UriValidator.java index 295a130..9e678fc 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/validator/UriValidator.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/validator/UriValidator.java @@ -33,6 +33,7 @@ import org.apache.olingo.commons.api.edm.EdmReturnType; import org.apache.olingo.commons.api.edm.EdmSingleton; import org.apache.olingo.commons.api.edm.EdmType; import org.apache.olingo.commons.api.edm.constants.EdmTypeKind; +import org.apache.olingo.commons.api.http.HttpMethod; import org.apache.olingo.server.api.uri.UriInfo; import org.apache.olingo.server.api.uri.UriParameter; import org.apache.olingo.server.api.uri.UriResource; @@ -172,7 +173,7 @@ public class UriValidator { super(); } - public void validate(final UriInfo uriInfo, final String httpMethod) throws UriValidationException { + public void validate(final UriInfo uriInfo, final HttpMethod httpMethod) throws UriValidationException { validateForHttpMethod(uriInfo, httpMethod); validateQueryOptions(uriInfo); validateKeyPredicateTypes(uriInfo); @@ -554,7 +555,7 @@ public class UriValidator { } - private void validateForHttpMethod(final UriInfo uriInfo, final String httpMethod) throws UriValidationException { + private void validateForHttpMethod(final UriInfo uriInfo, final HttpMethod httpMethod) throws UriValidationException { RowIndexForHttpMethod row = rowIndexForHttpMethod(httpMethod); for (SystemQueryOption option : uriInfo.getSystemQueryOptions()) { @@ -567,22 +568,29 @@ public class UriValidator { } - private RowIndexForHttpMethod rowIndexForHttpMethod(final String httpMethod) throws UriValidationException { + private RowIndexForHttpMethod rowIndexForHttpMethod(final HttpMethod httpMethod) throws UriValidationException { RowIndexForHttpMethod idx; - if ("GET".equalsIgnoreCase(httpMethod)) { + switch (httpMethod) { + case GET: idx = RowIndexForHttpMethod.GET; - } else if ("POST".equalsIgnoreCase(httpMethod)) { + break; + case POST: idx = RowIndexForHttpMethod.POST; - } else if ("PUT".equalsIgnoreCase(httpMethod)) { + break; + case PUT: idx = RowIndexForHttpMethod.PUT; - } else if ("DELETE".equalsIgnoreCase(httpMethod)) { + break; + case DELETE: idx = RowIndexForHttpMethod.DELETE; - } else if ("PATCH".equalsIgnoreCase(httpMethod)) { + break; + case PATCH: idx = RowIndexForHttpMethod.PATCH; - } else if ("MERGE".equalsIgnoreCase(httpMethod)) { + break; + case MERGE: idx = RowIndexForHttpMethod.MERGE; - } else { + break; + default: throw new UriValidationException("HTTP method not supported: " + httpMethod); } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/a9ed4655/lib/server-core/src/test/java/org/apache/olingo/server/core/ODataHttpHandlerImplTest.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/ODataHttpHandlerImplTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/ODataHttpHandlerImplTest.java index 2bc24e1..faa82e3 100644 --- a/lib/server-core/src/test/java/org/apache/olingo/server/core/ODataHttpHandlerImplTest.java +++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/ODataHttpHandlerImplTest.java @@ -27,6 +27,7 @@ import javax.servlet.http.HttpServletRequest; import org.apache.olingo.commons.api.ODataRuntimeException; import org.apache.olingo.commons.api.http.HttpMethod; +import org.apache.olingo.server.api.ODataRequest; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/a9ed4655/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/TechnicalProcessor.java ---------------------------------------------------------------------- diff --git a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/TechnicalProcessor.java b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/TechnicalProcessor.java new file mode 100644 index 0000000..7f05862 --- /dev/null +++ b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/TechnicalProcessor.java @@ -0,0 +1,39 @@ +/* + * 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.server.tecsvc; + +import org.apache.olingo.commons.api.edm.Edm; +import org.apache.olingo.server.api.OData; +import org.apache.olingo.server.api.processor.Processor; + +public class TechnicalProcessor implements Processor { + + private OData odata; + private Edm edm; + + + @Override + public void init(OData odata, Edm edm) { + this.odata = odata; + this.edm = edm; + } + + + +} http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/a9ed4655/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/TechnicalServlet.java ---------------------------------------------------------------------- diff --git a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/TechnicalServlet.java b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/TechnicalServlet.java index 5ce5a84..f49cd99 100644 --- a/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/TechnicalServlet.java +++ b/lib/server-tecsvc/src/main/java/org/apache/olingo/server/tecsvc/TechnicalServlet.java @@ -46,6 +46,12 @@ public class TechnicalServlet extends HttpServlet { Edm edm = odata.createEdm(new EdmTechProvider()); ODataHttpHandler handler = odata.createHandler(edm); + + handler.register(new TechnicalProcessor()); + +// handler.registerServiceDocumentProcessor(new TechnicalProcessor()); +// handler.registerMetadataProcessor(new TechnicalProcessor()); + handler.process(req, resp); } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/a9ed4655/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerTest.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerTest.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerTest.java index 18400d8..1b7c891 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerTest.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/ODataHandlerTest.java @@ -21,11 +21,17 @@ package org.apache.olingo.server.core; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.withSettings; import org.apache.commons.io.IOUtils; import org.apache.olingo.commons.api.edm.Edm; import org.apache.olingo.commons.api.http.HttpMethod; import org.apache.olingo.server.api.OData; +import org.apache.olingo.server.api.ODataRequest; +import org.apache.olingo.server.api.ODataResponse; +import org.apache.olingo.server.api.processor.DeleteMeProcessor; +import org.apache.olingo.server.api.processor.Processor; import org.apache.olingo.server.tecsvc.provider.EdmTechProvider; import org.junit.Before; import org.junit.Test; @@ -49,9 +55,12 @@ public class ODataHandlerTest { request.setMethod(HttpMethod.GET); request.setRawBaseUri("http://localhost/odata/"); request.setRawODataPath(""); + + Processor processor = mock(Processor.class, withSettings().extraInterfaces(DeleteMeProcessor.class)); + handler.register(processor); ODataResponse response = handler.process(request); - + assertNotNull(response); assertEquals(200, response.getStatusCode()); assertEquals("application/json", response.getHeaders().get("Content-Type")); @@ -70,6 +79,9 @@ public class ODataHandlerTest { request.setMethod(HttpMethod.GET); request.setRawODataPath("$metadata"); + Processor processor = mock(Processor.class, withSettings().extraInterfaces(DeleteMeProcessor.class)); + handler.register(processor); + ODataResponse response = handler.process(request); assertNotNull(response); @@ -80,7 +92,6 @@ public class ODataHandlerTest { String doc = IOUtils.toString(response.getContent()); assertTrue(doc.contains("<edmx:Edmx Version=\"4.0\"")); - } } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/a9ed4655/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/ResourceValidator.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/ResourceValidator.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/ResourceValidator.java index 143871a..0eadd59 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/ResourceValidator.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/ResourceValidator.java @@ -29,6 +29,7 @@ import org.apache.olingo.commons.api.edm.Edm; import org.apache.olingo.commons.api.edm.EdmElement; import org.apache.olingo.commons.api.edm.EdmType; import org.apache.olingo.commons.api.edm.FullQualifiedName; +import org.apache.olingo.commons.api.http.HttpMethod; import org.apache.olingo.server.api.uri.UriInfo; import org.apache.olingo.server.api.uri.UriInfoKind; import org.apache.olingo.server.api.uri.UriParameter; @@ -92,7 +93,7 @@ public class ResourceValidator implements TestValidator { uriInfoTmp = (UriInfoImpl) testParser.parseUri(uri, edm); UriValidator uriValidator = new UriValidator(); - uriValidator.validate(uriInfoTmp, "GET"); + uriValidator.validate(uriInfoTmp, HttpMethod.GET); } catch (Exception e) { fail("Exception occured while parsing the URI: " + uri + "\n" + " Message: " + e.getMessage()); http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/a9ed4655/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/TestUriValidator.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/TestUriValidator.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/TestUriValidator.java index 35687f6..47dd0ba 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/TestUriValidator.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/testutil/TestUriValidator.java @@ -27,6 +27,7 @@ import org.apache.olingo.commons.api.edm.Edm; import org.apache.olingo.commons.api.edm.EdmEntityType; import org.apache.olingo.commons.api.edm.EdmType; import org.apache.olingo.commons.api.edm.FullQualifiedName; +import org.apache.olingo.commons.api.http.HttpMethod; import org.apache.olingo.server.api.uri.UriInfoKind; import org.apache.olingo.server.api.uri.queryoption.CustomQueryOption; import org.apache.olingo.server.api.uri.queryoption.SelectItem; @@ -61,7 +62,7 @@ public class TestUriValidator implements TestValidator { uriInfo = null; try { uriInfo = (UriInfoImpl) parser.parseUri(uri, edm); - validator.validate(uriInfo, "GET"); + validator.validate(uriInfo, HttpMethod.GET); } catch (Exception e) { throw new RuntimeException(e); } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/a9ed4655/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/validator/UriValidatorTest.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/validator/UriValidatorTest.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/validator/UriValidatorTest.java index 9787fab..ce087b2 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/validator/UriValidatorTest.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/validator/UriValidatorTest.java @@ -24,6 +24,7 @@ import static org.junit.Assert.fail; import java.util.ArrayList; import org.apache.olingo.commons.api.edm.Edm; +import org.apache.olingo.commons.api.http.HttpMethod; import org.apache.olingo.server.api.uri.UriInfo; import org.apache.olingo.server.core.edm.provider.EdmProviderImpl; import org.apache.olingo.server.core.uri.parser.Parser; @@ -269,32 +270,26 @@ public class UriValidatorTest { public void validateSelect() throws Exception { String[] uris = { "/ESAllPrim(1)?$select=PropertyString" }; for (String uri : uris) { - parseAndValidate(uri, "GET"); + parseAndValidate(uri, HttpMethod.GET); } } - @Test(expected = UriValidationException.class) - public void validateForHttpMethodsFail() throws Exception { - String uri = URI_ENTITY; - parseAndValidate(uri, "xyz"); - } - @Test public void validateForHttpMethods() throws Exception { String uri = URI_ENTITY; - parseAndValidate(uri, "GET"); - parseAndValidate(uri, "POST"); - parseAndValidate(uri, "PUT"); - parseAndValidate(uri, "DELETE"); - parseAndValidate(uri, "PATCH"); - parseAndValidate(uri, "MERGE"); + parseAndValidate(uri, HttpMethod.GET); + parseAndValidate(uri, HttpMethod.POST); + parseAndValidate(uri, HttpMethod.PUT); + parseAndValidate(uri, HttpMethod.DELETE); + parseAndValidate(uri, HttpMethod.PATCH); + parseAndValidate(uri, HttpMethod.MERGE); } @Test public void validateOrderBy() throws Exception { String[] uris = { "/ESAllPrim?$orderby=PropertyString" }; for (String uri : uris) { - parseAndValidate(uri, "GET"); + parseAndValidate(uri, HttpMethod.GET); } } @@ -302,25 +297,25 @@ public class UriValidatorTest { @Ignore("uri parser doen't support orderby yet") public void validateOrderByInvalid() throws Exception { String uri = "/ESAllPrim(1)?$orderBy=XXXX"; - parseAndValidate(uri, "GET"); + parseAndValidate(uri, HttpMethod.GET); } @Test(expected = UriValidationException.class) public void validateKeyPredicatesWrongKey() throws Exception { String uri = "ESTwoKeyNav(xxx=1, yyy='abc')"; - parseAndValidate(uri, "GET"); + parseAndValidate(uri, HttpMethod.GET); } @Test public void validateKeyPredicates() throws Exception { String uri = "ESTwoKeyNav(PropertyInt16=1, PropertyString='abc')"; - parseAndValidate(uri, "GET"); + parseAndValidate(uri, HttpMethod.GET); } @Test(expected = UriValidationException.class) public void validateKeyPredicatesWrongValueType() throws Exception { String uri = "ESTwoKeyNav(PropertyInt16='abc', PropertyString=1)"; - parseAndValidate(uri, "GET"); + parseAndValidate(uri, HttpMethod.GET); } @Test @@ -329,7 +324,7 @@ public class UriValidatorTest { for (String uri : uris) { try { - parseAndValidate(uri, "GET"); + parseAndValidate(uri, HttpMethod.GET); } catch (Exception e) { throw new Exception("Faild for uri: " + uri, e); } @@ -342,7 +337,7 @@ public class UriValidatorTest { for (String uri : uris) { try { - parseAndValidate(uri, "GET"); + parseAndValidate(uri, HttpMethod.GET); fail("Validation Exception not thrown: " + uri); } catch (UriValidationException e) { assertTrue(e instanceof UriValidationException); @@ -368,7 +363,7 @@ public class UriValidatorTest { return uris.toArray(new String[0]); } - private void parseAndValidate(final String uri, String method) throws UriParserException, UriValidationException { + private void parseAndValidate(final String uri, HttpMethod method) throws UriParserException, UriValidationException { UriInfo uriInfo = parser.parseUri(uri.trim(), edm); UriValidator validator = new UriValidator();
