Facade门面系统透传接口

import com.sun.jersey.api.core.InjectParam;
import com.sun.jersey.api.representation.Form;
import com.sun.jersey.server.impl.application.WebApplicationContext;
import com.sun.jersey.spi.container.ContainerRequest;


import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriInfo;


@Path("pass")
public class PassResource {


	@InjectParam
	private FacadeProvider facadeProvider;
    
    @GET
	@Path("/{server}/{path:.*}")
	@Produces(value = MediaType.APPLICATION_JSON)
    public String rarvPass(@PathParam("server") String server, @PathParam("path") String path, @Context HttpServletRequest request){
    	BaseFacade facade = facadeProvider.getFacade(server);
    	if(facade != null){
    		FacadeParams param = new FacadeParams().withUrl(path).withUser(currentUser().getId());
    		Enumeration<String> paramNames = request.getParameterNames();
 	        while (paramNames.hasMoreElements()) {
 	        	 String paramName = paramNames.nextElement();
 	        	 String[] paramValues = request.getParameterValues(paramName);  
 	        	 for(String v : paramValues){
 	        		 param.addQueryParam(paramName, v);
 	        	 }
 	        }
    		return facade.getString(param);
    	}else{
    		return String.format("ERROR : wrong server (%s).", server);
    	}
    }
    
    @POST
	@Path("/post/{server}/{path:.*}")
	@Produces(value = MediaType.APPLICATION_JSON)
    public String rarvPostPass(@PathParam("server") String server, @PathParam("path") String path, @Context HttpServletRequest request,@Context UriInfo ui){
        BaseFacade facade = facadeProvider.getFacade(server);
        Form form = ((WebApplicationContext)ui).getContainerRequest().getFormParameters();
    	if(facade != null){
    		FacadeParams param = new FacadeParams().withUrl(path).withUser(currentUser().getId());
            if(form != null){
                for(Map.Entry<String,List<String>> entry : form.entrySet()){
                    for(String tmp : entry.getValue()){
                        param.addFormValue(entry.getKey(),tmp);
                    }
                }
            }
    		return facade.post(param);
    	}else{
    		return String.format("ERROR : wrong server (%s).", server);
    	}
    }
}
Postman请求数据时使用x-www-form-urlencoded

你可能感兴趣的:(门面系统,接口透传)