move RestRequest to be an abstract class, and expose local/remote address
This commit is contained in:
parent
8686ffe761
commit
bc0909b232
|
@ -24,6 +24,6 @@ import org.elasticsearch.rest.RestRequest;
|
|||
/**
|
||||
*
|
||||
*/
|
||||
public interface HttpRequest extends RestRequest {
|
||||
public abstract class HttpRequest extends RestRequest {
|
||||
|
||||
}
|
||||
|
|
|
@ -40,7 +40,7 @@ public class HttpRequestHandler extends SimpleChannelUpstreamHandler {
|
|||
HttpRequest request = (HttpRequest) e.getMessage();
|
||||
// the netty HTTP handling always copy over the buffer to its own buffer, either in NioWorker internally
|
||||
// when reading, or using a cumalation buffer
|
||||
serverTransport.dispatchRequest(new NettyHttpRequest(request), new NettyHttpChannel(serverTransport, e.getChannel(), request));
|
||||
serverTransport.dispatchRequest(new NettyHttpRequest(request, e.getChannel()), new NettyHttpChannel(serverTransport, e.getChannel(), request));
|
||||
super.messageReceived(ctx, e);
|
||||
}
|
||||
|
||||
|
|
|
@ -23,28 +23,28 @@ import org.elasticsearch.common.bytes.BytesArray;
|
|||
import org.elasticsearch.common.bytes.BytesReference;
|
||||
import org.elasticsearch.common.bytes.ChannelBufferBytesReference;
|
||||
import org.elasticsearch.http.HttpRequest;
|
||||
import org.elasticsearch.rest.support.AbstractRestRequest;
|
||||
import org.elasticsearch.rest.support.RestUtils;
|
||||
import org.jboss.netty.channel.Channel;
|
||||
import org.jboss.netty.handler.codec.http.HttpMethod;
|
||||
|
||||
import java.net.SocketAddress;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class NettyHttpRequest extends AbstractRestRequest implements HttpRequest {
|
||||
public class NettyHttpRequest extends HttpRequest {
|
||||
|
||||
private final org.jboss.netty.handler.codec.http.HttpRequest request;
|
||||
|
||||
private final Channel channel;
|
||||
private final Map<String, String> params;
|
||||
|
||||
private final String rawPath;
|
||||
|
||||
private final BytesReference content;
|
||||
|
||||
public NettyHttpRequest(org.jboss.netty.handler.codec.http.HttpRequest request) {
|
||||
public NettyHttpRequest(org.jboss.netty.handler.codec.http.HttpRequest request, Channel channel) {
|
||||
this.request = request;
|
||||
this.channel = channel;
|
||||
this.params = new HashMap<String, String>();
|
||||
if (request.getContent().readable()) {
|
||||
this.content = new ChannelBufferBytesReference(request.getContent());
|
||||
|
@ -119,11 +119,38 @@ public class NettyHttpRequest extends AbstractRestRequest implements HttpRequest
|
|||
return content;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the remote address where this rest request channel is "connected to". The
|
||||
* returned {@link SocketAddress} is supposed to be down-cast into more
|
||||
* concrete type such as {@link java.net.InetSocketAddress} to retrieve
|
||||
* the detailed information.
|
||||
*/
|
||||
@Override
|
||||
public SocketAddress getRemoteAddress() {
|
||||
return channel.getRemoteAddress();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the local address where this request channel is bound to. The returned
|
||||
* {@link SocketAddress} is supposed to be down-cast into more concrete
|
||||
* type such as {@link java.net.InetSocketAddress} to retrieve the detailed
|
||||
* information.
|
||||
*/
|
||||
@Override
|
||||
public SocketAddress getLocalAddress() {
|
||||
return channel.getLocalAddress();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String header(String name) {
|
||||
return request.headers().get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<Map.Entry<String, String>> headers() {
|
||||
return request.headers().entries();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasParam(String key) {
|
||||
return params.containsKey(key);
|
||||
|
@ -142,10 +169,4 @@ public class NettyHttpRequest extends AbstractRestRequest implements HttpRequest
|
|||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<Map.Entry<String, String>> headers() {
|
||||
return request.headers().entries();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -19,71 +19,143 @@
|
|||
|
||||
package org.elasticsearch.rest;
|
||||
|
||||
import org.elasticsearch.ElasticsearchIllegalArgumentException;
|
||||
import org.elasticsearch.common.Booleans;
|
||||
import org.elasticsearch.common.Nullable;
|
||||
import org.elasticsearch.common.Strings;
|
||||
import org.elasticsearch.common.bytes.BytesReference;
|
||||
import org.elasticsearch.common.unit.ByteSizeValue;
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.common.xcontent.ToXContent;
|
||||
import org.elasticsearch.rest.support.RestUtils;
|
||||
|
||||
import java.net.SocketAddress;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.elasticsearch.common.unit.ByteSizeValue.parseBytesSizeValue;
|
||||
import static org.elasticsearch.common.unit.TimeValue.parseTimeValue;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public interface RestRequest extends ToXContent.Params {
|
||||
public abstract class RestRequest implements ToXContent.Params {
|
||||
|
||||
enum Method {
|
||||
public enum Method {
|
||||
GET, POST, PUT, DELETE, OPTIONS, HEAD
|
||||
}
|
||||
|
||||
Method method();
|
||||
public abstract Method method();
|
||||
|
||||
/**
|
||||
* The uri of the rest request, with the query string.
|
||||
*/
|
||||
String uri();
|
||||
public abstract String uri();
|
||||
|
||||
/**
|
||||
* The non decoded, raw path provided.
|
||||
*/
|
||||
String rawPath();
|
||||
public abstract String rawPath();
|
||||
|
||||
/**
|
||||
* The path part of the URI (without the query string), decoded.
|
||||
*/
|
||||
String path();
|
||||
public final String path() {
|
||||
return RestUtils.decodeComponent(rawPath());
|
||||
}
|
||||
|
||||
boolean hasContent();
|
||||
public abstract boolean hasContent();
|
||||
|
||||
/**
|
||||
* Is the byte array content safe or unsafe for usage on other threads
|
||||
*/
|
||||
boolean contentUnsafe();
|
||||
public abstract boolean contentUnsafe();
|
||||
|
||||
BytesReference content();
|
||||
public abstract BytesReference content();
|
||||
|
||||
String header(String name);
|
||||
public abstract String header(String name);
|
||||
|
||||
boolean hasParam(String key);
|
||||
public abstract Iterable<Map.Entry<String, String>> headers();
|
||||
|
||||
String param(String key);
|
||||
@Nullable
|
||||
public SocketAddress getRemoteAddress() {
|
||||
return null;
|
||||
}
|
||||
|
||||
String[] paramAsStringArray(String key, String[] defaultValue);
|
||||
@Nullable
|
||||
public SocketAddress getLocalAddress() {
|
||||
return null;
|
||||
}
|
||||
|
||||
float paramAsFloat(String key, float defaultValue);
|
||||
public abstract boolean hasParam(String key);
|
||||
|
||||
int paramAsInt(String key, int defaultValue);
|
||||
@Override
|
||||
public abstract String param(String key);
|
||||
|
||||
long paramAsLong(String key, long defaultValue);
|
||||
public abstract Map<String, String> params();
|
||||
|
||||
boolean paramAsBoolean(String key, boolean defaultValue);
|
||||
public float paramAsFloat(String key, float defaultValue) {
|
||||
String sValue = param(key);
|
||||
if (sValue == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
return Float.parseFloat(sValue);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new ElasticsearchIllegalArgumentException("Failed to parse float parameter [" + key + "] with value [" + sValue + "]", e);
|
||||
}
|
||||
}
|
||||
|
||||
Boolean paramAsBooleanOptional(String key, Boolean defaultValue);
|
||||
public int paramAsInt(String key, int defaultValue) {
|
||||
String sValue = param(key);
|
||||
if (sValue == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
return Integer.parseInt(sValue);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new ElasticsearchIllegalArgumentException("Failed to parse int parameter [" + key + "] with value [" + sValue + "]", e);
|
||||
}
|
||||
}
|
||||
|
||||
TimeValue paramAsTime(String key, TimeValue defaultValue);
|
||||
public long paramAsLong(String key, long defaultValue) {
|
||||
String sValue = param(key);
|
||||
if (sValue == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
return Long.parseLong(sValue);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new ElasticsearchIllegalArgumentException("Failed to parse int parameter [" + key + "] with value [" + sValue + "]", e);
|
||||
}
|
||||
}
|
||||
|
||||
ByteSizeValue paramAsSize(String key, ByteSizeValue defaultValue);
|
||||
@Override
|
||||
public boolean paramAsBoolean(String key, boolean defaultValue) {
|
||||
return Booleans.parseBoolean(param(key), defaultValue);
|
||||
}
|
||||
|
||||
Map<String, String> params();
|
||||
@Override
|
||||
public Boolean paramAsBooleanOptional(String key, Boolean defaultValue) {
|
||||
String sValue = param(key);
|
||||
if (sValue == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
return !(sValue.equals("false") || sValue.equals("0") || sValue.equals("off"));
|
||||
}
|
||||
|
||||
Iterable<Map.Entry<String, String>> headers();
|
||||
public TimeValue paramAsTime(String key, TimeValue defaultValue) {
|
||||
return parseTimeValue(param(key), defaultValue);
|
||||
}
|
||||
|
||||
public ByteSizeValue paramAsSize(String key, ByteSizeValue defaultValue) {
|
||||
return parseBytesSizeValue(param(key), defaultValue);
|
||||
}
|
||||
|
||||
public String[] paramAsStringArray(String key, String[] defaultValue) {
|
||||
String value = param(key);
|
||||
if (value == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
return Strings.splitStringByCommaToArray(value);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,113 +0,0 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch 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.elasticsearch.rest.support;
|
||||
|
||||
import org.elasticsearch.ElasticsearchIllegalArgumentException;
|
||||
import org.elasticsearch.common.Booleans;
|
||||
import org.elasticsearch.common.Strings;
|
||||
import org.elasticsearch.common.unit.ByteSizeValue;
|
||||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.rest.RestRequest;
|
||||
|
||||
import static org.elasticsearch.common.unit.ByteSizeValue.parseBytesSizeValue;
|
||||
import static org.elasticsearch.common.unit.TimeValue.parseTimeValue;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public abstract class AbstractRestRequest implements RestRequest {
|
||||
|
||||
@Override
|
||||
public final String path() {
|
||||
return RestUtils.decodeComponent(rawPath());
|
||||
}
|
||||
|
||||
@Override
|
||||
public float paramAsFloat(String key, float defaultValue) {
|
||||
String sValue = param(key);
|
||||
if (sValue == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
return Float.parseFloat(sValue);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new ElasticsearchIllegalArgumentException("Failed to parse float parameter [" + key + "] with value [" + sValue + "]", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int paramAsInt(String key, int defaultValue) {
|
||||
String sValue = param(key);
|
||||
if (sValue == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
return Integer.parseInt(sValue);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new ElasticsearchIllegalArgumentException("Failed to parse int parameter [" + key + "] with value [" + sValue + "]", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long paramAsLong(String key, long defaultValue) {
|
||||
String sValue = param(key);
|
||||
if (sValue == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
try {
|
||||
return Long.parseLong(sValue);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new ElasticsearchIllegalArgumentException("Failed to parse int parameter [" + key + "] with value [" + sValue + "]", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean paramAsBoolean(String key, boolean defaultValue) {
|
||||
return Booleans.parseBoolean(param(key), defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean paramAsBooleanOptional(String key, Boolean defaultValue) {
|
||||
String sValue = param(key);
|
||||
if (sValue == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
return !(sValue.equals("false") || sValue.equals("0") || sValue.equals("off"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public TimeValue paramAsTime(String key, TimeValue defaultValue) {
|
||||
return parseTimeValue(param(key), defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ByteSizeValue paramAsSize(String key, ByteSizeValue defaultValue) {
|
||||
return parseBytesSizeValue(param(key), defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] paramAsStringArray(String key, String[] defaultValue) {
|
||||
String value = param(key);
|
||||
if (value == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
return Strings.splitStringByCommaToArray(value);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue