-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUsuario.java
More file actions
258 lines (212 loc) · 9.33 KB
/
Copy pathUsuario.java
File metadata and controls
258 lines (212 loc) · 9.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.*;
import java.net.*;
import java.util.Scanner;
class Usuario {
static int id_num=0;
public Usuario() {
this.foto = null;
}
private String email;
private String nombre;
private String apellido_paterno;
private String apellido_materno;
private String fecha_nacimiento;
private String telefono;
private String genero;
private byte[] foto;
String getEmail() { return this.email; }
String getNombre() { return this.nombre; }
String getApellidoPaterno() { return this.apellido_paterno; }
String getApellidoMaterno() { return this.apellido_materno; }
String getFechaNacimiento() { return this.fecha_nacimiento; }
String getTelefono() { return this.telefono; }
String getGenero() { return this.genero; }
byte[] getFoto() { return this.foto; }
void setEmail(String email) { this.email = email; }
void setNombre(String nombre) { this.nombre = nombre; }
void setApellidoPaterno(String apellidoPaterno) { this.apellido_paterno = apellidoPaterno; }
void setApellidoMaterno(String apellidoMaterno) { this.apellido_materno = apellidoMaterno; }
void setFechaNacimiento(String fechaNacimiento) { this.fecha_nacimiento = fechaNacimiento; }
void setTelefono(String telefono) { this.telefono = telefono; }
void setGenero(String genero) { this.genero = genero; }
void setFoto(byte[] foto) { this.foto = foto; }
public void Imprimir_datos() {
System.out.println( "Email: " + email + "\n" +
"Nombre: " + nombre + "\n" +
"Apellido Paterno: " + apellido_paterno + "\n" +
"Apellido Materno: " + apellido_materno + "\n" +
"Fecha de nacimiento: " + fecha_nacimiento + "\n" +
"Telefono: " + telefono + "\n" +
"Genero: " + genero + "\n" +
"Foto: null");
}
/** */
private static Usuario crearUsuario(Usuario usuario) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Email:");
usuario.setEmail(br.readLine());
System.out.println("Nombre:");
usuario.setNombre(br.readLine());
System.out.println("Apellido Paterno:");
usuario.setApellidoPaterno(br.readLine());
System.out.println("Apellido Materno:");
usuario.setApellidoMaterno(br.readLine());
System.out.println("Fecha de nacimiento:");
usuario.setFechaNacimiento(br.readLine());
System.out.println("Telefono:");
usuario.setTelefono(br.readLine());
System.out.println("Genero (M/F):");
usuario.setGenero(br.readLine());
id_num++;
System.out.println("El id de usuario es: " + id_num);
return usuario;
}
protected static String Consultar_usuario() throws IOException {
String email;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Introduce el e-mail del Usuario que se quiere consultar");
email = br.readLine();
return email;
}
static class Respuesta {
public Respuesta(int responseCode, String message) {
this.responseCode = responseCode;
this.message = message;
}
public void setResponseCode(int responseCode) {
this.responseCode = responseCode;
}
public void setMessage(String message) {
this.message = message;
}
public int getResponseCode() { return this.responseCode; }
public String getMessage() { return this.message; }
private int responseCode;
private String message;
}
static class Servicio {
static Respuesta hacerConsulta(String cuerpo, String metodo, String endpoint, String parametro) {
try {
URL url = new URL(URL_MAQUINA + endpoint);
HttpURLConnection conexion = (HttpURLConnection) url.openConnection();
conexion.setDoOutput(true);
conexion.setRequestMethod(metodo);
conexion.setRequestProperty(REQUEST_KEY, VALUE_KEY);
if(cuerpo.length() > 0 && parametro.length() > 0) {
String parametros = parametro + "=" + URLEncoder.encode(cuerpo, "UTF-8");
OutputStream os = conexion.getOutputStream();
os.write(parametros.getBytes(), 0, parametros.getBytes().length);
os.flush();
os.close();
}
if(conexion.getResponseCode() != HttpURLConnection.HTTP_OK)
return new Respuesta(400, "{message: 'Usuario dado de alta'}");//modificado
BufferedReader br = new BufferedReader(new InputStreamReader(conexion.getInputStream()));
String respuestaServidor;
String respuesta = "";
while((respuestaServidor = br.readLine()) != null) respuesta += respuestaServidor;
conexion.disconnect();
return new Respuesta(conexion.getResponseCode(), respuesta);
} catch(Exception e) { e.printStackTrace(); }
return new Respuesta(404, "No encontrado");
}
private final static String URL_MAQUINA = "http://104.210.211.132:8080/Servicio/rest/ws/";
private final static String REQUEST_KEY = "Content-Type";
private final static String VALUE_KEY = "application/x-www-form-urlencoded";
}
static class Error {
String message;
public Error(String message) {
this.message = message;
}
}
static class Menu {
public Menu() {}
protected char mostrarMenu() {
Scanner s = new Scanner(System.in);
System.out.println("a. Alta usuario");
System.out.println("b. Consulta usuario");
System.out.println("c. Borra usuario");
System.out.println("d. Salir");
System.out.println("\n Opción:");
char seleccion = s.nextLine().charAt(0);
return seleccion;
}
protected void opcion(char op) {
switch(op) {
case 'a':
altaUsuario();
break;
case 'b':
consultaUsuario();
break;
case 'c':
borrarUsuario();
break;
case 'd':
System.exit(0);
break;
default:
System.out.println("Opcion incorrecta");
break;
}
}
private void altaUsuario() {
GsonBuilder builder = new GsonBuilder();
builder.serializeNulls();
Gson gson = builder.create();
try {
Usuario usuario = new Usuario();
usuario = crearUsuario(usuario);
String cuerpo = gson.toJson(usuario);
Respuesta response = Servicio.hacerConsulta(cuerpo, POST_METHOD, "alta_usuario", "usuario");
if(response.getResponseCode() != 400) {
System.out.println(response.getMessage());
} else {
Error error = gson.fromJson(response.getMessage(), Error.class);
System.out.println(error.message);
}
} catch(Exception e) { e.printStackTrace(); }
}
private void consultaUsuario() {
Gson gson = new Gson();
try {
String cuerpo = Consultar_usuario();
Respuesta response = Servicio.hacerConsulta(cuerpo, GET_METHOD, "consulta_usuario", "email");
if(response.getResponseCode() != 400) {
Usuario usuario = gson.fromJson(response.getMessage(), Usuario.class);
usuario.Imprimir_datos();
} else {
Error error = gson.fromJson(response.getMessage(), Error.class);
System.out.println(error.message);
}
} catch(Exception e) { e.printStackTrace(); }
}
private void borrarUsuario() {
Gson gson = new Gson();
try {
String cuerpo = Consultar_usuario();
Respuesta response = Servicio.hacerConsulta(cuerpo, POST_METHOD, "borra_usuario", "email");
if(response.getResponseCode() != 400) {
System.out.println(response.getMessage());
} else {
Error error = gson.fromJson(response.getMessage(), Error.class);
System.out.println(error.message);
}
} catch(Exception e) { e.printStackTrace(); }
}
private final String POST_METHOD = "POST";
private final String GET_METHOD = "GET";
}
public static void main(String[] args) {
Menu Menu = new Menu();
while(true) {
char e = Menu.mostrarMenu();
Menu.opcion(e);
}
// fin main
}
// fin class Usuario
}