Skip to content

Random Services Notes

Jinlian(Sunny) Wang edited this page Dec 27, 2018 · 13 revisions

References

Rule Engine

Run Services

mvn clean install to build and mvn package -P devint-test cargo:run to run from within the “web” module; to debug mvnDebug package -P devint-test cargo:run, and then set up Remote Java Application to connect to the listening port - 8000. See this stackoverflow for arguments - MAVEN_DEBUG_OPT=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000

Use -DskipTests to skip tests.

Attach source code and docs

Simple, get your sources and JavaDocs:

mvn dependency:resolve -Dclassifier=javadoc -Dclassifier=sources

To download sources for your dependencies: mvn eclipse:eclipse -DdownloadSources=true

To attach sources to an installation: mvn source:jar install

From this stackoverflow

Session Validation

@Component
public class SessionValidationInterceptorAdapter extends HandlerInterceptorAdapter {

  private static final Logger logger = LoggerFactory.getLogger(SessionValidationInterceptorAdapter.class);


  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
      throws Exception {
    
    HandlerMethod method = (HandlerMethod) handler;
    SessionValidationToken methodAnnotation =
        method.getMethodAnnotation(SessionValidationToken.class);

    GroupSessionValidationToken classAnnotation =
        method.getMethod().getDeclaringClass().getAnnotation(GroupSessionValidationToken.class);

		if (methodAnnotation != null || classAnnotation != null) {

			// Decipher and validate the session token.
			try {
				UserToken userToken = new UserToken();
				token = request.getHeader("Authorization").trim();

				//decrypt the token and check its validity
				if (this.isValid(userToken)) {
					return true;
				}
			} finally {
				response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
				return false;
			}
  }

  @Override
  public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
      ModelAndView modelAndView) throws Exception {

  }

}

Spring AOP and @AspectJ

  1. Define Aspect Config
@Configuration
@EnableAspectJAutoProxy
@ComponentScan(basePackages="com.concretepage")
public class AspectConfig {
}  
  1. Define annotation
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Auditable {
    AuditCode value();
}
  1. Define advice with point cut expression
@Before("com.xyz.lib.Pointcuts.anyPublicMethod() && @annotation(auditable)")
public void audit(Auditable auditable) {
    AuditCode code = auditable.value();
    // ...
}
  1. Note that point cut can be defined separately from Advice -
@Pointcut("com.xyz.lib.Pointcuts.anyPublicMethod() && @annotation(auditable)")
public void pointcutDemo() {}

@Before("pointcutDemo())")
public void audit(Auditable auditable) {
    AuditCode code = auditable.value();
    // ...
}
Clone this wiki locally