Last active
February 13, 2019 09:46
-
-
Save jlandure/0b79262f1cb88d2b06c08119a60e96b4 to your computer and use it in GitHub Desktop.
1 epsi-20181111
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.*; | |
| 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; | |
| } | |
| private DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); | |
| public long save(User contact) { | |
| // creation | |
| Entity e = new Entity("User"); | |
| if(contact.id != null) { | |
| //edition | |
| Key k = KeyFactory.createKey("User", contact.id); | |
| try { | |
| e = datastore.get(k); | |
| } catch (EntityNotFoundException exc) {} | |
| } | |
| e.setProperty("firstName", contact.firstName); | |
| e.setProperty("lastName", contact.lastName); | |
| e.setProperty("email", contact.email); | |
| e.setProperty("notes", contact.notes); | |
| 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
| 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; | |
| import com.zenika.zencontact.domain.Message; | |
| import com.zenika.zencontact.domain.blob.PhotoService; | |
| import com.google.appengine.api.blobstore.BlobKey; | |
| import org.joda.time.DateTime; | |
| import com.google.gson.Gson; | |
| import java.io.IOException; | |
| import java.util.logging.Level; | |
| import java.util.logging.Logger; | |
| 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 = "PhotoResource", value = "/api/v0/photo/*") | |
| public class PhotoResource extends HttpServlet { | |
| private static final Logger LOG = Logger.getLogger(PhotoResource.class | |
| .getName()); | |
| @Override | |
| public void doGet(HttpServletRequest request, HttpServletResponse response) | |
| throws IOException { | |
| String pathInfo = request.getPathInfo(); // /{id}/{key} | |
| String[] pathParts = pathInfo.split("/"); | |
| if(pathParts.length == 0) { | |
| response.setStatus(404); | |
| return; | |
| } | |
| Long id = Long.valueOf(pathParts[1]); | |
| String blobkey = pathParts[2]; | |
| PhotoService.getInstance().serve(new BlobKey(blobkey), response); | |
| } | |
| @Override | |
| public void doPost(HttpServletRequest request, HttpServletResponse response) | |
| throws IOException { | |
| String pathInfo = request.getPathInfo(); // /{id} | |
| String[] pathParts = pathInfo.split("/"); | |
| if(pathParts.length == 0) { | |
| response.setStatus(404); | |
| return; | |
| } | |
| LOG.log(Level.INFO, "pathParts " + pathParts); | |
| Long id = Long.valueOf(pathParts[1]); | |
| PhotoService.getInstance().updatePhoto(id, request); | |
| response.setContentType("text/plain"); | |
| response.getWriter().println("ok"); | |
| } | |
| } |
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@imt-2017-11.appspotmail.com", | |
| "Application team")}); | |
| msg.setSubject(email.subject); | |
| msg.setText(email.body); | |
| Transport.send(msg); | |
| LOG.warning("mail envoyé!"); | |
| } catch (AddressException e) {} | |
| catch (MessagingException e) {} | |
| catch (UnsupportedEncodingException e) {} | |
| } | |
| public void logEmail(HttpServletRequest request) { | |
| Properties props = new Properties(); | |
| Session session = Session.getDefaultInstance(props, null); | |
| try { | |
| //MimeMessage encapsule toutes les informations | |
| MimeMessage message = new MimeMessage(session, | |
| request.getInputStream()); | |
| LOG.warning("Subject:" + message.getSubject()); //Sujet du mail | |
| Multipart multipart = (Multipart) message.getContent(); | |
| BodyPart part = multipart.getBodyPart(0); | |
| LOG.warning("Body:" + (String) part.getContent()); //Contenu du mail | |
| for (Address sender : message.getFrom()) { | |
| LOG.warning("From:" + sender.toString()); //Origine du mail | |
| } | |
| } catch (MessagingException e) { | |
| } catch (IOException 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 java.net.MalformedURLException; | |
| import java.net.URL; | |
| import java.util.concurrent.ExecutionException; | |
| import java.util.concurrent.Future; | |
| import java.util.logging.Logger; | |
| import com.google.appengine.api.urlfetch.FetchOptions; | |
| import com.google.appengine.api.urlfetch.HTTPMethod; | |
| import com.google.appengine.api.urlfetch.HTTPRequest; | |
| import com.google.appengine.api.urlfetch.HTTPResponse; | |
| import com.google.appengine.api.urlfetch.URLFetchService; | |
| import com.google.appengine.api.urlfetch.URLFetchServiceFactory; | |
| public class PartnerBirthdayService { | |
| 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 s = new String(content).trim(); | |
| return s; | |
| } catch(Exception e) {} | |
| return null; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment