通过配置的容器过滤

=========================================通过配置的容器过滤????????????????????????????????????

//过滤器的seevlet形式,

<servlet>

<servlet-name>RESTEntrance</servlet-name>

<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

<init-param>

<param-name>javax.ws.rs.Application</param-name>

<param-value>com.certusnet.nfv.mano.vim.RestApplication</param-value>//在第三容器中,注入过滤器监听(监听注册类),即可实现代码加强,用这个注册类注册的类来加强目标方法1,注册所有rest接口,2,注册接口调用时,同时调用的加强代码(实现ContainerResponseFilter才可)

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>RESTEntrance</servlet-name>

privatestaticfinalStringSUBSYS_NFVO="nfvo";

privatestaticfinalStringSUBSYS_VIM="vim";

privatestaticfinalStringSUBSYS_VNFM="vnfm";

<url-pattern>/rest/*</url-pattern>

</servlet-mapping>

//加强代码

packagecom.certusnet.nfv.mano.rest;

importjava.io.IOException;

importjava.net.URI;

importjava.util.Arrays;

importjava.util.Date;

importjava.util.List;

importjava.util.Map;

importjavax.servlet.http.HttpServletRequest;

importjavax.ws.rs.container.ContainerRequestContext;

importjavax.ws.rs.container.ContainerResponseContext;

importjavax.ws.rs.container.ContainerResponseFilter;

importjavax.ws.rs.core.Context;

importjavax.ws.rs.core.Response.Status;

importjavax.ws.rs.ext.Provider;

importnet.sf.json.JSONArray;

importnet.sf.json.JSONObject;

importorg.apache.commons.lang.StringUtils;

importorg.apache.log4j.Logger;

importorg.springframework.beans.factory.annotation.Autowired;

importcom.certusnet.nfv.mano.ManoThreadLocal;

importcom.certusnet.nfv.mano.common.logicapi.IOperationLogLS;

importcom.certusnet.nfv.mano.common.model.EventNameId;

importcom.certusnet.nfv.mano.common.model.OperationLog;

importcom.certusnet.nfv.mano.util.DateUtil;

/**

*

*

*@authorBiwei

*

*/

@Provider

publicclassRestResponseFilterimplementsContainerResponseFilter{

Loggerlogger=Logger.getLogger(RestResponseFilter.class);

@Autowired(required=true)

privateIOperationLogLSoperationLogLS;

@Context

HttpServletRequestrequest;

privatestaticfinalStringSUBSYS_NFVO="nfvo";

privatestaticfinalStringSUBSYS_VIM="vim";

privatestaticfinalStringSUBSYS_VNFM="vnfm";

@Override

publicvoidfilter(ContainerRequestContextrequestContext,

ContainerResponseContextresponseContext)throwsIOException{

//

capsuitResult(responseContext);

//

printLog(requestContext);

//

recordOperLog(requestContext,responseContext);

}

privatevoidcapsuitResult(ContainerResponseContextresponseContext){

if(responseContext.getStatus()>=200

&&responseContext.getStatus()<300){

//

responseContext.setStatus(Status.OK.getStatusCode());

RestResponsePOrpo=newRestResponsePO();

rpo.setResult(responseContext.getEntity());

responseContext.setEntity(rpo);

}elseif(responseContext.getStatus()==ManoExceptionMapper.MANOEXCEPTION_STATUS){

//

responseContext.setStatus(Status.OK.getStatusCode());

}

}

/**

*

*

*@paramrequestContext

*/

privatevoidprintLog(ContainerRequestContextrequestContext){

URIuri=requestContext.getUriInfo().getAbsolutePath();

Stringurl=uri.getHost()+":"+uri.getPort()+uri.getPath();

logger.debug(url+"sendresponsetoUI.");

}

/**

*

*

*@paramrequestContext

*@paramresponseContext

*/

privatevoidrecordOperLog(ContainerRequestContextrequestContext,

ContainerResponseContextresponseContext){

StringuserId=requestContext.getHeaderString("User-Id");

StringeventId=requestContext.getHeaderString("Event-Id");

StringoperType=requestContext.getHeaderString("Operate-Type");

Stringsubsystem=retrieveSubsys(requestContext);

if(StringUtils.isEmpty(operType)){

return;

}

//

StringrequestContent=getRequestContent();

//

RestResponsePOrpo=(RestResponsePO)responseContext.getEntity();

StringresponseStatus=rpo.getRetCode();

StringresponseContent="ok".equals(responseStatus)?getJsonStr(rpo

.getResult()):rpo.getMsg();

try{

OperationLogoperLog=newOperationLog();

operLog.setEventId(eventId);

operLog.setOperateType(operType);

operLog.setRequestContent(requestContent);

operLog.setResponseContent(responseContent);

operLog.setResponseStatus(responseStatus);

operLog.setSubsystem(subsystem);

operLog.setUserId(userId);

operationLogLS.insertOne(operLog);

}catch(Exceptionex){

logger.error(ex.getMessage(),ex);

}

}

privateStringretrieveSubsys(ContainerRequestContextrequestContext){

URIuri=requestContext.getUriInfo().getAbsolutePath();

Stringpath=uri.getPath();

if(path.contains(SUBSYS_NFVO)){

returnSUBSYS_NFVO;

}elseif(path.contains(SUBSYS_VNFM)){

returnSUBSYS_VNFM;

}else{

returnSUBSYS_VIM;

}

}

privateStringgetRequestContent(){

Objectparam=ManoThreadLocal.getRequestParam();

if(param!=null)

{

returngetJsonStr(param);

}

else

{

return"requestparamsarenotcollected.";

}

}

privateStringgetJsonStr(Objectobj){

//

if(objinstanceofArrays||objinstanceofList){

JSONArrayjsonObj=JSONArray.fromObject(obj);

returnjsonObj.toString();

}else{

JSONObjectjsonObj=JSONObject.fromObject(obj);

returnjsonObj.toString();

}

}

}

//注册织入代码,这个具有注册过滤器功能的类,一方面需要继承ResourceConfig;另一方面需要在他监听的容器内注入

packagecom.certusnet.nfv.mano.nfvo;

importorg.codehaus.jackson.jaxrs.JacksonJsonProvider;

importorg.glassfish.jersey.server.ResourceConfig;

importorg.springframework.web.filter.RequestContextFilter;

importcom.certusnet.nfv.mano.nfvo.log.rest.OperationLogRest;

importcom.certusnet.nfv.mano.nfvo.network.rest.NetworkRest;

importcom.certusnet.nfv.mano.nfvo.nsd.rest.NsdRest;

importcom.certusnet.nfv.mano.nfvo.nsr.rest.NsrRest;

importcom.certusnet.nfv.mano.nfvo.pnfd.rest.PnfdRest;

importcom.certusnet.nfv.mano.nfvo.vnfd.rest.LifecycleEventRest;

importcom.certusnet.nfv.mano.nfvo.vnfd.rest.VduRest;

importcom.certusnet.nfv.mano.nfvo.vnfd.rest.VnfcRest;

importcom.certusnet.nfv.mano.nfvo.vnfd.rest.VnfdRest;

importcom.certusnet.nfv.mano.nfvo.vnfr.rest.VdurRest;

importcom.certusnet.nfv.mano.nfvo.vnfr.rest.VnfrRest;

importcom.certusnet.nfv.mano.rest.ManoExceptionMapper;

importcom.certusnet.nfv.mano.rest.RestRequestFilter;

importcom.certusnet.nfv.mano.rest.RestResponseFilter;

publicclassRestApplicationextendsResourceConfig{

/**

*RegisterJAX-RSapplicationcomponents.

*/

publicRestApplication(){

register(RequestContextFilter.class);

register(ManoExceptionMapper.class);

register(RestResponseFilter.class);

register(RestRequestFilter.class);

register(JacksonJsonProvider.class);

register(NsdRest.class);

register(PnfdRest.class);

register(VnfdRest.class);

register(LifecycleEventRest.class);

register(VduRest.class);

register(VnfcRest.class);

register(NsrRest.class);

register(VnfrRest.class);

register(VdurRest.class);

register(NetworkRest.class);

register(OperationLogRest.class);

}

}

相关推荐