Java tutorial
/******************************************************************************* * Copyright 2014 Juan Diego Navarre Gonzalez * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. ******************************************************************************/ package net.navasoft.madcoin.backend.services.controller; import javax.servlet.http.HttpServletRequest; import net.navasoft.madcoin.backend.model.controller.exceptions.AlreadyOnSourceException; import net.navasoft.madcoin.backend.services.controller.exception.impl.InactiveUserException; import net.navasoft.madcoin.backend.services.controller.exception.impl.InexistentUserException; import net.navasoft.madcoin.backend.services.controller.exception.impl.SessionControllerException; import net.navasoft.madcoin.backend.services.controller.exception.impl.UnmatchedLoginException; import net.navasoft.madcoin.backend.services.rest.ISessionService; import net.navasoft.madcoin.backend.services.vo.request.impl.AppLoginSuccessRequestVO; import net.navasoft.madcoin.backend.services.vo.request.impl.AppSignupSuccessRequestVO; import net.navasoft.madcoin.backend.services.vo.request.impl.UserInfoSuccessRequestVO; import net.navasoft.madcoin.backend.services.vo.response.FailedResponseVO; import net.navasoft.madcoin.backend.services.vo.response.SuccessResponseVO; import net.navasoft.madcoin.backend.services.vo.response.impl.AuthenticationFailedResponseVO; import net.navasoft.madcoin.backend.services.vo.response.impl.SignUpFailedResponseVO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; /** * net.navasoft.madcoin.backend.services.controller Class class * SessionController. Description: * * @author Juan Diego Navarre Gonzalez - (<a * href="mailto:jdnavarreg@outlook.com">{@literal jdnavarreg@outlook.com} * </a>) * @version 1.0 * @since 27/07/2014 06:48:25 PM */ @Controller @RequestMapping(value = "/auth") public class SessionController extends ClicknDoneController { /** * service. * * @since 27/07/2014, 06:48:25 PM */ @Autowired(required = true) @Qualifier("SessionService") private ISessionService service; /** * login. * * @since 27/07/2014, 06:48:25 PM */ private final String login = "/login"; private final String newUser = "/signup"; /** * Login. * * @param request * the request * @param fromSystem * the from system * @param fromToken * the from token * @param userAgent * the user agent * @param login_type * the login_type * @return the success response vo * @since 18/08/2014, 07:52:45 PM */ @RequestMapping(value = { login }, method = RequestMethod.POST, headers = { "Content-Type=application/json", "Accept=application/json" }) @ResponseStatus(value = HttpStatus.OK) public @ResponseBody SuccessResponseVO login(HttpServletRequest request, @RequestHeader("X-Origin-OS") String fromSystem, @RequestHeader("X-Origin-Token") String fromToken, @RequestHeader("User-Agent") String userAgent, @RequestBody(required = true) AppLoginSuccessRequestVO login_type) { String loadBalanceIP = request.getHeader("X-Forwarded-For"); String ipAddress = request.getRemoteAddr(); if (ipAddress.equals("127.0.0.1") || ipAddress.equals("localhost")) { login_type.setIpAddress(loadBalanceIP); } else { login_type.setIpAddress(ipAddress); } login_type.setGadget(userAgent); login_type.setOs(fromSystem); login_type.setSerial(fromToken); login_type.setVersion(userAgent); return service.login(buildWrapper(login_type)); } /** * Signup. * * @param request * the request * @param fromSystem * the from system * @param fromToken * the from token * @param userAgent * the user agent * @param signer * the signer * @return the success response vo * @since 18/08/2014, 08:00:20 PM */ @RequestMapping(value = { newUser }, method = { RequestMethod.PUT, RequestMethod.POST }) @ResponseStatus(value = HttpStatus.OK) public @ResponseBody SuccessResponseVO signup(HttpServletRequest request, @RequestHeader("X-Origin-OS") String fromSystem, @RequestHeader("X-Origin-Token") String fromToken, @RequestHeader("User-Agent") String userAgent, @RequestBody(required = true) AppSignupSuccessRequestVO signer) { String ipAddress = request.getRemoteAddr(); System.out.println(request.getRemoteAddr() + "/" + request.getRemoteHost()); signer.setIpAddress(ipAddress); signer.setGadget(userAgent); signer.setOs(fromSystem); signer.setSerial(fromToken); signer.setVersion(userAgent); return service.signUpUser(buildWrapper(signer)); } /** * Gets the user info. * * @param req * the req * @param cabecera * the cabecera * @param request_type * the request_type * @return the user info * @since 27/07/2014, 06:48:25 PM */ @RequestMapping(value = "/cnd-api/user_info", method = RequestMethod.POST, headers = { "Content-Type=application/json" }) @ResponseStatus(value = HttpStatus.OK) public final @ResponseBody SuccessResponseVO getUserInfo(HttpServletRequest req, @RequestHeader("Accept") String cabecera, @RequestBody(required = true) UserInfoSuccessRequestVO request_type) { // response.setUrlAvatar("http://"+req.getLocalName()+":"+req.getLocalPort()+req.getServletContext().getContextPath()+response.getUrlAvatar()); return service.getUserInfo(buildWrapper(request_type)); } } @ControllerAdvice class SessionControllerExceptionHandler { @ExceptionHandler(InactiveUserException.class) @ResponseStatus(value = HttpStatus.UNAUTHORIZED) private @ResponseBody FailedResponseVO handleInactiveUser(SessionControllerException e) { FailedResponseVO value = new AuthenticationFailedResponseVO(); value.setErrorMessage(e.getMessage()); value.setCauses(e.getCauses()); value.setTip(e.formulateTips()); return value; } @ExceptionHandler(InexistentUserException.class) @ResponseStatus(value = HttpStatus.LOCKED) private @ResponseBody FailedResponseVO handleInexistentUser(SessionControllerException e) { FailedResponseVO value = new AuthenticationFailedResponseVO(); value.setErrorMessage(e.getMessage()); value.setCauses(e.getCauses()); value.setTip(e.formulateTips()); return value; } @ExceptionHandler(UnmatchedLoginException.class) @ResponseStatus(value = HttpStatus.BAD_REQUEST) private @ResponseBody FailedResponseVO handleBadUser(SessionControllerException e) { FailedResponseVO value = new AuthenticationFailedResponseVO(); value.setErrorMessage(e.getMessage()); value.setCauses(e.getCauses()); value.setTip(e.formulateTips()); return value; } @ExceptionHandler(AlreadyOnSourceException.class) @ResponseStatus(value = HttpStatus.BAD_REQUEST) private @ResponseBody FailedResponseVO handleSQL(AlreadyOnSourceException e) { FailedResponseVO value = new SignUpFailedResponseVO(); value.setErrorMessage(e.getMessage()); value.setCauses("Alguno de los datos que proporciono ya cuenta con un usuario registrado."); String[] tips = new String[] { "Por favor, reintente con otro correo electrnico, ya que ya esta registrado en el sistema.", "Por favor, reintente con otro nombre de usuario, ya que ya esta registrado en el sistema.", "Por favor, reintente con otro numero de celular, ya que ya esta registrado en el sistema." }; value.setTip(tips); return value; } }