9d767a2670f1e5ac05cc7192bcc5751c5c0dd637.svn-base 6.24 KB
package com.thinkgem.jeesite.modules.rest.wsapi.dto.web;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.thinkgem.jeesite.common.mapper.JaxbMapper;
import com.thinkgem.jeesite.common.mapper.JsonMapper;
import com.thinkgem.jeesite.common.utils.xml.JXmlUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.reg.entity.base.RegBaseH;
import com.thinkgem.jeesite.modules.reg.entity.bus.RegBusQlr;
import com.thinkgem.jeesite.modules.reg.entity.updata.RegBusSjhjtable;
import com.thinkgem.jeesite.modules.reg.service.bus.RegBusQlrService;
import com.thinkgem.jeesite.modules.reg.service.updata.RegBusSjhjtableService;
import com.thinkgem.jeesite.modules.rest.wsapi.dto.service.RestfulService;
import com.thinkgem.jeesite.modules.sys.web.LoginController;

@Controller
@RequestMapping(value = "${adminPath}/regService")
public class TestRestfulController extends BaseController {
	
	@Autowired  
    private RestfulService restfulService; 
	@Autowired
	private RegBusQlrService regBusQlrService;
	@Autowired
	private RegBusSjhjtableService regBusSjhjtableService;

	
	@RequestMapping("display")
	public ModelAndView helloWorld() {  
        String message = restfulService.getHelloWorld();
        //logger.info(message);
        return new ModelAndView("display", "message", message);
    }
    
    @RequestMapping(value="display/random")
    @ResponseBody
    public String displayJson() throws Exception{
        int randomValue = restfulService.getRandomNumber();
        Map<String,Object> map = new HashMap<String,Object>();  
        map.put("随机数", randomValue);
        ObjectMapper objectMapper = new ObjectMapper();
        String result = objectMapper.writeValueAsString(map);
        return result;
    }
      
    @RequestMapping(value="display/hxml")  
    public @ResponseBody String displayUserWithXml(HttpServletResponse response, Model model) throws Exception{  
        //报文头部数据填充
    	RegBusSjhjtable regBusSjhjtable = new RegBusSjhjtable();
    	regBusSjhjtable.setBizmsgid("500115151030000025");
        regBusSjhjtable.setAsid("AS100");
        regBusSjhjtable.setAreacode("500115");
        regBusSjhjtable.setRectype("2000402");
        regBusSjhjtable.setRighttype("3");
        regBusSjhjtable.setRegtype("100");
        regBusSjhjtable.setCreatedate(new Date());
        regBusSjhjtable.setRecflowid("201507011190001");
        regBusSjhjtable.setRegorgid("500115");
        regBusSjhjtable.setParcelid("500115001019GB00008");
        regBusSjhjtable.setEstatenum("500115001019GB00008F00000064");
        regBusSjhjtable.setPreestatenum("500115001019GB00008F00000064");
        regBusSjhjtable.setPrecertid("汉2016证书000001");
        regBusSjhjtable.setCertcount("12");
        regBusSjhjtable.setProofcount("12");
        regBusSjhjtable.setDigitalsign("6bfa32a846896c9cf6");
        regBusSjhjtableService.save(regBusSjhjtable);
        
        //获取权利人信息xml
        RegBusQlr regBusQlr = regBusQlrService.get("016efb4bc6f74f8d91966134dbc1d219");
        StringBuilder sbStr = new StringBuilder();
        
        //报文头转化
        String retHead = JaxbMapper.toXml(regBusSjhjtable, RegBusSjhjtable.class, "UTF-8");
        //报文体-权利人信息转化
        String retQlr = JaxbMapper.toXml(regBusQlr, RegBusQlr.class, "UTF-8");
        
        sbStr.append(JXmlUtils.messageBStr).append(retHead.substring(56));
        sbStr.append(JXmlUtils.dataBStr).append(retQlr.substring(56))
        	 .append(JXmlUtils.dataEStr).append(JXmlUtils.messageEStr);
        System.out.println(sbStr.toString());
        
        //调用生成XML报文文件的函数,参数为生成文件名和要生成的内容字符串
    	JXmlUtils.strChangeXML("Biz500115151030000001", sbStr.toString());
        return sbStr.toString();
    }
    
    @RequestMapping(value="display/hjson")
    @ResponseBody
    public String displayUserWithJson(HttpServletResponse response, Model model) throws Exception{  
        List<RegBaseH> regBaseHs = restfulService.getRegBaseH();
        String ret = JsonMapper.toJsonString(regBaseHs);
        //model.addAttribute("regBaseHs", ret);
        //modelMap.put("regBaseHs", ret);//已经有已引用的户信息记录 
        return ret;
    }
    
    @RequestMapping(value="display/mydata")
    public void displayUserWithJson(HttpServletRequest request, HttpServletResponse response) throws Exception{  
	    response.setContentType("text/html;charset=utf-8");
	    request.setCharacterEncoding("utf-8");
	    response.setCharacterEncoding("utf-8");    
	    PrintWriter out = response.getWriter();  
	    String userName = request.getParameter("username");  
	    String passWord = request.getParameter("password");  
	    System.out.println("userName:"+userName);
	    System.out.println("passWord:"+passWord);
	    
	    Subject currentUser = SecurityUtils.getSubject();
	    if (!currentUser.isAuthenticated()) {
	    	//collect user principals and credentials in a gui specific manner
	    	//such as username/password html form, X509 certificate, OpenID, etc.
	    	//We'll use the username/password example here since it is the most common.
	    	//(do you know what movie this is from? ;)
	    	UsernamePasswordToken token = new UsernamePasswordToken(userName, passWord);

	    	//this is all you have to do to support 'remember me' (no config - built in!):
	    	token.setRememberMe(true);
	    	LoginController loginc = new LoginController();
	    	loginc.loginFail(request, response, null);
    	}
	    //调用应用中的登录函数
	    
	    out.print("login successful!");
	    out.flush();
	    out.close();
	}
    
    
    
}