How to setup ajaxFilter in DWR? - java

I create a DWR ajaxfilter, but not sure how to config it with Spring. DWR version is 2.0.1, and i hope config it as a globel filter, so when session is expired, i can catch this exception in client side.
package com.gbtags;
import org.directwebremoting.AjaxFilter;
import org.directwebremoting.AjaxFilterChain;
import org.directwebremoting.WebContextFactory;
import org.directwebremoting.extend.LoginRequiredException;
import java.lang.reflect.Method;
public class DWRSessionFilter implements AjaxFilter {
public Object doFilter(Object obj, Method method, Object[] params, AjaxFilterChain chain) throws Exception {
//Check if session has timedout/invalidated
if( WebContextFactory.get().getSession( false ) == null ) {
System.out.println("session expired");
//Throw an exception
throw new LoginRequiredException( "This operation requires login." );
}
return chain.doFilter(obj, method, params);
}
}

add filter to dwr.xml as below:

Related

Amazon SQS call lambda function

I am trying to trigger function thru amazon sqs trigger. The trigger is working fine but, the message is not passed into the my function.
Here is my lambda function
import java.text.SimpleDateFormat;
import java.util.Calendar;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.RequestHandler;
public class x implements RequestHandler<RequestClass, ResponseClass> {
private LambdaLogger logger;
public void log(String message) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
logger.log(sdf.format(cal.getTime()) + " " + message+"\n");
}
public ResponseClass handleRequest(RequestClass request, Context context) {
this.logger = context.getLogger();
log("Request " + request);
if (request == null || (request.getFilename() == null && request.getRecords() == null)) {
log("No file was passed in");
throw new RuntimeException("No file was passed in");
}
return new ResponseClass(null);
}
}
And request class is https://pastebin.com/Q1G6bnrA
The records are always null when I see logs.
Have you taken care of the execution role permissions of the Lambda?
From here:
Execution Role Permissions
Lambda needs the following permissions to manage messages in your Amazon SQS queue. Add them to your function's execution role.
sqs:ReceiveMessage
sqs:DeleteMessage
sqs:GetQueueAttributes
The following code is working fine for me:
package au.com.redbarn.aws.lambda2lambda_via_sqs;
import java.util.List;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.SQSEvent;
import com.amazonaws.services.lambda.runtime.events.SQSEvent.SQSMessage;
import lombok.extern.log4j.Log4j2;
#Log4j2
public class SQSConsumerLambda implements RequestHandler<SQSEvent, String> {
#Override
public String handleRequest(SQSEvent input, Context context) {
log.info("message received");
List<SQSMessage> records = input.getRecords();
for (SQSMessage record : records) {
log.info(record.getBody());
}
return "Ok";
}
}
Maybe try using SQSEvent instead of your own RequestClass.

How do I mock a Session in Ratpack with RequestFixture?

What I'm trying to do is test an authentication handler, but my problem boils down to having no Session instance in the registry.
An example test:
package whatever
import groovy.transform.CompileStatic
import ratpack.groovy.handling.GroovyChainAction
import ratpack.groovy.test.handling.GroovyRequestFixture
import ratpack.http.Status
import ratpack.session.Session
import spock.lang.Specification
class SessionChainTest extends Specification {
GroovyChainAction sessionChain = new GroovyChainAction() {
#Override
#CompileStatic
void execute() throws Exception {
get('foo') {
Session s = get Session
// Stuff using session here
}
}
}
def "should get session"() {
given:
def result = GroovyRequestFixture.handle(sessionChain) {
uri 'foo'
method 'GET'
}
expect:
result.status == Status.OK
// If the server threw, rethrow that
Throwable t = result.exception(Throwable)
if (t) throw t // <<< Throws NotInRegistryException because no Session in registry
}
}
(The extra rethrow is in there to allow us to see the exception thrown within the ratpack test, because by default it is caught and stashed in the result.)
I know that in principle I could create a Session instance and add it to the registry with a registry { add <Session instance> } block, but I've delved into the Ratpack code, and creating a Session object requires getting a lot of disparate other components and passing them to SessionModule#sessionAdaptor (or the DefaultSession constructor). I can't find any examples of that being done, it appears this call is handled by Guice dependency-injection magic I can't unpick.
The usual way to do it in an application is to use a bind { module SessionModule } block but this isn't accessible from the context of RequestFixture#execute.
As sessions are bread and butter for any web application, my hunch is that this may be an easily solved problem, I just haven't found the right way to do it?
You can access Registry through GroovyRequestFixture.handle(handler, closure) method call and you can e.g. register mocked Session object:
GroovyRequestFixture.handle(sessionChain) {
uri 'foo'
method 'GET'
registry { r ->
r.add(Session, session)
}
}
Take a look at following example:
import groovy.transform.CompileStatic
import ratpack.exec.Promise
import ratpack.groovy.handling.GroovyChainAction
import ratpack.groovy.test.handling.GroovyRequestFixture
import ratpack.http.Status
import ratpack.jackson.internal.DefaultJsonRender
import ratpack.session.Session
import spock.lang.Specification
import static ratpack.jackson.Jackson.json
class SessionChainTest extends Specification {
Session session = Mock(Session) {
get('test') >> Promise.value(Optional.of('Lorem ipsum'))
}
GroovyChainAction sessionChain = new GroovyChainAction() {
#Override
#CompileStatic
void execute() throws Exception {
get('foo') {
Session s = get Session
s.get('test').map { Optional<String> o ->
o.orElse(null)
}.flatMap { value ->
Promise.value(value)
}.then {
render(json([message: it]))
}
}
}
}
def "should get session"() {
given:
def result = GroovyRequestFixture.handle(sessionChain) {
uri 'foo'
method 'GET'
registry { r ->
r.add(Session, session)
}
}
expect:
result.status == Status.OK
and:
result.rendered(DefaultJsonRender).object == [message: 'Lorem ipsum']
}
}
In this test I mock Session object for key test to store Lorem ipsum text. When running this test, both assertions pass.
Alternative approach: registering Guice.registry()
If you don't want to use mocked Session object you can try replacing default Ratpack's Registry with a Guice registry object. Firstly, initialize a function that creates Guice registry and add SessionModule via bindings:
static Function<Registry, Registry> guiceRegistry = Guice.registry { bindings ->
bindings.module(new SessionModule())
}
Next inside execute() method of GroovyChainAction you can replace the default registry by calling:
register(guiceRegistry.apply(registry))
No mocks anymore, but in this case you can't access Session object outside request scope, so you wont be able to add anything to the session in preparation stage of your test. Below you can find full example:
import groovy.transform.CompileStatic
import ratpack.exec.Promise
import ratpack.func.Function
import ratpack.groovy.handling.GroovyChainAction
import ratpack.groovy.test.handling.GroovyRequestFixture
import ratpack.guice.Guice
import ratpack.http.Status
import ratpack.jackson.internal.DefaultJsonRender
import ratpack.registry.Registry
import ratpack.session.Session
import ratpack.session.SessionModule
import spock.lang.Specification
import static ratpack.jackson.Jackson.json
class SessionChainTest extends Specification {
static Function<Registry, Registry> guiceRegistry = Guice.registry { bindings ->
bindings.module(new SessionModule())
}
GroovyChainAction sessionChain = new GroovyChainAction() {
#Override
#CompileStatic
void execute() throws Exception {
register(guiceRegistry.apply(registry))
get('foo') {
Session s = get Session
s.get('test').map { Optional<String> o ->
o.orElse(null)
}.flatMap { value ->
Promise.value(value)
}.then {
render(json([message: it]))
}
}
}
}
def "should get session"() {
given:
def result = GroovyRequestFixture.handle(sessionChain) {
uri 'foo'
method 'GET'
}
expect:
result.status == Status.OK
and:
result.rendered(DefaultJsonRender).object == [message: null]
}
}
Hope it helps.

How to you return a customized error page when a MySQL disconnects while a user is using an app?

Instead of showing an 500 screen that says there is a connectionException, I would like to customize a view for this specific situation.
I currently have an CustomErrorController which redirects a user to the top page which is /todoLists when the user tries to access a URL that does not exist.
I'm having trouble with implementing the code which returns a view in the error directory titled database.html.
package com.teamlab.todolist.web;
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import javax.servlet.http.HttpServletRequest;
import java.net.ConnectException;
#Controller
public class CustomErrorController implements ErrorController {
#GetMapping("/error")
String backToTopPage() {
return "redirect:/todoLists";
}
public String getErrorPath() {
return "/internalServerError";
}
//
// #ExceptionHandler(Exception.class)
// #ResponseStatus(value=HttpStatus.INTERNAL_SERVER_ERROR)
// public String connectionErrorHandling(ConnectException e) {
// return "/internalServerError";
// }
#ExceptionHandler(ConnectException.class)
public String connectionErrorHandling(HttpServletRequest request, Exception ex) {
return "/internalServerError";
}
}
Since I don't know how you structured your project and I don't know if your connectionErrorHandling-method is called, I assume that the handling is not called, because the endpoint, causing this exception, is declared in another controller.
If that's the case, you're looking for #ControllerAdvice and #ExceptionHandler
#ControllerAdvice
public class GlobalExceptionHandler {
#ExceptionHandler(TheExceptionYouWantToHandle.class)
public String handleYourException(HttpServletRequest request, Exception ex){
return "name_of_view_you_want_to_show";
}
}
This will catch the exception you want to catch and redirect to the view with the name you return by your #ExceptionHandler
If you just want to handle exceptions inside a specific controller, or you want to handle the same exception in different controllers in a different way, you can declare your #ExceptionHandlerinside this controller. When you use #ControllerAdvice the #ExceptionHandlers are registered for all controllers.

java getSession().setAttribute() [duplicate]

This question already has answers here:
NullPointerException when setting attribute?
(5 answers)
Closed 7 years ago.
I have to make a litle change to an existing project(tomcat and java WebApplication).
now, in loginForm, if users type correct login and password, it is Ok,
to users wil be shown main page. But when any user types incorrect password,
or may be his account is temporarily locked, so to user again wil be shown loginform,
user can not know why he cannot log in, by what cause he can not login.
(for example "invalid username/password","user account locked",...).
now i want to insert the session error message that includes also causes of why user cannot log in.
insert(save) to session an attribute named "LoggingError".
i am writing as:
request.getSession().setAttribute("LoggingError", message);
but when running application, in this line
request.getSession().setAttribute("LoggingError", message);
occurs error in web page:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
com.se.eee.security.EeeAuthenticationProvider.authenticate(EeeAuthenticationProvider.java:153)
net.sf.acegisecurity.providers.ProviderManager.doAuthentication(ProviderManager.java:159)
net.sf.acegisecurity.AbstractAuthenticationManager.authenticate(AbstractAuthenticationManager.java:49)
net.sf.ace
...
...
here java code of EeeAuthenticationProvider.java
package com.se.eee.security;
import net.sf.acegisecurity.*;
import net.sf.acegisecurity.providers.AuthenticationProvider;
import net.sf.acegisecurity.providers.UsernamePasswordAuthenticationToken;
import net.sf.acegisecurity.providers.dao.User;
import net.sf.acegisecurity.providers.dao.UsernameNotFoundException;
import net.sf.acegisecurity.providers.dao.event.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import com.se.eee.bus.*;
import com.se.eee.bus.SecurityManager;
import com.se.spring.datasource.core.MakeConnectionException;
import com.se.spring.ext.CurrentRequestContext;
import com.opensymphony.webwork.interceptor.SessionAware;
import com.opensymphony.webwork.interceptor.ServletRequestAware;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
public class EeeAuthenticationProvider implements AuthenticationProvider, SessionAware, ServletRequestAware {
private static Log log = LogFactory.getLog(EeeAuthenticationProvider.class);
private JDBCProperties jdbcProp;
private ApplicationContext context;
private SecurityManager securityManager;
private HttpServletRequest request;
public void setServletRequest(HttpServletRequest req) {
this.request = req;
}
public void setSession(Map session) {
//To change body of implemented methods use File | Settings | File Templates.
}
public void setSecurityManager(SecurityManager securityManager) {
this.securityManager = securityManager;
}
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.context = applicationContext;
}
public void setJdbcProp(JDBCProperties jdbcProp) {
this.jdbcProp = jdbcProp;
}
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
// Determine username
// log.warn((authentication.isAuthenticated()?"Already Authenticated. Skip it!":"")+"authenticate: "+authentication);
if(authentication.isAuthenticated()) {
//log.warn("Already Authenticated. Skip it!");
return authentication;
}
String username = "NONE_PROVIDED";
if (authentication.getPrincipal() != null) {
username = authentication.getPrincipal().toString();
}
if (authentication.getPrincipal() instanceof UserDetails) {
username = ((UserDetails) authentication.getPrincipal()).getUsername();
}
UserDetails user = null;
com.se.eee.bus.User principal=null;
try
{
JDBCProperties props = jdbcProp.deserialize();
String input_passwords= authentication.getCredentials().toString();
String[] psd = input_passwords.split(":");
Filial fil = props.getFilial(psd[1]);
String sgn = input_passwords;
int i= sgn.indexOf(":", 1);
sgn = sgn.substring(i+1,sgn.length());
i= sgn.indexOf(":", 1);
sgn = sgn.substring(i+1,sgn.length());
if(fil==null)username=null;
securityManager.makeConnect(username, psd[0], fil);
user=new User(username, "skipped",true,true,true,true, new GrantedAuthority[]{new GrantedAuthorityImpl("ROLE_USER")});
//set connection for DataSource
ContextDataBean dataBean=(ContextDataBean)CurrentRequestContext.get();
dataBean.setUserKey(username+fil.id);
principal=securityManager.getUserByLogin(username.toUpperCase());
if(principal == null) throw new UsernameNotFoundException("Couldn't login.");
principal.setLogin(username);
principal.setPassword("******");
//principal.setBranch(fil.id);
if (principal.getBanktype().equals("055"))
{
if ( sgn!=null && sgn.length() != 0)
{
securityManager.insUserKey(principal.getBranch(), principal.getId(), sgn);
com.se.eee.bus.Document docum = new com.se.eee.bus.Document();
docum.setBranch(principal.getBranch());
docum.setEmpId(principal.getId());
docum.setErrCode("991");
docum = securityManager.getAnswerUserKey(docum);
if (!docum.getErrCode().equals("000")) throw new UsernameNotFoundException("Key code error. User: "+principal.getLogin());
}
else
{
throw new UsernameNotFoundException("error while inserting test key code. please touch i-key or check loginform.ftl. user: "+principal.getLogin());
}
}
}
catch (MakeConnectionException mex)
{
log.error(mex.getMessage());
if (this.context != null) {
context.publishEvent(new AuthenticationFailureUsernameOrPasswordEvent(authentication, new User("".equals(username)? "EMPTY_STRING_PROVIDED" : username, "*****", false, false, false, false, new GrantedAuthority[0])));
}
throw new BadCredentialsException("Couldn't login connection problem.");
}
catch(Exception ex)
{
Throwable cause=ex.getCause();
String message=null;
if(cause!=null)message = cause.getMessage();
else message = ex.toString();
log.error(message);
// здес я пытаюс написать в session
request.getSession().setAttribute("LoggingError", message);
// но код не компилируется
throw new UsernameNotFoundException("Couldn't login.");
}
return createSuccessAuthentication(principal, authentication, user);
}
protected Authentication createSuccessAuthentication(Object principal, Authentication authentication, UserDetails user) {
UsernamePasswordAuthenticationToken result = new UsernamePasswordAuthenticationToken(principal, authentication.getCredentials(), user.getAuthorities());
result.setDetails((authentication.getDetails() != null) ? authentication.getDetails() : null);
result.setAuthenticated(true);
return result;
}
public boolean supports(Class aClass) {
if (UsernamePasswordAuthenticationToken.class.isAssignableFrom(aClass)) return true;
return false;
}
}
If your request object is a HttpServletRequest object then this should work.
If this isn't the problem can you send the exact code snippet (shouldn't need the whole program) and the exact error message?
this should work.
request.getSession(true).setAttribute("LoggingError", message);
Is your authentication provider specified as prototype scope bean? Not sure how Struts / WebWork is exactly integrated with Spring, but if your bean is singleton, it can not work.
In other words, make sure setServletRequest is called.
And by the way this application must be pretty old, if it has such package names as it has.

Getting server name in ContextLoaderListener

My listener is filling Cache (Terracota) and if something goes wrong at application start, ExceptionInInitializerError is thrown. I would like to get server name (like on HttpServletRequest - getServerName()) to know where this happened.
How can I come to this information??
import javax.servlet.ServletContextEvent;
import net.f.core.service.util.CacheUtil;
import org.apache.log4j.Logger;
import org.springframework.web.context.ContextLoaderListener;
/**
* Application Lifecycle Listener implementation class OnContextLoadListener
*
*/
public class OnContextLoadListener extends ContextLoaderListener {
private static final Logger log = Logger
.getLogger(OnContextLoadListener.class);
#Override
public void contextDestroyed(
#SuppressWarnings("unused") ServletContextEvent sce) {
// nothing here
}
#Override
public void contextInitialized(
#SuppressWarnings("unused") ServletContextEvent sce) {
try {
CacheUtil.getInstance();
} catch (ExceptionInInitializerError e) {
log.error("Problem with application start!", e);
// notify me
}
}
The server hostname is part of the request, as it depends on what URL the client used to reach your host.
If you are interested in the local hostname, you can try:
String hostname = InetAddress.getLocalHost().getHostName();
HttpServletRequest.getServerName():
Returns the host name of the server to
which the request was sent.
Its not a property of the server itself, it's a property of the request. It makes no sense outside of the context of the ContextLoaderListener.
What information are you actually looking for?
Simply:
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
....
ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest req = sra.getRequest();
String serverName = req.getServerName();
If you're just trying to determine if you're on localhost:
boolean isLocalHost = "localhost/127.0.0.1".equals(InetAddress.getLoopbackAddress().toString());

Categories

Resources