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

org.brutusin.jsonsrv.impl.SchemaAction 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.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.brutusin.json.spi.JsonSchema;
import org.brutusin.jsonsrv.JsonService;
import org.brutusin.jsonsrv.JsonServlet;
import org.brutusin.jsonsrv.SafeAction;
import org.brutusin.jsonsrv.caching.CachingInfo;
import org.brutusin.jsonsrv.caching.ConditionalCachingInfo;

/**
 *
 * @author Ignacio del Valle Alles [email protected]
 */
public class SchemaAction extends SafeAction {

    private Map services;
    private Map inputSchemaCachingInfoMap;
    private Map outputSchemaCachingInfoMap;

    @Override
    protected void init(Map services) throws IOException {
        this.services = services;
        this.inputSchemaCachingInfoMap = new HashMap(services.size());
        this.outputSchemaCachingInfoMap = new HashMap(services.size());

        for (Map.Entry entrySet : services.entrySet()) {
            String id = entrySet.getKey();
            JsonService service = entrySet.getValue();
            inputSchemaCachingInfoMap.put(id, new ConditionalCachingInfo(String.valueOf(service.getInputSchema().toString())));
            outputSchemaCachingInfoMap.put(id, new ConditionalCachingInfo(String.valueOf(service.getOutputSchema().toString())));
        }
    }

    @Override
    public JsonSchema execute(SchemaActionInput input) throws Exception {
        validateInput(input);
        JsonService service = services.get(input.getId());
        if (input.getMode() == JsonServlet.SchemaMode.I) {
            return service.getInputSchema();
        } else {
            return service.getOutputSchema();
        }
    }

    @Override
    public CachingInfo getCachingInfo(SchemaActionInput input) {
        validateInput(input);
        if (input.getMode() == JsonServlet.SchemaMode.I) {
            return this.inputSchemaCachingInfoMap.get(input.getId());
        } else {
            return this.outputSchemaCachingInfoMap.get(input.getId());
        }
    }

    private void validateInput(SchemaActionInput input) {
        if (input.getId() == null) {
            throw new IllegalArgumentException("Service id is required");
        }
        if (input.getMode() == null) {
            throw new IllegalArgumentException("Schema mode is required");
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy