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

org.brutusin.jsonsrv.JsonService Maven / Gradle / Ivy

/*
 * Copyright 2015 brutusin.org
 *
 * 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 org.brutusin.jsonsrv;

import net.jodah.typetools.TypeResolver;
import org.brutusin.json.spi.JsonCodec;
import org.brutusin.json.spi.JsonSchema;

/**
 *
 * @author Ignacio del Valle Alles [email protected]
 */
public final class JsonService {

    private final String id;
    private final JsonAction action;
    private final String description;

    private final JsonSchema inputSchema;
    private final JsonSchema outputSchema;
    private final Class inputClass;
    private final Class outputClass;
    
    public JsonService(String id, JsonAction action, String description) {
        this.id = id;
        this.action = action;
        this.description = description;
        Class[] types = TypeResolver.resolveRawArguments(JsonAction.class, action.getClass());
        this.inputClass = (Class) types[0];
        this.outputClass = (Class) types[1];
        this.inputSchema = JsonCodec.getInstance().getSchema(this.inputClass);
        this.outputSchema = JsonCodec.getInstance().getSchema(this.outputClass);
    }

    public String getId() {
        return id;
    }

    public JsonAction getAction() {
        return action;
    }

    public JsonSchema getInputSchema() {
        return inputSchema;
    }

    public JsonSchema getOutputSchema() {
        return outputSchema;
    }

    public Class getInputClass() {
        return inputClass;
    }

    public Class getOutputClass() {
        return outputClass;
    }

    public String getDescription() {
        return description;
    }

    public boolean isSafe() {
        return getAction() instanceof SafeAction;
    }
}