Search in sources :

Example 1 with RequestChanger

use of org.jocean.http.rosa.impl.RequestChanger in project jocean-http by isdom.

the class HeaderParamPreprocessor method call.

@Override
public RequestChanger call(final Object signalBean) {
    if (null == signalBean) {
        return null;
    }
    final Field[] headerFields = ReflectUtils.getAnnotationFieldsOf(signalBean.getClass(), HeaderParam.class);
    if (headerFields.length > 0) {
        return new RequestChanger() {

            @Override
            public void call(final HttpRequest request) {
                for (Field field : headerFields) {
                    try {
                        final Object value = field.get(signalBean);
                        if (null != value) {
                            final String headername = field.getAnnotation(HeaderParam.class).value();
                            request.headers().set(headername, value);
                        }
                    } catch (Exception e) {
                        LOG.warn("exception when get value from field:[{}], detail:{}", field, ExceptionUtils.exception2detail(e));
                    }
                }
            }

            @Override
            public int ordinal() {
                return 100;
            }
        };
    } else {
        return null;
    }
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) Field(java.lang.reflect.Field) HeaderParam(javax.ws.rs.HeaderParam) RequestChanger(org.jocean.http.rosa.impl.RequestChanger)

Example 2 with RequestChanger

use of org.jocean.http.rosa.impl.RequestChanger in project jocean-http by isdom.

the class QueryParamPreprocessor method call.

@Override
public RequestChanger call(final Object signalBean) {
    if (null == signalBean) {
        return null;
    }
    final Field[] queryFields = ReflectUtils.getAnnotationFieldsOf(signalBean.getClass(), QueryParam.class);
    if (queryFields.length > 0) {
        return new RequestChanger() {

            @Override
            public void call(final HttpRequest request) {
                // only GET method will assemble query parameters
                // or the QueryParam field explicit annotated HttpMethod via AnnotationWrapper
                // assemble query parameters
                final boolean isGetMethod = request.method().equals(HttpMethod.GET);
                final QueryStringEncoder encoder = new QueryStringEncoder(request.uri());
                for (Field field : queryFields) {
                    if (isGetMethod || Nettys.isFieldAnnotatedOfHttpMethod(field, request.method())) {
                        try {
                            final Object value = field.get(signalBean);
                            if (null != value) {
                                final String paramkey = field.getAnnotation(QueryParam.class).value();
                                encoder.addParam(paramkey, String.valueOf(value));
                            }
                        } catch (Exception e) {
                            LOG.warn("exception when get field({})'s value, detail:{}", field, ExceptionUtils.exception2detail(e));
                        }
                    }
                }
                request.setUri(encoder.toString());
            }

            @Override
            public int ordinal() {
                return 100;
            }
        };
    } else {
        return null;
    }
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) Field(java.lang.reflect.Field) QueryParam(javax.ws.rs.QueryParam) RequestChanger(org.jocean.http.rosa.impl.RequestChanger) QueryStringEncoder(io.netty.handler.codec.http.QueryStringEncoder)

Aggregations

HttpRequest (io.netty.handler.codec.http.HttpRequest)2 Field (java.lang.reflect.Field)2 RequestChanger (org.jocean.http.rosa.impl.RequestChanger)2 QueryStringEncoder (io.netty.handler.codec.http.QueryStringEncoder)1 HeaderParam (javax.ws.rs.HeaderParam)1 QueryParam (javax.ws.rs.QueryParam)1