CTLViewRender.java :  » Google-tech » jrest4guice » org » jrest4guice » rest » writer » renderer » Java Open Source

Java Open Source » Google tech » jrest4guice 
jrest4guice » org » jrest4guice » rest » writer » renderer » CTLViewRender.java
package org.jrest4guice.rest.writer.renderer;


import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.commontemplate.core.Context;
import org.commontemplate.core.Template;
import org.commontemplate.engine.Engine;
import org.jrest4guice.rest.ServiceResult;
import org.jrest4guice.rest.annotations.PageFlow;
import org.jrest4guice.rest.writer.HtmlResponseWriter;
import org.jrest4guice.security.SecurityContext;
import org.jrest4guice.security.UserRole;

import com.google.inject.Inject;

/**
 * CTLcommontemplate
 * @author <a href="mailto:zhangyouqun@gmail.com">cnoss (QQ:86895156)</a>
 */
public class CTLViewRender implements ViewRender {
  @Inject(optional=true)
  private HttpServletRequest request;
  @Inject(optional=true)
  private Engine engine;
  @Inject(optional=true)
  private Context context;
  
  @Inject(optional=true)
  private SecurityContext securityContext;

  /* (non-Javadoc)
   * @see org.jrest4guice.rest.render.ViewRender#render(java.io.OutputStream, org.jrest4guice.rest.annotations.PageFlow, org.jrest4guice.rest.ServiceResult)
   */
  @Override
  public void render(OutputStream out, PageFlow annotation, ServiceResult result,String charset)
      throws Exception {
    try {
      String url = annotation.success().value();
      Boolean inChain = result.isInChain();
      if((inChain==null || !inChain) &&(result.getErrorType() != null || result.getInvalidValues() != null)){
        url = annotation.error().value();
      }
      
      HttpSession session = this.request.getSession();
      url = session.getServletContext().getRealPath("/")+url;
      
      //
      Template template = this.engine.getTemplate(url);
      //
      this.context.put("ctxPath", this.request.getContextPath());
      UserRole userPrincipal = this.securityContext.getUserPrincipal();
      this.context.put(SecurityContext.CURRENT_USER_ROlE, userPrincipal);
      this.context.put(SecurityContext.USER_IS_LOGIN, userPrincipal!=null);
      this.context.put("ctx", result);
      this.context.put("xctx",session.getAttribute(HtmlResponseWriter.OPTION_KEY));
      
      template.render(this.context);
      
      String content = this.context.getOut().toString();
      
      //
      out.write(content.getBytes("utf-8"));
    }finally{
      this.context.clear();
    }
  }

  /* (non-Javadoc)
   * @see org.jrest4guice.rest.render.ViewRender#getRenderType()
   */
  @Override
  public String getRenderType() {
    return ResultType.CTL;
  }

  @Override
  public String getRenderTypeShortName() {
    return ".ctl";
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.