Search in sources :

Example 46 with QueryStringDecoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringDecoder in project cdap by caskdata.

the class MetricsHandler method query.

@POST
@Path("/query")
public void query(FullHttpRequest request, HttpResponder responder, @QueryParam("metric") List<String> metrics, @QueryParam("groupBy") List<String> groupBy, @QueryParam("tag") List<String> tags) throws Exception {
    try {
        Map<String, List<String>> queryParams = new QueryStringDecoder(request.uri()).parameters();
        if (queryParams.isEmpty()) {
            if (HttpUtil.getContentLength(request) > 0) {
                Map<String, MetricsQueryHelper.QueryRequestFormat> queries = GSON.fromJson(request.content().toString(StandardCharsets.UTF_8), new TypeToken<Map<String, MetricsQueryHelper.QueryRequestFormat>>() {
                }.getType());
                responder.sendJson(HttpResponseStatus.OK, GSON.toJson(metricsQueryHelper.executeBatchQueries(queries)));
                return;
            }
            responder.sendJson(HttpResponseStatus.BAD_REQUEST, "Batch request with empty content");
        }
        responder.sendJson(HttpResponseStatus.OK, GSON.toJson(metricsQueryHelper.executeTagQuery(tags, metrics, groupBy, queryParams)));
    } catch (IllegalArgumentException e) {
        LOG.warn("Invalid request", e);
        responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
    } catch (Exception e) {
        LOG.error("Exception querying metrics ", e);
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Internal error while querying for metrics");
    }
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) TypeToken(com.google.gson.reflect.TypeToken) List(java.util.List) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 47 with QueryStringDecoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringDecoder in project cdap by caskdata.

the class PreviewHttpHandler method query.

@POST
@Path("previews/{preview-id}/metrics/query")
public void query(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("preview-id") String previewId, @QueryParam("metric") List<String> metrics, @QueryParam("groupBy") List<String> groupBy, @QueryParam("tag") List<String> tags) throws Exception {
    MetricsQueryHelper helper = getMetricsQueryHelper(namespaceId, previewId);
    try {
        Map<String, List<String>> queryParameters = new QueryStringDecoder(request.uri()).parameters();
        if (queryParameters.isEmpty()) {
            if (request.content().isReadable()) {
                Map<String, MetricsQueryHelper.QueryRequestFormat> queries = GSON.fromJson(request.content().toString(StandardCharsets.UTF_8), new TypeToken<Map<String, MetricsQueryHelper.QueryRequestFormat>>() {
                }.getType());
                overrideQueries(queries, namespaceId, previewId);
                responder.sendJson(HttpResponseStatus.OK, GSON.toJson(helper.executeBatchQueries(queries)));
                return;
            }
            responder.sendJson(HttpResponseStatus.BAD_REQUEST, "Batch request with empty content");
        }
        tags = overrideTags(tags, namespaceId, previewId);
        responder.sendJson(HttpResponseStatus.OK, GSON.toJson(helper.executeTagQuery(tags, metrics, groupBy, queryParameters)));
    } catch (IllegalArgumentException e) {
        LOG.warn("Invalid request", e);
        responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
    } catch (Exception e) {
        LOG.error("Exception querying metrics ", e);
        responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Internal error while querying for metrics");
    }
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) MetricsQueryHelper(co.cask.cdap.metrics.query.MetricsQueryHelper) TypeToken(com.google.gson.reflect.TypeToken) List(java.util.List) JsonSyntaxException(com.google.gson.JsonSyntaxException) BadRequestException(co.cask.cdap.common.BadRequestException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST)

Example 48 with QueryStringDecoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringDecoder in project duangframework by tcrct.

the class GetDecoder method decoder.

@Override
public Map<String, String[]> decoder() throws Exception {
    String url = request.uri();
    // 先解码
    url = QueryStringDecoder.decodeComponent(url, HttpConstants.DEFAULT_CHARSET);
    QueryStringDecoder queryStringDecoder = new QueryStringDecoder(url);
    Map<String, List<String>> map = queryStringDecoder.parameters();
    if (ToolsKit.isNotEmpty(map)) {
        for (String key : map.keySet()) {
            // System.out.println(key+"               "+map.get(key).toArray(EMPTY_ARRAYS).getClass());
            paramsMap.put(key, map.get(key).toArray(EMPTY_ARRAYS));
        }
    }
    return paramsMap;
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) List(java.util.List) ArrayList(java.util.ArrayList)

Example 49 with QueryStringDecoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringDecoder in project incubator-servicecomb-java-chassis by apache.

the class CseClientHttpRequest method execute.

@Override
public ClientHttpResponse execute() throws IOException {
    path = findUriPath(uri);
    requestMeta = createRequestMeta(method.name(), uri);
    QueryStringDecoder queryStringDecoder = new QueryStringDecoder(uri.getRawSchemeSpecificPart());
    queryParams = queryStringDecoder.parameters();
    Object[] args = this.collectArguments();
    // 异常流程,直接抛异常出去
    return this.invoke(args);
}
Also used : QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder)

Example 50 with QueryStringDecoder

use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.QueryStringDecoder in project jocean-http by isdom.

the class DefaultSignalClientTestCase method testSignalClientOnlySignalForGetDuplicateFeatures.

@Test
public void testSignalClientOnlySignalForGetDuplicateFeatures() throws Exception {
    final TestResponse respToSendback = new TestResponse("0", "OK");
    final AtomicReference<HttpMethod> reqMethodReceivedRef = new AtomicReference<>();
    final AtomicReference<String> reqpathReceivedRef = new AtomicReference<>();
    final AtomicReference<String> reqbeanReceivedRef = new AtomicReference<>();
    final Action2<FullHttpRequest, HttpTrade> requestAndTradeAwareWhenCompleted = new Action2<FullHttpRequest, HttpTrade>() {

        @Override
        public void call(final FullHttpRequest req, final HttpTrade trade) {
            reqMethodReceivedRef.set(req.method());
            final QueryStringDecoder decoder = new QueryStringDecoder(req.uri());
            reqpathReceivedRef.set(decoder.path());
            reqbeanReceivedRef.set(decoder.parameters().get("id").get(0));
            trade.outbound(buildResponse(respToSendback, trade.onTerminate()));
        }
    };
    final String testAddr = UUID.randomUUID().toString();
    final Subscription server = TestHttpUtil.createTestServerWith(testAddr, requestAndTradeAwareWhenCompleted, Feature.ENABLE_LOGGING, Feature.ENABLE_COMPRESSOR);
    try {
        final TestChannelCreator creator = new TestChannelCreator();
        final TestChannelPool pool = new TestChannelPool(1);
        final DefaultHttpClient httpclient = new DefaultHttpClient(creator, pool);
        final DefaultSignalClient signalClient = new DefaultSignalClient(new URI("http://test"), httpclient);
        signalClient.registerRequestType(TestRequest.class, TestResponse.class, null, buildUri2Addr(testAddr), // duplicate ENABLE_SETURI
        RosaProfiles.ENABLE_SETPATH, Feature.ENABLE_LOGGING);
        final TestRequest reqToSend = new TestRequest("1");
        final TestResponse respReceived = ((SignalClient) signalClient).interaction().request(reqToSend).<TestResponse>build().timeout(1, TimeUnit.SECONDS).toBlocking().single();
        assertEquals(HttpMethod.GET, reqMethodReceivedRef.get());
        assertEquals("/test/simpleRequest", reqpathReceivedRef.get());
        assertEquals(reqToSend.getId(), reqbeanReceivedRef.get());
        assertEquals(respToSendback, respReceived);
        pool.awaitRecycleChannels();
    } finally {
        server.unsubscribe();
    }
}
Also used : FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) Action2(rx.functions.Action2) AtomicReference(java.util.concurrent.atomic.AtomicReference) URI(java.net.URI) DefaultHttpClient(org.jocean.http.client.impl.DefaultHttpClient) QueryStringDecoder(io.netty.handler.codec.http.QueryStringDecoder) HttpTrade(org.jocean.http.server.HttpServerBuilder.HttpTrade) TestChannelPool(org.jocean.http.client.impl.TestChannelPool) DefaultSignalClient(org.jocean.http.rosa.impl.DefaultSignalClient) Subscription(rx.Subscription) HttpMethod(io.netty.handler.codec.http.HttpMethod) TestChannelCreator(org.jocean.http.client.impl.TestChannelCreator) Test(org.junit.Test)

Aggregations

QueryStringDecoder (io.netty.handler.codec.http.QueryStringDecoder)69 List (java.util.List)29 Test (org.junit.Test)15 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)14 Map (java.util.Map)10 HashMap (java.util.HashMap)9 IOException (java.io.IOException)8 URI (java.net.URI)7 ByteBuf (io.netty.buffer.ByteBuf)6 HttpContent (io.netty.handler.codec.http.HttpContent)6 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)6 HttpPostRequestDecoder (io.netty.handler.codec.http.multipart.HttpPostRequestDecoder)6 ArrayList (java.util.ArrayList)6 HttpMethod (io.netty.handler.codec.http.HttpMethod)5 HttpRequest (io.netty.handler.codec.http.HttpRequest)5 DeviceSession (org.traccar.DeviceSession)5 Position (org.traccar.model.Position)5 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)4 Channel (io.netty.channel.Channel)3 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)3