diff --git a/.classpath b/.classpath index b35e07b..8549f85 100644 --- a/.classpath +++ b/.classpath @@ -23,5 +23,10 @@ + + + + + diff --git a/.settings/org.eclipse.wst.common.component b/.settings/org.eclipse.wst.common.component index ca78711..78afb01 100644 --- a/.settings/org.eclipse.wst.common.component +++ b/.settings/org.eclipse.wst.common.component @@ -1,9 +1,9 @@ - - + + diff --git a/pom.xml b/pom.xml index 8f2eb91..f8197c1 100644 --- a/pom.xml +++ b/pom.xml @@ -51,6 +51,12 @@ spring-context-support ${spring.version} + + + org.springframework.mobile + spring-mobile-device + 1.1.1.RELEASE + @@ -148,6 +154,31 @@ commons-io 1.3.2 + + org.springframework.boot + spring-boot-starter-batch + 1.0.0.RELEASE + + + org.springframework.boot + spring-boot-starter-batch + 1.0.0.RELEASE + + + org.springframework.boot + spring-boot-starter-batch + 1.0.0.RELEASE + + + com.googlecode.json-simple + json-simple + 1.1 + + + org.springframework.boot + spring-boot-starter-remote-shell + 1.0.0.RELEASE + diff --git a/src/main/java/ai/server/controller/AndroidAuthenticationProvider.java b/src/main/java/ai/server/controller/AndroidAuthenticationProvider.java new file mode 100644 index 0000000..d2ca248 --- /dev/null +++ b/src/main/java/ai/server/controller/AndroidAuthenticationProvider.java @@ -0,0 +1,61 @@ +package ai.server.controller; + +import hibernate.Utente; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.authentication.AuthenticationProvider; +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.AuthenticationException; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.core.userdetails.User; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.stereotype.Component; + +import dati.Dati; + +@Component +public class AndroidAuthenticationProvider implements AuthenticationProvider { + + @Autowired + private Dati dati; + + public void setDati(Dati dati){ + this.dati = dati; + } + + @Override + public Authentication authenticate(Authentication authentication) throws AuthenticationException { + String username = authentication.getName(); + String password = authentication.getCredentials().toString(); + + System.out.println(username); + System.out.println(password); + + Utente utente = dati.getUtenti().get(username); + if(utente != null) { + if(!utente.getConfermato() || !utente.getPassword().equals(password)) + utente = null; + } else { + System.out.println("User " + username + " not found"); + return null; + } + + final List grantedAuths = new ArrayList<>(); + grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER")); + final UserDetails principal = new User(username, password, grantedAuths); + final Authentication auth = new UsernamePasswordAuthenticationToken(principal, password, grantedAuths); + return auth; + + } + + @Override + public boolean supports(Class authentication) { + return authentication.equals(UsernamePasswordAuthenticationToken.class); + } + +} diff --git a/src/main/java/ai/server/controller/InserzioneController.java b/src/main/java/ai/server/controller/InserzioneController.java index e8f48af..a8d6098 100644 --- a/src/main/java/ai/server/controller/InserzioneController.java +++ b/src/main/java/ai/server/controller/InserzioneController.java @@ -13,7 +13,10 @@ import java.net.URL; import java.security.Principal; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Calendar; +import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; @@ -23,12 +26,15 @@ import javax.imageio.ImageIO; import javax.servlet.ServletContext; +import javax.servlet.ServletRequest; import javax.servlet.http.HttpServletRequest; import org.apache.commons.io.FileUtils; import org.codehaus.jackson.node.ArrayNode; import org.codehaus.jackson.node.JsonNodeFactory; import org.codehaus.jackson.node.ObjectNode; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.validation.BindingResult; @@ -165,7 +171,7 @@ public ModelAndView showForm(Map model){ } @RequestMapping(value="/inserzione",method= RequestMethod.POST) - public ModelAndView processInserzione(InserzioneForm inserzioneForm,BindingResult result,Principal principal){ + public ModelAndView processInserzione(InserzioneForm inserzioneForm, BindingResult result,Principal principal){ boolean inserimentoSupermercato=false; boolean inserimentoInserzione=false; boolean inserimentoProdotto=false; @@ -327,5 +333,91 @@ public static float distFrom(float lat1, float lng1, float lat2, float lng2) { return new Float(dist).floatValue(); } + + @RequestMapping(value="/android/inserzione/getCategorie", method = RequestMethod.GET) + @ResponseBody + public JSONArray getCategorie() { + System.out.println("Called: /android/inserzione/getCategorie"); + JSONArray response = new JSONArray(); + + ArrayList categorieList = new ArrayList(); + for(Map.Entry cat : dati.getCategorie().entrySet()){ + response.add(cat.getValue().getNome()); + } + return response; + } + + @RequestMapping(value="/android/inserzione/checkbarcode/{barcode}", method= RequestMethod.GET) + @ResponseBody + public JSONArray checkbarcode(@PathVariable Long barcode) { + System.out.println("Called: /android/inserzione/checkbarcode " + barcode); + JSONArray response = new JSONArray(); + JSONObject jsonObj = new JSONObject(); + + if(dati.getProdotti().containsKey(barcode)) { + jsonObj.put("descrizione", dati.getProdotti().get(barcode).getDescrizione()); + jsonObj.put("trovato", true); + } + else + jsonObj.put("trovato", false); + + response.add(jsonObj); + System.out.println("TROVATO:" + jsonObj.get("trovato")); + return response; + } + + @RequestMapping(value="/android/inserzione/getSottoCategorie/{categoria}", method = RequestMethod.GET) + @ResponseBody + public JSONArray getSottoCategorieAndroid(@PathVariable String categoria) { + System.out.println("Called: /android/inserzione/getSottoCategorie " + categoria); + JSONArray response = new JSONArray(); + for(Map.Entry c : dati.getCategorie().entrySet()) + if(c.getValue().getNome().equals(categoria)) + for(Sottocategoria s : (Set) c.getValue().getSottocategorias()) + response.add(s.getNome()); + return response; + } + + @RequestMapping(value="/android/inserzione/getSupermercati") + @ResponseBody + public JSONArray getSupermercatiAndroid(float lat, float lng){ + System.out.println("Called: /android/inserzione/getSupermercati " + lat + " - " + lng); + JSONArray response = new JSONArray(); + List jsonObjList = new ArrayList(); + float massimaDistanza = 50000; // distanza = 3 km! + for(Map.Entry s : dati.getSupermercati().entrySet()) { + float distanza = distFromAndroid(lat, lng, s.getValue().getLatitudine().floatValue(), s.getValue().getLongitudine().floatValue()); + if( distanza <= massimaDistanza) { + JSONObject jsonObj = new JSONObject(); + jsonObj.put("nome", s.getValue().getNome()); + jsonObj.put("distanza", distanza); + jsonObjList.add(jsonObj); + } + } + + Collections.sort(jsonObjList, new Comparator(){ + @Override + public int compare(JSONObject arg0, JSONObject arg1) { + return (int) (((float) arg0.get("distanza")) - ((float) arg1.get("distanza"))); + }}); + + response.addAll(jsonObjList); + return response; + } + + public static float distFromAndroid(float lat1, float lng1, float lat2, float lng2) { + double earthRadius = 3958.75; + double dLat = Math.toRadians(lat2-lat1); + double dLng = Math.toRadians(lng2-lng1); + double a = Math.sin(dLat/2) * Math.sin(dLat/2) + + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * + Math.sin(dLng/2) * Math.sin(dLng/2); + double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); + double dist = earthRadius * c; + + int meterConversion = 1609; + + return (float) (dist * meterConversion); + } } diff --git a/src/main/java/ai/server/controller/LoginController.java b/src/main/java/ai/server/controller/LoginController.java index abb44d0..042f893 100644 --- a/src/main/java/ai/server/controller/LoginController.java +++ b/src/main/java/ai/server/controller/LoginController.java @@ -4,6 +4,7 @@ import java.util.HashMap; import java.util.Map; +import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; @@ -11,6 +12,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.support.RedirectAttributes; @@ -75,5 +77,14 @@ public ModelAndView loginerror(){ public ModelAndView logout(){ return new ModelAndView("index"); } + + @RequestMapping(value="/android/login", method = RequestMethod.POST) + @ResponseBody + public Boolean androidLogin(HttpServletRequest request, HttpServletResponse response) + { + System.out.println("sono entrato in login android"); + + return new Boolean(true); + } } diff --git a/src/main/java/ai/server/controller/RegisterController.java b/src/main/java/ai/server/controller/RegisterController.java index 6bd7689..030752b 100644 --- a/src/main/java/ai/server/controller/RegisterController.java +++ b/src/main/java/ai/server/controller/RegisterController.java @@ -3,17 +3,26 @@ import hibernate.Utente; import java.util.Date; +import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; + + + + + +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; +import org.springframework.mobile.device.Device; import org.springframework.validation.BindingResult; import org.springframework.validation.FieldError; import org.springframework.web.bind.annotation.RequestMapping; @@ -42,11 +51,7 @@ public void setDati(Dati dati){ } @RequestMapping(value="/register", method = RequestMethod.GET) - public String showForm(Model model, HttpServletRequest httpServletRequest){ - if(httpServletRequest.getParameter("android-device")!=null){ - System.out.println("eccoci"); - return "eccoci";} - + public String showForm(Model model){ Registration registration = new Registration(); model.addAttribute("registration", registration); @@ -67,7 +72,7 @@ public void processRegistration(@Valid Registration registration, BindingResult return; } try{ - dati.inserisciUtente(registration.getEmail(), registration.getUserName(), registration.getPassword(),new Date(),numerocasuale); + dati.inserisciUtente(registration.getEmail(), registration.getUserName(), registration.getPassword(), new Date(), numerocasuale); }catch(Exception e){ System.out.println(e.getMessage()); System.out.println(e.getStackTrace()); @@ -102,4 +107,53 @@ else if(utente.getConfermato() == true) } + + @RequestMapping(value="/android/register", method = RequestMethod.POST) + @ResponseBody + public JSONArray processAndroidRegistration(@Valid Registration registration, BindingResult result, HttpServletRequest request){ + ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); + JSONArray response = new JSONArray(); + JSONObject jsonObj = new JSONObject(); + + System.out.println("calling android register"); + + registrationValidation.validate(registration, result); + String numerocasuale = java.util.UUID.randomUUID().toString(); + Map errorsMap = new HashMap(); + + if(result.hasErrors()){ + + jsonObj.put("status", false); + + for(FieldError fieldError : result.getFieldErrors()) + errorsMap.put(fieldError.getField(), fieldError.getDefaultMessage()); + jsonObj.put("errors", errorsMap); + response.add(jsonObj); + return response; + + } + try{ + dati.inserisciUtente(registration.getEmail(), registration.getUserName(), registration.getPassword(), new Date(), numerocasuale); + }catch(Exception e){ + System.out.println(e.getMessage()); + System.out.println(e.getStackTrace()); + jsonObj.put("status", false); + errorsMap.put("database", e.getMessage()); + jsonObj.put("errors", errorsMap); + response.add(jsonObj); + return response; + } + + Mail mail = (Mail) context.getBean("mail"); + + String [] temp = request.getRequestURL().toString().split("/"); + String url = temp[0]+"//"+temp[1]+temp[2]+"/"+temp[3]+"/"; + mail.sendMail("brunano21@gmail.com", registration.getEmail(), "Registration Confirmation", "Click the link above to confirm your registration\n\n\n"+""); + jsonObj.put("status", true); + response.add(jsonObj); + return response; + } + + + } diff --git a/src/main/java/spring-security.xml b/src/main/java/spring-security.xml index 51492c8..df9fb8f 100644 --- a/src/main/java/spring-security.xml +++ b/src/main/java/spring-security.xml @@ -1,45 +1,68 @@ - - - - - - - - - + + - + + + + + + + + + + + + + + - - - - - - + + + + - + - - + + - - + + + + + + + + + + + + + + + + \ No newline at end of file