Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import okhttp3.*;
- public privileged aspect MailAspect {
- private static final String MAIL_SERVER = "http://da-sentence.cloudapp.net/aspects/sendmail";
- pointcut newClient(String uname, String pass, String email): initialization(Customer.new(String, String, String)) && args(uname, pass, email);
- pointcut buy(): call(void ShoppingCart.pay());
- pointcut passRecovery(): call(void OnlineShopping.passRecovery());
- private static void sendMail(String mail, String subj, String body){
- try {
- OkHttpClient client = new OkHttpClient();
- MediaType mediaType = MediaType.parse("application/json");
- String req = "{\n\t\"from\" : \"OnlineShop\",\n"
- + "\t\"to\" : \"" + mail + "\",\n" +
- "\t\"subj\" : \"" + subj + "\",\n"
- + "\t\"body\" : \"" + body + "\"\n}";
- System.out.println(req);
- RequestBody reqBody = RequestBody.create(mediaType, req);
- Request request = new Request.Builder()
- .url(MAIL_SERVER)
- .post(reqBody)
- .addHeader("content-type", "application/json")
- .build();
- Response response = client.newCall(request).execute();
- } catch(Exception e){
- }
- }
- after (): passRecovery(){
- if(OnlineShopping.user == null){
- System.out.println("you are not logged in");
- return;
- }
- if(OnlineShopping.user.email == null){
- System.out.println("no registered mail");
- return;
- }
- String subj = "Your password";
- String body = "Hey " + OnlineShopping.user.getUsername() +
- "&your password is" + OnlineShopping.user.password;
- sendMail(OnlineShopping.user.getMail(), subj, body);
- }
- after (String uname, String pass, String email): newClient(uname, pass, email) {
- if (email == null)
- return;
- String subj = "Welcome to onelinshop";
- String body = "Hey " + uname + "&Welcome to our shop!";
- sendMail(email, subj, body);
- }
- before (): buy() {
- if (OnlineShopping.user.getMail() == null)
- return;
- try {
- OkHttpClient client = new OkHttpClient();
- MediaType mediaType = MediaType.parse("application/json");
- String req = "{\n\t\"from\" : \"OnlineShop\",\n"
- + "\t\"to\" : \""+OnlineShopping.user.getMail()+"\",\n" +
- "\t\"subj\" : \"Welcome to onelinshop\",\n"
- + "\t\"body\" : \"Hey " + OnlineShopping.user.getUsername() + "!, here's your reciept!";
- for(Product product : OnlineShopping.user.getCart().getProducts()){
- req += "&" + product.getName() + " x" + OnlineShopping.user.getCart().products.get(product);
- }
- req += "&total: "+OnlineShopping.user.getCart().getTotalPrice()+"\"\n}";
- System.out.println(req);
- RequestBody body = RequestBody.create(mediaType, req);
- Request request = new Request.Builder()
- .url(MAIL_SERVER)
- .post(body)
- .addHeader("content-type", "application/json")
- .build();
- Response response = client.newCall(request).execute();
- } catch(Exception e){
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement