diff --git a/src/main/java/services/SongsterImpl.java b/src/main/java/services/SongsterImpl.java index a081f804c4d222fd6685a889c70dbb4f7cfd8562..ef61800f5829ffce6f17f443b2883fd60985cc29 100644 --- a/src/main/java/services/SongsterImpl.java +++ b/src/main/java/services/SongsterImpl.java @@ -29,184 +29,206 @@ import javax.mail.internet.MimeMessage; import javax.mail.Authenticator; @WebService -public class SongsterImpl{ +public class SongsterImpl { @Resource WebServiceContext context; @WebMethod public String addSub( - @WebParam(name = "uid") Integer uid - ){ + @WebParam(name = "uid") Integer uid) { // String retval = ""; - if(Auth.IsAccepted(context, "REST")){ - try{ - // autentikasi berhasil - String endpoint = "addSub"; - String desc= "Request to add UID as a premium member"; - Logger.log(context, desc, endpoint); - - Subscription s = new Subscription(); - s.init(); + if (Auth.IsAccepted(context, "PHP")) { + try { + // autentikasi berhasil + String endpoint = "addSub"; + String desc = "Request to add UID as a premium member"; + Logger.log(context, desc, endpoint); + System.out.println("logged php yayayaa"); + + Subscription s = new Subscription(); + s.init(); s.addSubscription(uid); - } catch (Exception e){ + } catch (Exception e) { e.printStackTrace(); return "add failed"; } - return retval + uid + " is now added"; + return uid + " is now added"; - } - else{ + } else { return "unauthorized"; } - - } @WebMethod public int checkSub( - @WebParam(name = "uid") Integer uid - ){ + @WebParam(name = "uid") Integer uid) { // log to db - String endpoint = "checkSub"; - String desc= "Request to check premium status of UID"; - Logger.log(context, desc, endpoint); - - Subscription s = new Subscription(); - s.init(); - int retval = 0; - try{ - retval = s.checkExist(uid); - } catch (Exception e){ - e.printStackTrace(); - } + if (Auth.IsAccepted(context, "REST")) { + String endpoint = "checkSub"; + String desc = "Request to check premium status of UID"; + Logger.log(context, desc, endpoint); + + Subscription s = new Subscription(); + s.init(); + int retval = 0; + try { + retval = s.checkExist(uid); + } catch (Exception e) { + e.printStackTrace(); + } - return retval; + return retval; + } else { + return -1; + } } @WebMethod public String delSub( - @WebParam(name = "uid") Integer uid - ){ + @WebParam(name = "uid") Integer uid) { // log to db - String endpoint = "delSub"; - String desc= "Request to delete UID from Premium"; - Logger.log(context, desc, endpoint); - - Subscription s = new Subscription(); - s.init(); - try{ - s.delSubscription(uid); - } catch (Exception e){ - e.printStackTrace(); - } - return uid + " has been deleted"; + if (Auth.IsAccepted(context, "REST")) { + String endpoint = "delSub"; + String desc = "Request to delete UID from Premium"; + Logger.log(context, desc, endpoint); + + Subscription s = new Subscription(); + s.init(); + try { + s.delSubscription(uid); + } catch (Exception e) { + e.printStackTrace(); + } + + return uid + " has been deleted"; + } else { + return "unauthorized"; + } } @WebMethod public String editEmail( @WebParam(name = "uid") Integer uid, - @WebParam(name = "email") String email - ){ - // log to db - String endpoint = "editEmail"; - String desc= "Request to edit UID's email"; - Logger.log(context, desc, endpoint); - - try{ - Mailing m = new Mailing(); - m.init(); - m.editEmail(uid,email); - return "email has been added"; - } catch (Exception e){ - e.printStackTrace(); - return "failed"; + @WebParam(name = "email") String email) { + + if (Auth.IsAccepted(context, "REST")) { + // log to db + String endpoint = "editEmail"; + String desc = "Request to edit UID's email"; + Logger.log(context, desc, endpoint); + + try { + Mailing m = new Mailing(); + m.init(); + m.editEmail(uid, email); + return "email has been added"; + } catch (Exception e) { + e.printStackTrace(); + return "failed"; + } + + } else { + return "unauthorized"; } } @WebMethod public String sendEmail( @WebParam(name = "uid") Integer uid, - @WebParam(name = "msg") String msg - ){ + @WebParam(name = "msg") String msg) { // log to db - try{ - String endpoint = "sendEmail"; - String desc= "Request send an email"; - Logger.log(context, desc, endpoint); - - Mailer mailer = new Mailer(); - - Mailing m = new Mailing(); - m.init(); - String targetemail = m.getEmail(uid); - if(targetemail==null || targetemail=="") return "no email found"; - mailer.mail("songster", targetemail, msg); - - return "email has been sent"; - } catch (Exception e){ - e.printStackTrace(); - return "failed"; + if (Auth.IsAccepted(context, "REST")) { + try { + String endpoint = "sendEmail"; + String desc = "Request send an email"; + Logger.log(context, desc, endpoint); + + Mailer mailer = new Mailer(); + + Mailing m = new Mailing(); + m.init(); + String targetemail = m.getEmail(uid); + if (targetemail == null || targetemail == "") + return "no email found"; + mailer.mail("songster", targetemail, msg); + + return "email has been sent"; + } catch (Exception e) { + e.printStackTrace(); + return "failed"; + } + } else { + return "unauthorized"; } } @WebMethod public String getEmail( - @WebParam(name = "uid") Integer uid - ){ + @WebParam(name = "uid") Integer uid) { // log to db - try{ - String endpoint = "getEmail"; - String desc= "Request email of UID"; - Logger.log(context, desc, endpoint); + if (Auth.IsAccepted(context, "REST")) { + + try { + String endpoint = "getEmail"; + String desc = "Request email of UID"; + Logger.log(context, desc, endpoint); - Mailing m = new Mailing(); - m.init(); - String targetemail = m.getEmail(uid); - if(targetemail==null || targetemail=="") return "none"; + Mailing m = new Mailing(); + m.init(); + String targetemail = m.getEmail(uid); + if (targetemail == null || targetemail == "") + return "none"; - return targetemail; - } catch (Exception e){ - e.printStackTrace(); - return "failed"; + return targetemail; + } catch (Exception e) { + e.printStackTrace(); + return "failed"; + } + } else { + return "unauthorized"; } } @WebMethod public String test( - @WebParam(name = "name") String name - ) { - String to = "fariski.mail@gmail.com"; - String from = "songstermailer@gmail.com"; - String host = "smtp-relay.brevo.com"; - - Properties properties = System.getProperties(); - properties.put("mail.smtp.host", host); - properties.put("mail.smtp.port", "587"); - //properties.put("mail.smtp.ssl.enable", "true"); - properties.put("mail.smtp.auth", "true"); - - //for mailing purposes - Session session = Session.getInstance(properties, new Authenticator(){ - protected PasswordAuthentication getPasswordAuthentication() { - return new PasswordAuthentication("songstermailer@gmail.com", "bvWC3HhJ87DKcOg5"); + @WebParam(name = "name") String name) { + if (Auth.IsAccepted(context, "REST")) { + + String to = "fariski.mail@gmail.com"; + String from = "songstermailer@gmail.com"; + String host = "smtp-relay.brevo.com"; + + Properties properties = System.getProperties(); + properties.put("mail.smtp.host", host); + properties.put("mail.smtp.port", "587"); + // properties.put("mail.smtp.ssl.enable", "true"); + properties.put("mail.smtp.auth", "true"); + + // for mailing purposes + Session session = Session.getInstance(properties, new Authenticator() { + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication("songstermailer@gmail.com", "bvWC3HhJ87DKcOg5"); + } + }); + + try { + MimeMessage message = new MimeMessage(session); + message.setFrom(new InternetAddress(from)); + message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); + message.setSubject("New Deals"); + message.setText("In Songster, we have new deals coming through!"); + + Transport.send(message); + } catch (MessagingException mex) { + mex.printStackTrace(); } - }); - - try { - MimeMessage message = new MimeMessage(session); - message.setFrom(new InternetAddress(from)); - message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); - message.setSubject("New Deals"); - message.setText("In Songster, we have new deals coming through!"); - - Transport.send(message); - } catch (MessagingException mex) { - mex.printStackTrace(); - } - return name + "received cah"; + return name + "received cah"; + } else { + return "unauthorized"; + } } } diff --git a/src/main/java/utils/Auth.java b/src/main/java/utils/Auth.java index e598f7226f3df8f378ff74a49242011cd946b3e6..deb45d647927c752f03f7b1ae1f6eb24cf37f4ea 100644 --- a/src/main/java/utils/Auth.java +++ b/src/main/java/utils/Auth.java @@ -12,6 +12,7 @@ public class Auth { Map<String, List<String>> headers = (Map<String, List<String>>) context.getMessageContext().get(MessageContext.HTTP_REQUEST_HEADERS); List<String> auth = headers.get("authorization"); + if(auth==null) return false; if(auth.size()==0) return false;