Advertisement
Guest User

Untitled

a guest
May 27th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1.  
  2. import okhttp3.*;
  3.  
  4. public privileged aspect MailAspect {
  5.  
  6. private static final String MAIL_SERVER = "http://da-sentence.cloudapp.net/aspects/sendmail";
  7.  
  8. pointcut newClient(String uname, String pass, String email): initialization(Customer.new(String, String, String)) && args(uname, pass, email);
  9. pointcut buy(): call(void ShoppingCart.pay());
  10. pointcut passRecovery(): call(void OnlineShopping.passRecovery());
  11.  
  12. private static void sendMail(String mail, String subj, String body){
  13. try {
  14. OkHttpClient client = new OkHttpClient();
  15.  
  16. MediaType mediaType = MediaType.parse("application/json");
  17. String req = "{\n\t\"from\" : \"OnlineShop\",\n"
  18. + "\t\"to\" : \"" + mail + "\",\n" +
  19. "\t\"subj\" : \"" + subj + "\",\n"
  20. + "\t\"body\" : \"" + body + "\"\n}";
  21.  
  22. System.out.println(req);
  23. RequestBody reqBody = RequestBody.create(mediaType, req);
  24. Request request = new Request.Builder()
  25. .url(MAIL_SERVER)
  26. .post(reqBody)
  27. .addHeader("content-type", "application/json")
  28. .build();
  29.  
  30. Response response = client.newCall(request).execute();
  31. } catch(Exception e){
  32.  
  33. }
  34. }
  35.  
  36. after (): passRecovery(){
  37. if(OnlineShopping.user == null){
  38. System.out.println("you are not logged in");
  39. return;
  40. }
  41. if(OnlineShopping.user.email == null){
  42. System.out.println("no registered mail");
  43. return;
  44. }
  45. String subj = "Your password";
  46. String body = "Hey " + OnlineShopping.user.getUsername() +
  47. "&your password is" + OnlineShopping.user.password;
  48. sendMail(OnlineShopping.user.getMail(), subj, body);
  49. }
  50.  
  51. after (String uname, String pass, String email): newClient(uname, pass, email) {
  52. if (email == null)
  53. return;
  54.  
  55. String subj = "Welcome to onelinshop";
  56. String body = "Hey " + uname + "&Welcome to our shop!";
  57. sendMail(email, subj, body);
  58. }
  59.  
  60. before (): buy() {
  61. if (OnlineShopping.user.getMail() == null)
  62. return;
  63. try {
  64.  
  65. OkHttpClient client = new OkHttpClient();
  66.  
  67. MediaType mediaType = MediaType.parse("application/json");
  68. String req = "{\n\t\"from\" : \"OnlineShop\",\n"
  69. + "\t\"to\" : \""+OnlineShopping.user.getMail()+"\",\n" +
  70. "\t\"subj\" : \"Welcome to onelinshop\",\n"
  71. + "\t\"body\" : \"Hey " + OnlineShopping.user.getUsername() + "!, here's your reciept!";
  72.  
  73. for(Product product : OnlineShopping.user.getCart().getProducts()){
  74. req += "&" + product.getName() + " x" + OnlineShopping.user.getCart().products.get(product);
  75. }
  76.  
  77. req += "&total: "+OnlineShopping.user.getCart().getTotalPrice()+"\"\n}";
  78.  
  79. System.out.println(req);
  80. RequestBody body = RequestBody.create(mediaType, req);
  81. Request request = new Request.Builder()
  82. .url(MAIL_SERVER)
  83. .post(body)
  84. .addHeader("content-type", "application/json")
  85. .build();
  86.  
  87. Response response = client.newCall(request).execute();
  88. } catch(Exception e){
  89.  
  90. }
  91.  
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement