Java Code Examples for org.nutz.mvc.Mvcs#getReq()

The following examples show how to use org.nutz.mvc.Mvcs#getReq() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JWTUtil.java    From NutzSite with Apache License 2.0 6 votes vote down vote up
/**
 *  获取ID
 * @return
 */
public static String getId() {
    HttpServletRequest request = Mvcs.getReq();
    Map<String, String> map = new HashMap<String, String>();
    Enumeration headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String key = (String) headerNames.nextElement();
        String value = request.getHeader(key);
        map.put(key, value);
    }
    try{
        String token=map.get("authorization");
       if(verifyToken(token)){
           Claims claims = Jwts.parser()
                   .setSigningKey(key)
                   .parseClaimsJws(token).getBody();
           return  claims.getId();
       }
    }catch (Exception e){
        log.debug(e.getMessage());
        e.printStackTrace();

    }
    return null;
}
 
Example 2
Source File: CacheSessionDAO.java    From NutzSite with Apache License 2.0 6 votes vote down vote up
@Override
  protected void doUpdate(Session session) {
  	if (session == null || session.getId() == null) {  
          return;
      }
  	
  	HttpServletRequest request = Mvcs.getReq();
if (request != null){
	String uri = Mvcs.getReq().getRequestURI();
	// 如果是静态文件,则不更新SESSION
	if (isStaticFile(uri)){
		return;
	}

}
  	super.doUpdate(session);
  	logger.debug("update {} {}", session.getId(), request != null ? request.getRequestURI() : "");
  }
 
Example 3
Source File: SessionCacheManager.java    From NutzSite with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public V get(K key) throws CacheException {
    if (key == null){
        return null;
    }

    V v = null;
    HttpServletRequest request =  Mvcs.getReq();
    if (request != null){
        v = (V)request.getAttribute(cacheKeyName);
        if (v != null){
            return v;
        }
    }

    V value = null;
    value = (V)getSession().getAttribute(cacheKeyName);
    logger.debug("get {} {} {}", cacheKeyName, key, request != null ? request.getRequestURI() : "");

    if (request != null && value != null){
        request.setAttribute(cacheKeyName, value);
    }
    return value;
}
 
Example 4
Source File: SlogService.java    From NutzSite with Apache License 2.0 5 votes vote down vote up
public SlogBean c(String t, String tag, String source, String msg) {
    SlogBean sysLog = new SlogBean();
    sysLog.setCt(new Date());
    if (t == null || tag == null || msg == null) {
        throw new RuntimeException("t/tag/msg can't null");
    }
    if (source == null) {
        StackTraceElement[] tmp = Thread.currentThread().getStackTrace();
        if (tmp.length > 3) {
            source = tmp[3].getClassName() + "#" + tmp[3].getMethodName();
        } else {
            source = "main";
        }
    }
    sysLog.setT(t);;
    sysLog.setTag(tag);;
    sysLog.setSource(source);;
    sysLog.setMsg(msg);;
    if (Mvcs.getReq() != null) {
        sysLog.setUrl(Mvcs.getReq().getRequestURI());
        sysLog.setIp(Lang.getIP(Mvcs.getReq()));
        //获取地址
        sysLog.setLocation(AddressUtils.getRealAddressByIP(sysLog.getIp()));
        Map<String, String[]> map = Mvcs.getReq().getParameterMap();
        String params = JSONObject.toJSONString(map);
        //设置参数值
        sysLog.setParam(StringUtils.substring(params, 0, 255));
        UserAgent userAgent = UserAgent.parseUserAgentString(Mvcs.getReq().getHeader("User-Agent"));
        if(Lang.isNotEmpty(userAgent)){
            // 获取客户端操作系统
            String os = userAgent.getOperatingSystem().getName();
            sysLog.setOs(os);
            // 获取客户端浏览器
            String browser = userAgent.getBrowser().getName();
            sysLog.setBrowser(browser);
        }
    }
    return sysLog;
}
 
Example 5
Source File: CacheSessionDAO.java    From NutzSite with Apache License 2.0 5 votes vote down vote up
@Override
  protected Serializable doCreate(Session session) {
HttpServletRequest request = Mvcs.getReq();
if (request != null){
	String uri = Mvcs.getReq().getRequestURI();
	// 如果是静态文件,则不创建SESSION
	if (isStaticFile(uri)){
        return null;
	}
}
super.doCreate(session);
logger.debug("doCreate {} {}", session, request != null ? request.getRequestURI() : "");
  	return session.getId();
  }
 
Example 6
Source File: JedisSessionDAO.java    From NutzSite with Apache License 2.0 5 votes vote down vote up
@Override
protected Serializable doCreate(Session session) {
    HttpServletRequest request =  Mvcs.getReq();
    if (request != null){
        String uri = request.getServletPath();
        // 如果是静态文件,则不创建SESSION
        if (isStaticFile(uri)){
            return null;
        }
    }
    Serializable sessionId = this.generateSessionId(session);
    this.assignSessionId(session, sessionId);
    this.update(session);
    return sessionId;
}
 
Example 7
Source File: SessionCacheManager.java    From NutzSite with Apache License 2.0 5 votes vote down vote up
@Override
public V put(K key, V value) throws CacheException {
    if (key == null){
        return null;
    }

    getSession().setAttribute(cacheKeyName, value);

    if (logger.isDebugEnabled()){
        HttpServletRequest request = Mvcs.getReq();
        logger.debug("put {} {} {}", cacheKeyName, key, request != null ? request.getRequestURI() : "");
    }

    return value;
}
 
Example 8
Source File: JedisCacheManager.java    From NutzSite with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
      @Aop("redis")
public V get(K key) throws CacheException {
	if (key == null){
		return null;
	}
	
	V v = null;
	HttpServletRequest request = Mvcs.getReq();
	if (request != null){
		v = (V)request.getAttribute(cacheKeyName);
		if (v != null){
			return v;
		}
	}
	
	V value = null;
	Jedis jedis = null;
	try {
		jedis = jedisAgent.getResource();
              value = (V) jedis().hget(JedisUtils.getBytesKey(cacheKeyName),JedisUtils.getBytesKey(cacheKeyName));
		logger.debug("get {} {} {}", cacheKeyName, key, request != null ? request.getRequestURI() : "");
	} catch (Exception e) {
		logger.error("get {} {} {}", cacheKeyName, key, request != null ? request.getRequestURI() : "", e);
	} finally {
		Streams.safeClose(jedis);
	}
	
	if (request != null && value != null){
		request.setAttribute(cacheKeyName, value);
	}
	
	return value;
}
 
Example 9
Source File: ActionMethodInterceptor.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
    MethodInterceptResult result) throws Throwable {
    PathMappingCache pathMappingCache = (PathMappingCache) objInst.getSkyWalkingDynamicField();
    String requestURL = pathMappingCache.findPathMapping(method);
    if (requestURL == null) {
        At methodRequestMapping = method.getAnnotation(At.class);
        if (methodRequestMapping.value().length > 0) {
            requestURL = methodRequestMapping.value()[0];
        } else {
            requestURL = "";
        }
        pathMappingCache.addPathMapping(method, requestURL);
        requestURL = pathMappingCache.findPathMapping(method);
    }

    HttpServletRequest request = Mvcs.getReq();
    ContextCarrier contextCarrier = new ContextCarrier();
    CarrierItem next = contextCarrier.items();
    while (next.hasNext()) {
        next = next.next();
        next.setHeadValue(request.getHeader(next.getHeadKey()));
    }
    AbstractSpan span = ContextManager.createEntrySpan(requestURL, contextCarrier);
    Tags.URL.set(span, request.getRequestURL().toString());
    Tags.HTTP.METHOD.set(span, request.getMethod());
    span.setComponent(ComponentsDefine.NUTZ_MVC_ANNOTATION);
    SpanLayer.asHttp(span);
}
 
Example 10
Source File: JedisSessionDAO.java    From NutzSite with Apache License 2.0 4 votes vote down vote up
@Override
    public void update(Session session) throws UnknownSessionException {
        if (session == null || session.getId() == null) {
            return;
        }

        HttpServletRequest request = Mvcs.getReq();
        if (request != null){
            String uri = request.getServletPath();
            // 如果是静态文件,则不更新SESSION
            if (isStaticFile(uri)){
                return;
            }

            // 手动控制不更新SESSION
//            if (Global.NO.equals(request.getParameter("updateSession"))){
//                return;
//            }
        }

        Jedis jedis = null;
        try {

            jedis = jedisAgent.getResource();

            // 获取登录者编号
            PrincipalCollection pc = (PrincipalCollection)session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
            String principalId = pc != null ? pc.getPrimaryPrincipal().toString() : StringUtils.EMPTY;

            jedis.hset(sessionKeyPrefix, session.getId().toString(), principalId + "|" + session.getTimeout() + "|" + session.getLastAccessTime().getTime());
            jedis.set(JedisUtils.getBytesKey(sessionKeyPrefix + session.getId()), JedisUtils.toBytes(session));

            // 设置超期时间
            int timeoutSeconds = (int)(session.getTimeout() / 1000);
            jedis.expire((sessionKeyPrefix + session.getId()), timeoutSeconds);

            logger.debug("update {} {}", session.getId(), request != null ? request.getRequestURI() : "");
        } catch (Exception e) {
            logger.error("update {} {}", session.getId(), request != null ? request.getRequestURI() : "", e);
        } finally {
           Streams.safeClose(jedis);
        }
    }