co.edu.uniandes.csw.Arquidalgos.usuario.service.UsuarioService.java Source code

Java tutorial

Introduction

Here is the source code for co.edu.uniandes.csw.Arquidalgos.usuario.service.UsuarioService.java

Source

/* ========================================================================
 * Copyright 2014 Arquidalgos
 *
 * Licensed under the MIT, The MIT License (MIT)
 * Copyright (c) 2014 Arquidalgos
    
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
    
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
    
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 * ========================================================================
    
    
Source generated by CrudMaker version 1.0.0.201408112050
    
*/

package co.edu.uniandes.csw.Arquidalgos.usuario.service;

import co.edu.uniandes.csw.Arquidalgos.bono.logic.dto.BonoDTO;
import co.edu.uniandes.csw.Arquidalgos.tienda.logic.dto.TiendaDTO;
import co.edu.uniandes.csw.Arquidalgos.usuario.logic.dto.UsuarioAmigosDTO;
import co.edu.uniandes.csw.Arquidalgos.usuario.logic.dto.UsuarioBonosDTO;
import co.edu.uniandes.csw.Arquidalgos.usuario.logic.dto.UsuarioDTO;
import co.edu.uniandes.csw.Arquidalgos.usuario.logic.dto.UsuarioTiendasDTO;
import java.util.List;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.ejb.Stateless;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.apache.commons.codec.binary.Hex;

@Path("/Usuario")
@Stateless
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class UsuarioService extends _UsuarioService {

    @POST
    @Path("/registrarse")
    public UsuarioDTO registrarseCliente(UsuarioDTO usuario) throws Exception {
        return this.usuarioLogicService.createUsuario(usuario);
    }

    @POST
    @Path("/agregarAmigos")
    public List<UsuarioDTO> agregarAmigos(UsuarioAmigosDTO usuarioAmigos) throws Exception {

        Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
        String key = "123";
        SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(), "HmacSHA256");
        sha256_HMAC.init(secret_key);

        String x = usuarioAmigos.toString();
        System.out.println("TO String: " + x);

        String hash = Hex.encodeHexString(sha256_HMAC.doFinal(x.getBytes()));
        System.out.println("CODIGO HASH: " + hash);
        System.out.println("CODIGO HASH JSON " + usuarioAmigos.getHash());

        boolean alterado = !(hash.equalsIgnoreCase(usuarioAmigos.getHash()));
        System.out.println("Alterado: " + alterado);

        if (alterado) {
            System.out.println("Alterado el sistema");
        }
        return this.usuarioLogicService.agregarAmigos(usuarioAmigos);
    }

    @POST
    @Path("/darAmigos")
    public List<UsuarioDTO> darAmigos(UsuarioDTO usuario) throws Exception {
        System.out.println("Dar amigos service de: " + usuario.getFacebookId());
        Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
        String key = "123";
        SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(), "HmacSHA256");
        sha256_HMAC.init(secret_key);

        System.out.println("TO String: " + usuario.toString());

        String hash = Hex.encodeHexString(sha256_HMAC.doFinal(usuario.toString().getBytes()));
        System.out.println("CODIGO HASH: " + hash);
        System.out.println("CODIGO HASH JSON " + usuario.getHash());

        boolean alterado = !(hash.equalsIgnoreCase(usuario.getHash()));
        System.out.println("Alterado: " + alterado);

        if (alterado) {
            throw new Exception("Se han alterado los datos");
        }

        return this.usuarioLogicService.darAmigosUsuario(usuario.getFacebookId());
    }

    @POST
    @Path("/darLikesUsuario")
    public List<TiendaDTO> darLikesUsuario(UsuarioTiendasDTO usuarioTiendas) {
        System.out.print("Service usuarioID: " + usuarioTiendas.getFacebookId());
        return this.usuarioLogicService.darLikesUsuario(usuarioTiendas);
    }

    @POST
    @Path("/agregarBonos")
    public List<BonoDTO> agregarBonos(UsuarioBonosDTO usuarioBonos) throws Exception {
        return this.usuarioLogicService.agregarBonos(usuarioBonos);
    }
}