Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package br.com.ufjf.labredes.rest; import br.com.ufjf.labredes.model.CandidatoList; import br.com.ufjf.labredes.model.Eleitor; import br.com.ufjf.labredes.model.Voto; import br.com.ufjf.labredes.modelService.CandidatoService; import br.com.ufjf.labredes.modelService.EleitorService; import static br.com.ufjf.labredes.rest.ConectaResource.client_aes; import static br.com.ufjf.labredes.rest.ConectaResource.rsa; import br.com.ufjf.labredes.servletUtil.Response; import java.util.Base64; import javax.crypto.SealedObject; import javax.ws.rs.Consumes; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import org.apache.commons.lang.SerializationUtils; /** * * @author leoja */ @Path("/eleitor") @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8") @Consumes(MediaType.APPLICATION_JSON + ";charset=utf-8") public class EleitorResource { private EleitorService eleitorService = new EleitorService(); private CandidatoService candidatoService = new CandidatoService(); @POST @Path("/{eleitor}") //3. Envia Credenciais public String login(@PathParam("eleitor") String elector) { byte[] elector_decoded = Base64.getDecoder().decode(elector); SealedObject aux = (SealedObject) SerializationUtils.deserialize(elector_decoded); Eleitor eleitor = (Eleitor) rsa.decrypt(aux, client_aes); eleitor = eleitorService.getEleitor(eleitor.getCpf(), eleitor.getSenha()); Response ans = new Response(); CandidatoList candidatos = new CandidatoList(); if (eleitor != null) { //3.1 Responde candidatos.setCandidatos(candidatoService.getAll()); byte[] data = SerializationUtils.serialize(rsa.encrypt(candidatos, client_aes)); String retorno = Base64.getEncoder().encodeToString(data); return retorno; } else { ans.Error("Erro ao decriptar chave"); //3.1 Responde byte[] data = SerializationUtils.serialize(rsa.encrypt(ans, client_aes)); String retorno = Base64.getEncoder().encodeToString(data); return retorno; } } @POST @Path("/votar/{voto}") public String votar(@PathParam("voto") String vote) { byte[] voto_decoded = Base64.getDecoder().decode(vote); SealedObject aux = (SealedObject) SerializationUtils.deserialize(voto_decoded); Voto voto = null; voto = (Voto) rsa.decrypt(aux, client_aes); Response ans = new Response(); if (voto != null) { eleitorService.votar(voto.getCpf(), voto.getNumero()); ans.Ok("Voto computado"); //3.1 Responde byte[] data = SerializationUtils.serialize(rsa.encrypt(ans, client_aes)); String retorno = Base64.getEncoder().encodeToString(data); return retorno; } else { ans.Error("Erro ao decriptar chave"); //3.1 Responde byte[] data = SerializationUtils.serialize(rsa.encrypt(ans, client_aes)); String retorno = Base64.getEncoder().encodeToString(data); return retorno; } } }