Last active
February 10, 2021 09:13
-
-
Save jlandure/00abbd3151aaf301de4482003d81eefb to your computer and use it in GitHub Desktop.
Cours epsi-20181212
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| https://backoffice-dot-epsi-20181212-ju.appspot.com/tp1.zip |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.zenika.zencontact.persistence.datastore; | |
| import java.util.ArrayList; | |
| import java.util.Date; | |
| import java.util.List; | |
| import com.google.appengine.api.blobstore.BlobKey; | |
| import com.google.appengine.api.datastore.*; | |
| import com.zenika.zencontact.domain.User; | |
| import com.zenika.zencontact.persistence.UserDao; | |
| public class UserDaoDatastore implements UserDao { | |
| private static UserDaoDatastore INSTANCE = | |
| new UserDaoDatastore(); | |
| public static UserDaoDatastore getInstance() { | |
| return INSTANCE; | |
| } | |
| public DatastoreService datastore = | |
| DatastoreServiceFactory.getDatastoreService(); | |
| public long save(User contact) { | |
| // je créé l'entité | |
| Entity e = new Entity("User"); | |
| if (contact.id != null) { | |
| // en cas d'édition | |
| Key k = KeyFactory.createKey("User", contact.id); | |
| try { | |
| e = datastore.get(k); | |
| } catch(EntityNotFoundException e1) {} | |
| } | |
| // on met les valeurs | |
| e.setProperty("firstName", contact.firstName); | |
| e.setProperty("lastName", contact.lastName); | |
| e.setProperty("email", contact.email); | |
| e.setProperty("notes", contact.notes); | |
| //sauvegarder | |
| Key key = datastore.put(e); | |
| return key.getId(); | |
| } | |
| public void delete(Long id) { | |
| Key k = KeyFactory.createKey("User", id); | |
| datastore.delete(k); | |
| } | |
| public User get(Long id) { | |
| Entity e = null; | |
| try { | |
| e = datastore.get(KeyFactory.createKey("User", id)); | |
| } | |
| catch(EntityNotFoundException exc) {} | |
| return User.create() | |
| .id(e.getKey().getId()) | |
| .firstName((String) e.getProperty("firstName")) | |
| .lastName((String) e.getProperty("lastName")) | |
| .email((String) e.getProperty("email")) | |
| .notes((String) e.getProperty("notes")); | |
| } | |
| public List<User> getAll() { | |
| List<User> contacts = new ArrayList<>(); | |
| Query q = new Query("User") | |
| .addProjection(new PropertyProjection("firstName", String.class)) | |
| .addProjection(new PropertyProjection("lastName", String.class)) | |
| .addProjection(new PropertyProjection("email", String.class)) | |
| .addProjection(new PropertyProjection("notes", String.class)); | |
| PreparedQuery pq = datastore.prepare(q); | |
| for (Entity e : pq.asIterable()) { | |
| contacts.add(User.create() | |
| .id(e.getKey().getId()) | |
| .firstName((String) e.getProperty("firstName")) | |
| .lastName((String) e.getProperty("lastName")) | |
| .email((String) e.getProperty("email")) | |
| .notes((String) e.getProperty("notes"))); | |
| } | |
| return contacts; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.zenika.zencontact.resource; | |
| import com.zenika.zencontact.domain.User; | |
| import com.zenika.zencontact.persistence.UserRepository; | |
| import com.zenika.zencontact.persistence.datastore.UserDaoDatastore; | |
| import com.google.gson.Gson; | |
| import java.io.IOException; | |
| import javax.servlet.annotation.WebServlet; | |
| import javax.servlet.http.HttpServlet; | |
| import javax.servlet.http.HttpServletRequest; | |
| import javax.servlet.http.HttpServletResponse; | |
| // With @WebServlet annotation the webapp/WEB-INF/web.xml is no longer required. | |
| @WebServlet(name = "UserResource", value = "/api/v0/users") | |
| public class UserResource extends HttpServlet { | |
| @Override | |
| public void doGet(HttpServletRequest request, HttpServletResponse response) | |
| throws IOException { | |
| response.setContentType("application/json; charset=utf-8"); | |
| response.getWriter().println(new Gson() | |
| .toJsonTree(UserDaoDatastore.getInstance().getAll()).getAsJsonArray()); | |
| } | |
| @Override | |
| public void doPost(HttpServletRequest request, HttpServletResponse response) | |
| throws IOException { | |
| User user = new Gson().fromJson(request.getReader(), User.class); | |
| user.id(UserDaoDatastore.getInstance().save(user)); | |
| response.setContentType("application/json; charset=utf-8"); | |
| response.setStatus(201); | |
| response.getWriter().println(new Gson().toJson(user)); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.zenika.zencontact.resource; | |
| import com.zenika.zencontact.domain.User; | |
| import com.zenika.zencontact.persistence.UserRepository; | |
| import com.zenika.zencontact.persistence.datastore.UserDaoDatastore; | |
| import com.google.gson.Gson; | |
| import com.google.common.base.Predicate; | |
| import com.google.common.collect.Iterables; | |
| import java.io.IOException; | |
| import javax.servlet.annotation.WebServlet; | |
| import javax.servlet.http.HttpServlet; | |
| import javax.servlet.http.HttpServletRequest; | |
| import javax.servlet.http.HttpServletResponse; | |
| // With @WebServlet annotation the webapp/WEB-INF/web.xml is no longer required. | |
| @WebServlet(name = "UserResourceWithId", value = "/api/v0/users/*") | |
| public class UserResourceWithId extends HttpServlet { | |
| private Long getId(HttpServletRequest request) { | |
| String pathInfo = request.getPathInfo(); // /{id} | |
| String[] pathParts = pathInfo.split("/"); | |
| if(pathParts.length == 0) { | |
| return null; | |
| } | |
| return Long.valueOf(pathParts[1]); // {id} | |
| } | |
| @Override | |
| public void doGet(HttpServletRequest request, HttpServletResponse response) | |
| throws IOException { | |
| Long id = getId(request); | |
| if(id == null) { | |
| response.setStatus(404); | |
| return; | |
| } | |
| User user = UserDaoDatastore.getInstance().get(id); | |
| response.setContentType("application/json; charset=utf-8"); | |
| response.getWriter().println(new Gson().toJson(user)); | |
| } | |
| @Override | |
| public void doPut(HttpServletRequest request, HttpServletResponse response) | |
| throws IOException { | |
| Long id = getId(request); | |
| if(id == null) { | |
| response.setStatus(404); | |
| return; | |
| } | |
| User user = new Gson().fromJson(request.getReader(), User.class); | |
| UserDaoDatastore.getInstance().save(user); | |
| response.setContentType("application/json; charset=utf-8"); | |
| response.getWriter().println(new Gson().toJson(user)); | |
| } | |
| @Override | |
| public void doDelete(HttpServletRequest request, HttpServletResponse response) | |
| throws IOException { | |
| Long id = getId(request); | |
| if(id == null) { | |
| response.setStatus(404); | |
| return; | |
| } | |
| UserDaoDatastore.getInstance().delete(id); | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.zenika.zencontact.resource; | |
| import javax.servlet.annotation.WebFilter; | |
| import com.googlecode.objectify.ObjectifyFilter; | |
| @WebFilter(urlPatterns = {"/*"}) | |
| public class ObjectifyWebFilter extends ObjectifyFilter {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <dependency> | |
| <groupId>com.googlecode.objectify</groupId> | |
| <artifactId>objectify</artifactId> | |
| <version>5.1.21</version> | |
| </dependency> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.zenika.zencontact.persistence.objectify; | |
| import com.google.appengine.api.blobstore.BlobKey; | |
| import com.googlecode.objectify.*; | |
| import com.zenika.zencontact.domain.User; | |
| import com.zenika.zencontact.persistence.UserDao; | |
| import java.util.List; | |
| public class UserDaoObjectify implements UserDao { | |
| private static UserDaoObjectify INSTANCE = new UserDaoObjectify(); | |
| public static UserDaoObjectify getInstance() { | |
| ObjectifyService.factory().register(User.class); | |
| return INSTANCE; | |
| } | |
| public long save(User contact) { | |
| return ObjectifyService.ofy().save() | |
| .entity(contact).now().getId(); | |
| } | |
| public void delete(Long id) { | |
| ObjectifyService.ofy().delete() | |
| .key(Key.create(User.class, id)).now(); | |
| } | |
| public User get(Long id) { | |
| return ObjectifyService.ofy().load() | |
| .key(Key.create(User.class, id)).now(); | |
| } | |
| public List<User> getAll() { | |
| return ObjectifyService.ofy().load() | |
| .type(User.class).list(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.zenika.zencontact.resource.auth; | |
| import java.io.IOException; | |
| import javax.servlet.Filter; | |
| import javax.servlet.FilterChain; | |
| import javax.servlet.FilterConfig; | |
| import javax.servlet.ServletException; | |
| import javax.servlet.ServletRequest; | |
| import javax.servlet.ServletResponse; | |
| import javax.servlet.annotation.WebFilter; | |
| import javax.servlet.http.HttpServletRequest; | |
| import javax.servlet.http.HttpServletResponse; | |
| import java.util.logging.Level; | |
| import java.util.logging.Logger; | |
| @WebFilter(urlPatterns = {"api/v0/users/*"}) | |
| public class AuthFilter implements Filter { | |
| private static final Logger LOG = Logger.getLogger(AuthFilter.class | |
| .getName()); | |
| @Override | |
| public void init(FilterConfig filterConfig) throws ServletException { | |
| } | |
| @Override | |
| public void doFilter(ServletRequest request, ServletResponse response, | |
| FilterChain chain) throws IOException, ServletException { | |
| HttpServletRequest req = ((HttpServletRequest)request); | |
| HttpServletResponse res = ((HttpServletResponse)response); | |
| LOG.log(Level.INFO, "Filtre d'authentification"); | |
| String pathInfo = req.getPathInfo(); // /{id} | |
| if(pathInfo != null) { //{id} | |
| String[] pathParts = pathInfo.split("/"); | |
| //only admin can delete | |
| if (req.getMethod() == "DELETE" | |
| && !AuthenticationService.getInstance().isAdmin()) { | |
| res.setStatus(403); | |
| return; | |
| } | |
| if(AuthenticationService.getInstance().getUser() != null) { | |
| res.setHeader("Username", AuthenticationService.getInstance().getUsername()); | |
| res.setHeader("Logout", AuthenticationService.getInstance().getLogoutURL("/#/clear")); | |
| } else { | |
| //only authent users can edit | |
| res.setHeader("Location", AuthenticationService.getInstance().getLoginURL("/#/edit/" + pathParts[1])); | |
| res.setHeader("Logout", AuthenticationService.getInstance().getLogoutURL("/#/clear")); | |
| res.setStatus(401); | |
| return; | |
| } | |
| } | |
| chain.doFilter(request, response); | |
| } | |
| @Override | |
| public void destroy() { | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.zenika.zencontact.email; | |
| import java.io.IOException; | |
| import java.io.UnsupportedEncodingException; | |
| import java.util.Properties; | |
| import java.util.logging.Logger; | |
| import javax.mail.Address; | |
| import javax.mail.BodyPart; | |
| import javax.mail.Message; | |
| import javax.mail.MessagingException; | |
| import javax.mail.Multipart; | |
| import javax.mail.Session; | |
| import javax.mail.Transport; | |
| import javax.mail.internet.AddressException; | |
| import javax.mail.internet.InternetAddress; | |
| import javax.mail.internet.MimeMessage; | |
| import javax.servlet.http.HttpServletRequest; | |
| import com.zenika.zencontact.domain.Email; | |
| import com.zenika.zencontact.resource.auth.AuthenticationService; | |
| public class EmailService { | |
| private static final Logger LOG = Logger.getLogger(EmailService.class.getName()); | |
| private static EmailService INSTANCE = new EmailService(); | |
| public static EmailService getInstance() { | |
| return INSTANCE; | |
| } | |
| public void sendEmail(Email email) { | |
| Properties props = new Properties(); | |
| Session session = Session.getDefaultInstance(props, null); | |
| try { | |
| Message msg = new MimeMessage(session); | |
| msg.setFrom(new InternetAddress( | |
| AuthenticationService.getInstance().getUser().getEmail(), | |
| AuthenticationService.getInstance().getUsername() | |
| )); | |
| msg.addRecipient( | |
| Message.RecipientType.TO, | |
| new InternetAddress(email.to, email.toName) | |
| ); | |
| msg.setReplyTo(new Address[] { | |
| new InternetAddress("team@epsi-20181212-ju.appspotmail.com", | |
| "Application Team") | |
| }); | |
| msg.setSubject(email.subject); | |
| msg.setText(email.body); | |
| Transport.send(msg); | |
| } catch(Exception ex) {} | |
| } | |
| public void logEmail(HttpServletRequest request) { | |
| Properties props = new Properties(); | |
| Session session = Session.getDefaultInstance(props, null); | |
| try { | |
| MimeMessage message = new MimeMessage(session, | |
| request.getInputStream()); | |
| LOG.warning("Subject:" + message.getSubject()); | |
| Multipart multipart = (Multipart) message.getContent(); | |
| BodyPart part = multipart.getBodyPart(0); | |
| LOG.warning("Body: " + (String) part.getContent()); | |
| for(Address sender : message.getFrom()) { | |
| LOG.warning("From: " + sender.toString()); | |
| } | |
| } catch (Exception e) {} | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.zenika.zencontact.fetch; | |
| import com.google.appengine.api.urlfetch.*; | |
| import java.net.*; | |
| public class PartnerBirthdateService { | |
| private static PartnerBirthdateService INSTANCE = new PartnerBirthdateService(); | |
| public static PartnerBirthdateService getInstance() { | |
| return INSTANCE; | |
| } | |
| private static final String SERVICE_URL = | |
| "http://zenpartenaire.appspot.com/zenpartenaire"; | |
| public String findBirthdate(String firstname, String lastname) { | |
| try { | |
| URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService(); | |
| URL url = new URL(SERVICE_URL); | |
| HTTPRequest postRequest = new HTTPRequest( | |
| url, | |
| HTTPMethod.POST, | |
| FetchOptions.Builder.withDeadline(30)); | |
| String payload = firstname + " " + lastname; | |
| postRequest.setPayload(payload.getBytes()); | |
| HTTPResponse response = fetcher.fetch(postRequest); | |
| if(response.getResponseCode() != 200) { | |
| return null; | |
| } | |
| byte[] content = response.getContent(); | |
| String birthdate = new String(content).trim(); | |
| return birthdate; | |
| } catch(Exception e) {} | |
| return null; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| https://docs.google.com/forms/d/e/1FAIpQLSedsw5RUfdU4ci2AmyEUv0iCPlqVRkuS2iD20zqS8Bg5ENHVw/viewform |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment