ams.fwk.channel.web.AmsRequestView.java Source code

Java tutorial

Introduction

Here is the source code for ams.fwk.channel.web.AmsRequestView.java

Source

/*
 * Copyright (c) 2006 SK C&C. All rights reserved.
 * 
 * This software is the confidential and proprietary information of SK C&C. You
 * shall not disclose such Confidential Information and shall use it only in
 * accordance wih the terms of the license agreement you entered into with SK
 * C&C.
 */

package ams.fwk.channel.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import nexcore.framework.core.Constants;
import nexcore.framework.core.log.LogManager;
import nexcore.framework.online.channel.core.IRequestContext;
import nexcore.framework.online.channel.core.IResponseContext;
import nexcore.framework.online.channel.core.RenderException;
import nexcore.framework.online.channel.web.AbstractWebView;

import org.apache.commons.logging.Log;

import ams.fwk.utils.BaseUtils;

import com.star.nexcore.foundation.util.StringUtils;

/**
 * <ul>
 * <li>  :  ? </li>
 * <li>  : ?</li>
 * <li>  : Web? ? ???. 
 * <p>constructor ? url?   request parameter? target?  
 * ? . </p>
 * <p>url ? "forward:"  ? forward, "redirect:"  ? sendRedirect .
 *   forward ?. 
 * </p> </li>
 * <li>? : 2007. 2. 24</li>
 * <li>? : ?</li>
 * 
 * <li>  :  export properties? ? export JSP? forwarding  . </li>
 * <li>? : 2013. 4. 17</li>
 * <li>? : ?</li>
 * 
 * </ul>
 */
public class AmsRequestView extends AbstractWebView {

    /**
     * Logger.
     */
    private Log logger = LogManager.getFwkLog();

    /**
     * Forward ? Page.
     */
    private String target = null;

    public AmsRequestView() {
        this.target = "/should/set/meaningful/page/for/view";
    }

    /**
     * Constructor.
     * @param target
     */
    public AmsRequestView(String target) {
        this.target = target;
    }

    private static String FORWARD_KEY = "forward:";

    private static String REDIRECT_KEY = "redirect:";

    /* (non-java)
     * @see nexcore.framework.online.channel.core.IView#render(nexcore.framework.online.channel.core.IRequestContext, nexcore.framework.online.channel.core.IResponseContext)
     */
    public void render(IRequestContext requestCtx, IResponseContext responseCtx) throws RenderException {

        super.render(requestCtx, responseCtx);

        HttpServletRequest request = (HttpServletRequest) responseCtx.getReadProtocol();

        HttpServletResponse response = (HttpServletResponse) responseCtx.getWriteProtocol();

        /*
         * nc_target target ,   ?    url? .
         *  ?  properties? ? path   ? 
         */
        String target = null;
        if (request.getParameter(Constants.TARGET) != null) {
            target = BaseUtils.getConfiguration(request.getParameter(Constants.TARGET));
            if (StringUtils.isEmpty(target)) {
                target = (String) request.getParameter(Constants.TARGET);
            }
        } else {
            target = this.target;
        }

        /*
         * forward/redirect .
         */
        boolean isForwarding = true;
        if (target.startsWith(FORWARD_KEY)) {
            target = target.substring(FORWARD_KEY.length());
        } else if (target.startsWith(REDIRECT_KEY)) {
            isForwarding = false;
            target = target.substring(REDIRECT_KEY.length());
        }

        try {
            if (isForwarding) {
                request.getRequestDispatcher(target).forward(request, response);
            } else {
                String redirectPage = target.startsWith("/") ? request.getContextPath() + target : target;
                response.sendRedirect(response.encodeRedirectURL(redirectPage));
            }
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("Exception occurred while forward jsp to " + target, e);
            }
            e.printStackTrace();

            // ?  
            // -. ServletException
            // -. IOException
            throw new RenderException("SKFS1010", new String[] { target, e.getLocalizedMessage() }, e);
        }
    }

    public String toString() {
        return "IView::" + getClass().getName();
    }

}