001package org.xbib.elasticsearch.http.netty;
002
003import org.elasticsearch.ElasticsearchIllegalArgumentException;
004import org.elasticsearch.common.bytes.BytesArray;
005import org.elasticsearch.common.bytes.BytesReference;
006import org.elasticsearch.rest.support.RestUtils;
007import org.jboss.netty.handler.codec.http.HttpMethod;
008import org.xbib.elasticsearch.common.bytes.ChannelBufferBytesReference;
009import org.xbib.elasticsearch.http.HttpRequest;
010
011import java.util.Map;
012
013import static org.elasticsearch.common.collect.Maps.newHashMap;
014
015public class NettyHttpRequest extends HttpRequest {
016
017    private final org.jboss.netty.handler.codec.http.HttpRequest request;
018
019    private final Map<String, String> params;
020
021    private final String rawPath;
022
023    private final BytesReference content;
024
025    private final String uri;
026
027    public NettyHttpRequest(org.jboss.netty.handler.codec.http.HttpRequest request) {
028        this.request = request;
029        this.params = newHashMap();
030        this.content = request.getContent().readable() ?
031                new ChannelBufferBytesReference(request.getContent()) :
032                BytesArray.EMPTY;
033        this.uri = request.getUri();
034        int pathEndPos = uri.indexOf('?');
035        if (pathEndPos < 0) {
036            this.rawPath = uri;
037        } else {
038            this.rawPath = uri.substring(0, pathEndPos);
039            RestUtils.decodeQueryString(uri, pathEndPos + 1, params);
040        }
041    }
042
043    public org.jboss.netty.handler.codec.http.HttpRequest request() {
044        return this.request;
045    }
046
047    @Override
048    public Method method() {
049        HttpMethod httpMethod = request.getMethod();
050        if (httpMethod == HttpMethod.GET)
051            return Method.GET;
052
053        if (httpMethod == HttpMethod.POST)
054            return Method.POST;
055
056        if (httpMethod == HttpMethod.PUT)
057            return Method.PUT;
058
059        if (httpMethod == HttpMethod.DELETE)
060            return Method.DELETE;
061
062        if (httpMethod == HttpMethod.HEAD) {
063            return Method.HEAD;
064        }
065
066        if (httpMethod == HttpMethod.OPTIONS) {
067            return Method.OPTIONS;
068        }
069
070        throw new ElasticsearchIllegalArgumentException("unsupported method " + httpMethod.getName());
071    }
072
073    @Override
074    public String getMethod() {
075        return request.getMethod().getName();
076    }
077
078    @Override
079    public String uri() {
080        return uri;
081    }
082
083    @Override
084    public String rawPath() {
085        return rawPath;
086    }
087
088    @Override
089    public Map<String, String> params() {
090        return params;
091    }
092
093    @Override
094    public boolean hasContent() {
095        return content.length() > 0;
096    }
097
098    @Override
099    public boolean contentUnsafe() {
100        // Netty http decoder always copies over the http content
101        return false;
102    }
103
104    @Override
105    public BytesReference content() {
106        return content;
107    }
108
109    @Override
110    public String header(String name) {
111        return request.headers().get(name);
112    }
113
114    @Override
115    public Iterable<Map.Entry<String, String>> headers() {
116        return request.headers().entries();
117    }
118
119    @Override
120    public boolean hasParam(String key) {
121        return params.containsKey(key);
122    }
123
124    @Override
125    public String param(String key) {
126        return params.get(key);
127    }
128
129    @Override
130    public String param(String key, String defaultValue) {
131        String value = params.get(key);
132        if (value == null) {
133            return defaultValue;
134        }
135        return value;
136    }
137}