com.google.gerrit.extensions.restapi.RestApiModule Maven / Gradle / Ivy
// Copyright (C) 2012 The Android Open Source Project
//
// 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
//
// 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 com.google.gerrit.extensions.restapi;
import com.google.gerrit.extensions.annotations.Export;
import com.google.gerrit.extensions.annotations.Exports;
import com.google.gerrit.extensions.config.FactoryModule;
import com.google.inject.Provider;
import com.google.inject.TypeLiteral;
import com.google.inject.binder.LinkedBindingBuilder;
import com.google.inject.binder.ScopedBindingBuilder;
/** Guice DSL for binding {@link RestView} implementations. */
public abstract class RestApiModule extends FactoryModule {
protected static final String GET = "GET";
protected static final String PUT = "PUT";
protected static final String DELETE = "DELETE";
protected static final String POST = "POST";
protected ReadViewBinder get(TypeLiteral> viewType) {
return new ReadViewBinder<>(view(viewType, GET, "/"));
}
protected ModifyViewBinder put(TypeLiteral> viewType) {
return new ModifyViewBinder<>(view(viewType, PUT, "/"));
}
protected ModifyViewBinder post(TypeLiteral> viewType) {
return new ModifyViewBinder<>(view(viewType, POST, "/"));
}
protected ModifyViewBinder delete(TypeLiteral> viewType) {
return new ModifyViewBinder<>(view(viewType, DELETE, "/"));
}
protected ReadViewBinder get(
TypeLiteral> viewType, String name) {
return new ReadViewBinder<>(view(viewType, GET, name));
}
protected ModifyViewBinder put(
TypeLiteral> viewType, String name) {
return new ModifyViewBinder<>(view(viewType, PUT, name));
}
protected ModifyViewBinder post(
TypeLiteral> viewType, String name) {
return new ModifyViewBinder<>(view(viewType, POST, name));
}
protected ModifyViewBinder delete(
TypeLiteral> viewType, String name) {
return new ModifyViewBinder<>(view(viewType, DELETE, name));
}
protected ChildCollectionBinder
child(
TypeLiteral> type, String name) {
return new ChildCollectionBinder<>(view(type, GET, name));
}
protected LinkedBindingBuilder> view(
TypeLiteral> viewType, String method, String name) {
return bind(viewType).annotatedWith(export(method, name));
}
private static Export export(String method, String name) {
if (name.length() > 1 && name.startsWith("/")) {
// Views may be bound as "/" to mean the resource itself, or
// as "status" as in "/type/{id}/status". Don't bind "/status"
// if the caller asked for that, bind what the server expects.
name = name.substring(1);
}
return Exports.named(method + "." + name);
}
public static class ReadViewBinder {
private final LinkedBindingBuilder> binder;
private ReadViewBinder(LinkedBindingBuilder> binder) {
this.binder = binder;
}
public > ScopedBindingBuilder to(Class impl) {
return binder.to(impl);
}
public > void toInstance(T impl) {
binder.toInstance(impl);
}
public > ScopedBindingBuilder toProvider(
Class extends Provider extends T>> providerType) {
return binder.toProvider(providerType);
}
public > ScopedBindingBuilder toProvider(
Provider extends T> provider) {
return binder.toProvider(provider);
}
}
public static class ModifyViewBinder {
private final LinkedBindingBuilder> binder;
private ModifyViewBinder(LinkedBindingBuilder> binder) {
this.binder = binder;
}
public > ScopedBindingBuilder to(Class impl) {
return binder.to(impl);
}
public > void toInstance(T impl) {
binder.toInstance(impl);
}
public > ScopedBindingBuilder toProvider(
Class extends Provider extends T>> providerType) {
return binder.toProvider(providerType);
}
public > ScopedBindingBuilder toProvider(
Provider extends T> provider) {
return binder.toProvider(provider);
}
}
public static class ChildCollectionBinder {
private final LinkedBindingBuilder> binder;
private ChildCollectionBinder(LinkedBindingBuilder> binder) {
this.binder = binder;
}
public > ScopedBindingBuilder to(
Class impl) {
return binder.to(impl);
}
public > void toInstance(T impl) {
binder.toInstance(impl);
}
public >
ScopedBindingBuilder toProvider(Class extends Provider extends T>> providerType) {
return binder.toProvider(providerType);
}
public >
ScopedBindingBuilder toProvider(Provider extends T> provider) {
return binder.toProvider(provider);
}
}
}