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

org.apache.cxf.jaxrs.client.spec.InvocationBuilderImpl Maven / Gradle / Ivy

There is a newer version: 4.1.0
Show newest version
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF 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.apache.cxf.jaxrs.client.spec;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.Future;

import javax.ws.rs.HttpMethod;
import javax.ws.rs.client.AsyncInvoker;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.client.InvocationCallback;
import javax.ws.rs.client.SyncInvoker;
import javax.ws.rs.core.CacheControl;
import javax.ws.rs.core.Cookie;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.RuntimeDelegate;
import javax.ws.rs.ext.RuntimeDelegate.HeaderDelegate;

import org.apache.cxf.helpers.CastUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.cxf.jaxrs.utils.HttpUtils;

public class InvocationBuilderImpl implements Invocation.Builder {
    private static final String PROPERTY_KEY = "jaxrs.filter.properties";
    
    private WebClient webClient;
    private SyncInvoker sync;
    
    public InvocationBuilderImpl(WebClient webClient) {
        this.webClient = webClient;
        this.sync = webClient.sync();
    }
    
    public WebClient getWebClient() {
        return this.webClient;
    }
    
    @Override
    public Response delete() {
        return sync.delete();
    }

    @Override
    public  T delete(Class cls) {
        return sync.delete(cls);
    }

    @Override
    public  T delete(GenericType type) {
        return sync.delete(type);
    }

    @Override
    public Response get() {
        return sync.get();
    }

    @Override
    public  T get(Class cls) {
        return sync.get(cls);
    }

    @Override
    public  T get(GenericType type) {
        return sync.get(type);
    }

    @Override
    public Response head() {
        return sync.head();
    }

    @Override
    public Response method(String method) {
        return sync.method(method);
    }

    @Override
    public  T method(String method, Class cls) {
        return sync.method(method, cls);
    }

    @Override
    public  T method(String method, GenericType type) {
        return sync.method(method, type);
    }

    @Override
    public Response method(String method, Entity entity) {
        return sync.method(method, entity);
    }

    @Override
    public  T method(String method, Entity entity, Class cls) {
        return sync.method(method, entity, cls);
    }

    @Override
    public  T method(String method, Entity entity, GenericType type) {
        return sync.method(method, entity, type);
    }

    @Override
    public Response options() {
        return sync.options();
    }

    @Override
    public  T options(Class cls) {
        return sync.options(cls);
    }

    @Override
    public  T options(GenericType type) {
        return sync.options(type);
    }

    @Override
    public Response post(Entity entity) {
        return sync.post(entity);
    }

    @Override
    public  T post(Entity entity, Class cls) {
        return sync.post(entity, cls);
    }

    @Override
    public  T post(Entity entity, GenericType type) {
        return sync.post(entity, type);
    }

    @Override
    public Response put(Entity entity) {
        return sync.put(entity);
    }

    @Override
    public  T put(Entity entity, Class cls) {
        return sync.put(entity, cls);
    }

    @Override
    public  T put(Entity entity, GenericType type) {
        return sync.put(entity, type);
    }

    @Override
    public Response trace() {
        return sync.trace();
    }

    @Override
    public  T trace(Class cls) {
        return sync.trace(cls);
    }

    @Override
    public  T trace(GenericType type) {
        return sync.trace(type);
    }

    @Override
    public Builder accept(String... types) {
        webClient.accept(types);
        return this;
    }

    @Override
    public Builder accept(MediaType... types) {
        webClient.accept(types);
        return this;
    }

    @Override
    public Builder acceptEncoding(String... enc) {
        webClient.acceptEncoding(enc);
        return this;
        
    }

    @Override
    public Builder acceptLanguage(Locale... lang) {
        for (Locale l : lang) {
            webClient.acceptLanguage(HttpUtils.toHttpLanguage(l));
        }
        return this;
    }

    @Override
    public Builder acceptLanguage(String... lang) {
        webClient.acceptLanguage(lang);
        return this;
    }

    @Override
    public Builder cacheControl(CacheControl control) {
        webClient.header(HttpHeaders.CACHE_CONTROL, control.toString());
        return this;
    }

    @Override
    public Builder cookie(Cookie cookie) {
        webClient.cookie(cookie);
        return this;
    }

    @Override
    public Builder cookie(String name, String value) {
        webClient.header(HttpHeaders.COOKIE, name + "=" + value);
        return this;
    }

    @Override
    public Builder header(String name, Object value) {
        RuntimeDelegate rd = HttpUtils.getOtherRuntimeDelegate();
        doSetHeader(rd, name, value);
        return this;
    }
    
    @Override
    public Builder headers(MultivaluedMap headers) {
        RuntimeDelegate rd = HttpUtils.getOtherRuntimeDelegate();
        for (Map.Entry> entry : headers.entrySet()) {
            for (Object value : entry.getValue()) {
                doSetHeader(rd, entry.getKey(), value);
            }
        }
        return this;
    }

    private void doSetHeader(RuntimeDelegate rd, String name, Object value) {
        HeaderDelegate hd = HttpUtils.getHeaderDelegate(rd, value); 
        if (hd != null) {
            value = hd.toString(value);
        }
        webClient.header(name, value);
    }
    
    @Override
    public Builder property(String name, Object value) {
        Map contextProps = WebClient.getConfig(webClient).getRequestContext();
        Map filterProps = CastUtils.cast((Map)contextProps.get(PROPERTY_KEY));
        if (filterProps == null) {
            filterProps = new HashMap();
            contextProps.put(PROPERTY_KEY, filterProps);
        }
        if (value == null) {
            filterProps.remove(name);
        } else {
            filterProps.put(name, value);
        }
        return this;
    }
    
    @Override
    public AsyncInvoker async() {
        return webClient.async();
    }

    @Override
    public Invocation build(String method) {
        return new InvocationImpl(method);
    }

    @Override
    public Invocation build(String method, Entity entity) {
        return new InvocationImpl(method, entity);
    }

    @Override
    public Invocation buildDelete() {
        return build(HttpMethod.DELETE);
    }

    @Override
    public Invocation buildGet() {
        return build(HttpMethod.GET);
    }

    @Override
    public Invocation buildPost(Entity entity) {
        return build(HttpMethod.POST, entity);
    }

    @Override
    public Invocation buildPut(Entity entity) {
        return build(HttpMethod.PUT, entity);
    }
    
    private class InvocationImpl implements Invocation {

        private Invocation.Builder invBuilder;
        private String httpMethod;
        private Entity entity;
        
        InvocationImpl(String httpMethod) {
            this(httpMethod, null);
        }
        
        InvocationImpl(String httpMethod, Entity entity) {
            this.invBuilder = InvocationBuilderImpl.this;
            this.httpMethod = httpMethod;
            this.entity = entity;
        }
        
        @Override
        public Response invoke() {
            return invBuilder.method(httpMethod, entity);
        }

        @Override
        public  T invoke(Class cls) {
            return invBuilder.method(httpMethod, entity, cls);
        }

        @Override
        public  T invoke(GenericType type) {
            return invBuilder.method(httpMethod, entity, type);
        }

        @Override
        public Invocation property(String name, Object value) {
            invBuilder.property(name, value);
            return this;
        }

        @Override
        public Future submit() {
            return invBuilder.async().method(httpMethod, entity);
        }

        @Override
        public  Future submit(Class cls) {
            return invBuilder.async().method(httpMethod, entity, cls);
        }

        @Override
        public  Future submit(GenericType type) {
            return invBuilder.async().method(httpMethod, entity, type);
        }

        @Override
        public  Future submit(InvocationCallback callback) {
            return invBuilder.async().method(httpMethod, entity, callback);
        }

    }
}