Skip to content

Commit

Permalink
refactor: Redefine server request / response
Browse files Browse the repository at this point in the history
  • Loading branch information
brasseld committed Jun 12, 2015
1 parent fe665d6 commit 4aeba35
Show file tree
Hide file tree
Showing 14 changed files with 174 additions and 56 deletions.
11 changes: 4 additions & 7 deletions common/src/main/java/io/gravitee/common/http/HttpMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,11 @@
package io.gravitee.common.http;

/**
* Represents an HTTP method.
*
* @author David BRASSELY (brasseld at gmail.com)
*/
public interface HttpMethod {
public enum HttpMethod {

String OPTIONS = "OPTIONS";
String GET = "GET";
String POST = "POST";
String PUT = "PUT";
String DELETE = "DELETE";
String PATCH = "PATCH";
CONNECT, DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT, TRACE;
}
11 changes: 11 additions & 0 deletions common/src/main/java/io/gravitee/common/http/HttpVersion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.gravitee.common.http;

/**
* Represents the version of the HTTP protocol.
*
* @author David BRASSELY (brasseld at gmail.com)
*/
public enum HttpVersion {

HTTP_1_0, HTTP_1_1
}
6 changes: 6 additions & 0 deletions gateway/api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,11 @@
<artifactId>model</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>io.gravitee</groupId>
<artifactId>common</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
31 changes: 28 additions & 3 deletions gateway/api/src/main/java/io/gravitee/gateway/api/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,45 @@
*/
package io.gravitee.gateway.api;

import io.gravitee.common.http.HttpMethod;
import io.gravitee.common.http.HttpVersion;

import java.util.Map;

/**
* Represents a server-side HTTP request.
*
* @author David BRASSELY (brasseld at gmail.com)
*/
public interface Request {

String id();

String requestURI();
/**
* @return the URI of the request. This is usually a relative URI.
*/
String uri();

/**
* @return the query parameters in the request
*/
Map<String, String> parameters();

/**
* @return the headers in the request.
*/
Map<String, String> headers();

Map<String, String> queryParameters();
/**
* @return the HTTP method for the request.
*/
HttpMethod method();

String method();
/**
* @return the HTTP version of the request
*/
HttpVersion version();

//TODO: Shouldn't be manage like this... not the best way...
boolean hasContent();
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ protected AbstractHttpClient(Api api) {

protected String rewriteTarget(Request request) {
StringBuffer requestURI =
new StringBuffer(request.requestURI())
new StringBuffer(request.uri())
.delete(0, api.getPublicURI().getPath().length())
.insert(0, api.getTargetURI().toString());

if (request.queryParameters() != null && ! request.queryParameters().isEmpty()) {
if (request.parameters() != null && ! request.parameters().isEmpty()) {
requestURI.append('?');

for(Map.Entry<String, String> queryParam : request.queryParameters().entrySet()) {
for(Map.Entry<String, String> queryParam : request.parameters().entrySet()) {
requestURI.append(queryParam.getKey()).append('=').append(queryParam.getValue()).append('&');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import io.gravitee.gateway.api.Response;
import io.gravitee.gateway.core.components.client.AbstractHttpClient;
import io.gravitee.gateway.core.http.ContentRequest;
import io.gravitee.gateway.core.http.DefaultRequest;
import io.gravitee.gateway.core.http.DefaultResponse;
import io.gravitee.gateway.core.http.ServerRequest;
import io.gravitee.gateway.core.http.ServerResponse;
import io.gravitee.model.Api;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.Result;
Expand Down Expand Up @@ -60,17 +60,17 @@ public Observable<Response> invoke(final Request request) {
@Override
public void call(final Subscriber<? super Response> observer) {
URI rewrittenURI = rewriteURI(request);
LOGGER.debug("{} rewriting: {} -> {}", request.id(), request.requestURI(), rewrittenURI);
LOGGER.debug("{} rewriting: {} -> {}", request.id(), request.uri(), rewrittenURI);

org.eclipse.jetty.client.api.Request proxyRequest = client
.newRequest(rewrittenURI)
.method(request.method());
.method(request.method().name());

if (request.hasContent()) {
proxyRequest.content(new ProxyInputStreamContentProvider(proxyRequest, (ContentRequest) request));
}

proxyRequest.send(new ProxyResponseListener(request, new DefaultResponse(), observer));
proxyRequest.send(new ProxyResponseListener(request, new ServerResponse(), observer));
}
}
);
Expand All @@ -83,10 +83,10 @@ protected URI rewriteURI(Request request) {

protected class ProxyResponseListener extends org.eclipse.jetty.client.api.Response.Listener.Adapter {
private final Request request;
private final DefaultResponse response;
private final ServerResponse response;
private final Subscriber<? super Response> observer;

protected ProxyResponseListener(Request request, DefaultResponse response, Subscriber<? super Response> observer) {
protected ProxyResponseListener(Request request, ServerResponse response, Subscriber<? super Response> observer) {
this.request = request;
this.response = response;
this.observer = observer;
Expand Down Expand Up @@ -148,7 +148,7 @@ protected void onProxyResponseSuccess(Request clientRequest, Response proxyRespo
observer.onNext(proxyResponse);
}

protected void onProxyResponseFailure(Request clientRequest, DefaultResponse proxyResponse, org.eclipse.jetty.client.api.Response serverResponse, Throwable failure, Subscriber<? super Response> observer) {
protected void onProxyResponseFailure(Request clientRequest, ServerResponse proxyResponse, org.eclipse.jetty.client.api.Response serverResponse, Throwable failure, Subscriber<? super Response> observer) {
LOGGER.debug(clientRequest.id() + " proxying failed", failure);

if (failure instanceof TimeoutException)
Expand All @@ -161,7 +161,7 @@ protected void onProxyResponseFailure(Request clientRequest, DefaultResponse pro
observer.onNext(proxyResponse);
}

protected void onResponseContent(Request request, DefaultResponse response, org.eclipse.jetty.client.api.Response proxyResponse, byte[] buffer, int offset, int length, Callback callback) {
protected void onResponseContent(Request request, ServerResponse response, org.eclipse.jetty.client.api.Response proxyResponse, byte[] buffer, int offset, int length, Callback callback) {
try {
LOGGER.debug("{} proxying content to downstream: {} bytes", request.id(), length);
response.getOutputStream().write(buffer, offset, length);
Expand All @@ -171,7 +171,7 @@ protected void onResponseContent(Request request, DefaultResponse response, org.
}
}

protected void onServerResponseHeaders(DefaultResponse proxyResponse, org.eclipse.jetty.client.api.Response serverResponse) {
protected void onServerResponseHeaders(ServerResponse proxyResponse, org.eclipse.jetty.client.api.Response serverResponse) {
for (HttpField field : serverResponse.getHeaders()) {
String headerName = field.getName();
String lowerHeaderName = headerName.toLowerCase(Locale.ENGLISH);
Expand Down Expand Up @@ -217,7 +217,7 @@ protected void onReadFailure(Throwable failure) {
}
}

protected void onClientRequestFailure(org.eclipse.jetty.client.api.Request proxyRequest, DefaultRequest request, Throwable failure) {
protected void onClientRequestFailure(org.eclipse.jetty.client.api.Request proxyRequest, ServerRequest request, Throwable failure) {
LOGGER.debug(request.id() + " client request failure", failure);
proxyRequest.abort(failure);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
*
* @author David BRASSELY (brasseld at gmail.com)
*/
public class ContentRequest extends DefaultRequest {
public class ContentRequest extends ServerRequest {

private final InputStream inputStream;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package io.gravitee.gateway.core.http;

import io.gravitee.common.http.HttpMethod;
import io.gravitee.common.http.HttpVersion;
import io.gravitee.gateway.api.Request;

import java.util.HashMap;
Expand All @@ -26,23 +28,25 @@
*
* @author David BRASSELY (brasseld at gmail.com)
*/
public class DefaultRequest implements Request {
public class ServerRequest implements Request {

private final String id;

private String remoteAddr;

private String method;
private HttpMethod method;

private String requestURI;

private boolean secure;

private HttpVersion version;

private Map<String, String> queryParameters = new LinkedHashMap();

private Map<String, String> headers = new HashMap();

public DefaultRequest() {
public ServerRequest() {
this.id = UUID.randomUUID().toString();
}

Expand All @@ -54,15 +58,20 @@ public void setRemoteAddr(String remoteAddr) {
this.remoteAddr = remoteAddr;
}

public String method() {
public HttpMethod method() {
return method;
}

public void setMethod(String method) {
@Override
public HttpVersion version() {
return version;
}

public void setMethod(HttpMethod method) {
this.method = method;
}

public String requestURI() {
public String uri() {
return requestURI;
}

Expand All @@ -78,22 +87,15 @@ public void setSecure(boolean secure) {
this.secure = secure;
}

public Map<String, String> queryParameters() {
public Map<String, String> parameters() {
return queryParameters;
}

public void setQueryParameters(Map<String, String> queryParameters) {
this.queryParameters = queryParameters;
}

public Map<String, String> getHeaders() {
@Override
public Map<String, String> headers() {
return headers;
}

public void setHeaders(Map<String, String> headers) {
this.headers = headers;
}

public String id() {
return id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*
* @author David BRASSELY (brasseld at gmail.com)
*/
public class DefaultResponse implements Response {
public class ServerResponse implements Response {

private int status;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import io.gravitee.gateway.core.components.client.HttpClientFactory;
import io.gravitee.gateway.core.components.client.jetty.JettyHttpClientFactory;
import io.gravitee.gateway.core.Reactor;
import io.gravitee.gateway.core.http.DefaultResponse;
import io.gravitee.gateway.core.http.ServerResponse;
import io.gravitee.model.Api;
import org.springframework.beans.factory.annotation.Autowired;
import rx.Observable;
Expand All @@ -41,11 +41,11 @@ public class DefaultReactor implements Reactor {
@Override
public Observable<Response> process(Request request) {
// TODO: get the associated API / service from the request using the registry
Api api = registry.findMatchingApi(request.requestURI());
Api api = registry.findMatchingApi(request.uri());

if (api == null) {
// Not found -> 404
DefaultResponse response = new DefaultResponse();
ServerResponse response = new ServerResponse();
response.setStatus(HttpStatusCode.NOT_FOUND_404);

return Observable.just((Response)response);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* Licensed 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 io.gravitee.gateway.core.policy;

import io.gravitee.gateway.api.PolicyChain;

/**
* @author David BRASSELY (brasseld at gmail.com)
*/
public interface PolicyChainBuilder<T extends PolicyChain> {

T newPolicyChain();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* Licensed 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 io.gravitee.gateway.core.policy.rules;

import io.gravitee.gateway.api.PolicyChain;
import io.gravitee.gateway.api.Request;
import io.gravitee.gateway.core.policy.PolicyAdapter;

/**
* @author David BRASSELY (brasseld at gmail.com)
*/
public class AccessControlPolicy extends PolicyAdapter {

@Override
public void apply(Request request, PolicyChain<Request> handler) {
super.apply(request, handler);
}

@Override
public String name() {
return "Access Control Policy";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.gravitee.gateway.core.policy.rules;

import io.gravitee.gateway.core.policy.PolicyAdapter;

/**
* @author David BRASSELY (brasseld at gmail.com)
*/
public class TransformPolicy extends PolicyAdapter {

@Override
public String name() {
return "Transform Policy";
}
}
Loading

0 comments on commit 4aeba35

Please sign in to comment.