views:

126

answers:

1

I am using Wicket 1.4.9 + Spring 3 + iBatis. I want to get session data before used transactional but I have a big problem because this data put on Wicket web session(this data choose on login page).

I am using spring aop for point cut but I cannot point cut session per client. How to solve this problem.

User Class Entity

package com.app.entity;

import java.io.Serializable;
import java.util.Date;

public class User implements Serializable {

    private String name;
    private String lastname;
    private String username;
    private String password;
    private String crtUser;
    private Date crtDate;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getCrtUser() {
        return crtUser;
    }

    public void setCrtUser(String crtUser) {
        this.crtUser = crtUser;
    }

    public Date getCrtDate() {
        return crtDate;
    }

    public void setCrtDate(Date crtDate) {
        this.crtDate = crtDate;
    }

}

Web App Class

package com.app.conf;

import org.apache.wicket.Page;
import org.apache.wicket.Request;
import org.apache.wicket.Response;
import org.apache.wicket.Session;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;

import com.app.page.UserLoginPage;

public class WebApp extends WebApplication {

    @Override
    protected void init() {
        addComponentInstantiationListener(new SpringComponentInjector(this));
    }

    @Override
    public Class<? extends Page> getHomePage() {
        return UserLoginPage.class;
    }

    @Override
    public Session newSession(Request request, Response response) {
        return new CustomSession(request);
    }

}

Custom session class

package com.app.conf;

import org.apache.wicket.Request;
import org.apache.wicket.protocol.http.WebSession;

import com.app.entity.User;

public class CustomSession extends WebSession {

    private User user;

    public CustomSession(Request request) {
        super(request);
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

}

User Login Page : Keep Session when Login

package com.app.page;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;

import com.app.conf.CustomSession;
import com.app.entity.User;

public class UserLoginPage extends WebPage {

    private LoginForm form;

    public UserLoginPage() {
        form = new LoginForm("form");
        add(form);
    }

    private class LoginForm extends Form{

        private User user;

        private TextField<String> username;
        private PasswordTextField password;
        private AjaxButton submitButton;

        public LoginForm(String id) {
            super(id);

            user = new User();

            username = new TextField<String>("username",
                    new PropertyModel<String>(this, "user.username"));
            add(username);

            password = new PasswordTextField("password" , 
                    new PropertyModel<String>(this, "user.password"));
            add(password);

            submitButton = new AjaxButton("submitButton", this) {

                @Override
                protected void onSubmit(AjaxRequestTarget arg0, Form<?> arg1) {
                    getCustomSession().setUser(user);
                    setResponsePage(new UserAddPage());
                }
            };
            add(submitButton);
        }
    }

    public CustomSession getCustomSession(){
        return (CustomSession)getSession();
    }

}

User add page : i want point cut insert on Service Class method and get session for add Create user and Create date on User Entity Class

package com.app.page;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.app.entity.User;
import com.app.service.IUserService;

public class UserAddPage extends WebPage {

    @SpringBean private IUserService userService;

    private UserAddForm form;

    public UserAddPage() {
        form = new UserAddForm("form");
        add(form);
    }

    private class UserAddForm extends Form {

        private User user;

        private TextField<String> name;
        private TextField<String> lastname;
        private TextField<String> username;
        private PasswordTextField password;
        private AjaxButton submitButton;

        public UserAddForm(String id) {
            super(id);
            user = new User();

            name = new TextField<String>("name",
                    new PropertyModel<String>(this, "user.name"));
            add(name);

            lastname = new TextField<String>("lastname" , 
                    new PropertyModel<String>(this, "user.lastname"));
            add(lastname);

            username = new TextField<String>("username",
                    new PropertyModel<String>(this, "user.username"));
            add(username);

            password = new PasswordTextField("password" , 
                    new PropertyModel<String>(this, "user.password"));
            add(password);

            submitButton = new AjaxButton("submitButton", this) {

                @Override
                protected void onSubmit(AjaxRequestTarget arg0, Form<?> arg1) {
                    /*
                     * HOW TO POINT CUT THIS METHOD AND GET SESSION BEFORE INSERT
                     */
                    userService.insert(user);
                }
            };
            add(submitButton);
        }

    }

}

User Aspect

package com.app.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

import com.app.entity.User;

@Aspect
public class UserAspect {

    @Pointcut("execution(* com.app.service..*.insert*(..))&&" + "args(user)")
    public void userPointcut(User user){}

    @Around("userPointcut(user)")
    public Object aroundUser(ProceedingJoinPoint pj, User user) throws Throwable{
        /*
         * How to get Create User from Session
         */
        return pj.proceed();
    }

}
A: 

WebApp.getSession().getUser() ?

I'd imagine that you want to register a bean in the spring context that can supply an authorised user. This can retrieve the user from your WebApp session (the WebApp instance would also be a registered bean, but I assume it is already). There should be plenty of information on this use-case in the spring docs/community.

For better answers, you should clean up your question (use of code blocks) and state the use case.

ireddick