All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.micronaut.http.client.netty.NettyClientHttpRequestFactory Maven / Gradle / Ivy

There is a newer version: 4.7.6
Show newest version
/*
 * Copyright 2017-2020 original authors
 *
 * 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
 *
 * https://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.micronaut.http.client.netty;

import io.micronaut.core.annotation.Internal;
import io.micronaut.core.beans.BeanMap;
import io.micronaut.http.HttpMethod;
import io.micronaut.http.HttpRequestFactory;
import io.micronaut.http.MutableHttpRequest;
import io.micronaut.http.uri.UriTemplate;

import java.util.Map;

/**
 * Implementation of the {@link HttpRequestFactory} interface for Netty.
 *
 * @author Graeme Rocher
 * @since 1.0
 */
@Internal
public class NettyClientHttpRequestFactory implements HttpRequestFactory {

    @Override
    public  MutableHttpRequest get(String uri) {
        return new NettyClientHttpRequest<>(HttpMethod.GET, uri);
    }

    @Override
    public  MutableHttpRequest post(String uri, T body) {
        HttpMethod method = HttpMethod.POST;
        return buildRequest(uri, body, method);
    }

    @Override
    public  MutableHttpRequest put(String uri, T body) {
        return buildRequest(uri, body, HttpMethod.PUT);
    }

    @Override
    public  MutableHttpRequest patch(String uri, T body) {
        return buildRequest(uri, body, HttpMethod.PATCH);
    }

    @Override
    public  MutableHttpRequest head(String uri) {
        return new NettyClientHttpRequest<>(HttpMethod.HEAD, uri);
    }

    @Override
    public  MutableHttpRequest options(String uri) {
        return new NettyClientHttpRequest<>(HttpMethod.OPTIONS, uri);
    }

    @Override
    public  MutableHttpRequest delete(String uri, T body) {
        return buildRequest(uri, body, HttpMethod.DELETE);
    }

    @Override
    public  MutableHttpRequest create(HttpMethod httpMethod, String uri) {
        return new NettyClientHttpRequest<>(httpMethod, uri);
    }

    @Override
    public  MutableHttpRequest create(HttpMethod httpMethod, String uri, String httpMethodName) {
        return new NettyClientHttpRequest<>(httpMethod, uri, httpMethodName);
    }

    @SuppressWarnings("unchecked")
    private  MutableHttpRequest buildRequest(String uri, T body, HttpMethod method) {
        if (uri.indexOf('{') > -1 && body != null) {
            if (body instanceof Map) {
                uri = UriTemplate.of(uri).expand((Map) body);
            } else {
                uri = UriTemplate.of(uri).expand(BeanMap.of(body));
            }
        }
        return new NettyClientHttpRequest(method, uri).body(body);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy