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

org.brutusin.jsonsrv.human.HumanRenderer Maven / Gradle / Ivy

package org.brutusin.jsonsrv.human;

/*
 * 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.
 */
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.brutusin.jsonsrv.JsonService;
import org.brutusin.jsonsrv.JsonServlet;
import org.brutusin.jsonsrv.SafeAction;
import org.brutusin.jsonsrv.plugin.Renderer;

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

    public static final String PARAM_HUMAN = "human";
    public static final String REQ_ATT_JSON = "json";
    public static final String REQ_ATT_SRV_ID = "serviceId";
    public static final String REQ_ATT_SRV_SAFE = "serviceSafe";
    public static final String REQ_ATT_REQ_PATH = "requestedPath";

    public static final String RESOURCE_ROOT_PATH = "/jsonsrv/human-renderer/";

    public void service(ServletConfig servletConfig, HttpServletRequest req, HttpServletResponse resp, String json, JsonServlet.SchemaMode schemaMode, JsonService service) throws IOException {

        if (req.getParameter(PARAM_HUMAN) == null) {
            resp.getWriter().print(json);
        } else {
            try {
                String serviceId = service == null ? null : service.getId();
                Boolean serviceSafe = service == null ? null : service.getAction() instanceof SafeAction;
                
                req.setAttribute(REQ_ATT_JSON, json);
                req.setAttribute(REQ_ATT_SRV_ID, serviceId);
                req.setAttribute(REQ_ATT_SRV_SAFE, serviceSafe);
                
                req.setAttribute(REQ_ATT_REQ_PATH, req.getContextPath() + req.getServletPath());
                RequestDispatcher requestDispatcher;
                if (schemaMode == JsonServlet.SchemaMode.I) {
                    requestDispatcher = req.getServletContext().getRequestDispatcher(RESOURCE_ROOT_PATH + "input-schema.jsp");
                } else if (schemaMode == JsonServlet.SchemaMode.O) {
                    requestDispatcher = req.getServletContext().getRequestDispatcher(RESOURCE_ROOT_PATH + "output-schema.jsp");
                } else {
                    if (serviceId == null) {
                        requestDispatcher = req.getServletContext().getRequestDispatcher(RESOURCE_ROOT_PATH + "service-list.jsp");
                    } else {
                        requestDispatcher = req.getServletContext().getRequestDispatcher(RESOURCE_ROOT_PATH + "service-result.jsp");
                    }
                }
                requestDispatcher.forward(req, resp);
            } catch (ServletException ex) {
                Logger.getLogger(HumanRenderer.class.getName()).log(Level.SEVERE, null, ex);
                throw new RuntimeException(ex);
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy