org.apache.cxf.jaxrs.client.PromiseRxInvokerImpl Maven / Gradle / Ivy
/*
* 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;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.InvocationCallback;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;
import org.osgi.service.jaxrs.client.PromiseRxInvoker;
import org.osgi.util.promise.Deferred;
import org.osgi.util.promise.Promise;
import org.osgi.util.promise.PromiseFactory;
class PromiseRxInvokerImpl implements PromiseRxInvoker {
private static final class DeferredHandler implements InvocationCallback {
private final Deferred deferred;
private DeferredHandler(Deferred deferred) {
this.deferred = deferred;
}
@Override
public void completed(R response) {
deferred.resolve(response);
}
@Override
public void failed(Throwable throwable) {
deferred.fail(throwable);
}
}
public PromiseRxInvokerImpl(
WebClient webClient, PromiseFactory promiseFactory) {
_webClient = webClient;
_promiseFactory = promiseFactory;
}
@Override
public Promise delete() {
return method(HttpMethod.DELETE);
}
@Override
public Promise delete(Class aClass) {
return method(HttpMethod.DELETE, aClass);
}
@Override
public Promise delete(GenericType genericType) {
return method(HttpMethod.DELETE, genericType);
}
@Override
public Promise get() {
return method(HttpMethod.GET);
}
@Override
public Promise get(Class aClass) {
return method(HttpMethod.GET, aClass);
}
@Override
public Promise get(GenericType genericType) {
return method(HttpMethod.GET, genericType);
}
@Override
public Promise head() {
return method(HttpMethod.HEAD);
}
@Override
public Promise method(String s, Class responseType) {
Deferred deferred = _promiseFactory.deferred();
_webClient.doInvokeAsync(s, null, null, null, responseType, responseType,
new DeferredHandler(deferred));
return deferred.getPromise();
}
@Override
public Promise method(String s, Entity> entity, Class responseType) {
Deferred deferred = _promiseFactory.deferred();
_webClient.doInvokeAsync(s, entity, null, null, responseType, responseType,
new DeferredHandler(deferred));
return deferred.getPromise();
}
@Override
public Promise method(
String s, Entity> entity, GenericType genericType) {
Deferred deferred = _promiseFactory.deferred();
_webClient.doInvokeAsync(s, entity, null, null, genericType.getRawType(), genericType.getType(),
new DeferredHandler(deferred));
return deferred.getPromise();
}
@Override
public Promise method(String s, Entity> entity) {
return method(s, entity, Response.class);
}
@Override
public Promise method(String s, GenericType genericType) {
Deferred deferred = _promiseFactory.deferred();
_webClient.doInvokeAsync(s, null, null, null, genericType.getRawType(), genericType.getType(),
new DeferredHandler(deferred));
return deferred.getPromise();
}
@Override
public Promise method(String s) {
return method(s, Response.class);
}
@Override
public Promise options() {
return method(HttpMethod.OPTIONS);
}
@Override
public Promise options(Class aClass) {
return method(HttpMethod.OPTIONS, aClass);
}
@Override
public Promise options(GenericType genericType) {
return method(HttpMethod.OPTIONS, genericType);
}
@Override
public Promise post(Entity> entity, Class aClass) {
return method(HttpMethod.POST, entity, aClass);
}
@Override
public Promise post(Entity> entity, GenericType genericType) {
return method(HttpMethod.POST, entity, genericType);
}
@Override
public Promise post(Entity> entity) {
return method(HttpMethod.POST, entity);
}
@Override
public Promise put(Entity> entity, Class aClass) {
return method(HttpMethod.PUT, entity, aClass);
}
@Override
public Promise put(Entity> entity, GenericType genericType) {
return method(HttpMethod.PUT, entity, genericType);
}
@Override
public Promise put(Entity> entity) {
return method(HttpMethod.PUT, entity);
}
@Override
public Promise trace() {
return method("TRACE", Response.class);
}
@Override
public Promise trace(Class aClass) {
return method("TRACE", aClass);
}
@Override
public Promise trace(GenericType genericType) {
return method("TRACE", genericType);
}
private final PromiseFactory _promiseFactory;
private final WebClient _webClient;
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy