Subversion Repositories Integrator Subversion

Rev

Rev 305 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 305 Rev 356
Line 1... Line 1...
1
package br.com.ec.controller.seguranca;
1
package br.com.ec.controller.seguranca;
2
2
-
 
3
import java.io.BufferedInputStream;
3
import java.io.IOException;
4
import java.io.IOException;
-
 
5
import java.io.InputStream;
4
import java.io.Serializable;
6
import java.io.Serializable;
-
 
7
import java.net.InetAddress;
-
 
8
import java.net.NetworkInterface;
-
 
9
import java.net.SocketException;
-
 
10
import java.net.UnknownHostException;
-
 
11
import java.text.ParseException;
-
 
12
import java.util.Enumeration;
-
 
13
import java.util.StringTokenizer;
5
14
6
import javax.faces.context.FacesContext;
15
import javax.faces.context.FacesContext;
7
import javax.inject.Inject;
16
import javax.inject.Inject;
8
import javax.inject.Named;
17
import javax.inject.Named;
-
 
18
import javax.servlet.http.HttpServletRequest;
-
 
19
import javax.servlet.http.HttpServletResponse;
9
20
10
import org.springframework.context.annotation.Scope;
21
import org.springframework.context.annotation.Scope;
11
22
12
import br.com.ec.core.util.VerificadorUtil;
23
import br.com.ec.core.util.VerificadorUtil;
13
import br.com.ec.domain.model.Loja;
24
import br.com.ec.domain.model.Loja;
Line 207... Line 218...
207
        }
218
        }
208
       
219
       
209
        public String textoSobreCorMargem() {
220
        public String textoSobreCorMargem() {
210
                return ConstantesSEC.Textos.TEXTO_COR_MARGEM;
221
                return ConstantesSEC.Textos.TEXTO_COR_MARGEM;
211
        }
222
        }
-
 
223
       
-
 
224
        /*
-
 
225
       
-
 
226
        public FacesContext getContext() {
-
 
227
                return FacesContext.getCurrentInstance();
-
 
228
        }
-
 
229

-
 
230
        private boolean verificarContexto() {
-
 
231
                FacesContext context = getContext();
-
 
232
                return VerificadorUtil.naoEstaNulo(context) && VerificadorUtil.naoEstaNulo(context.getExternalContext());
-
 
233
        }
-
 
234
       
-
 
235
        public HttpServletRequest getRequest() {
-
 
236
                return verificarContexto() ? (HttpServletRequest) getContext().getExternalContext().getRequest() : null;
-
 
237
        }
-
 
238
       
-
 
239
        public HttpServletResponse getResponse() {
-
 
240
                return verificarContexto() ? (HttpServletResponse) getContext().getExternalContext().getResponse() : null;
-
 
241
        }
-
 
242
       
-
 
243
        public String nomeMaquina() {
-
 
244
                String nomeMaquina = "";
-
 
245
                try {
-
 
246
                        nomeMaquina = InetAddress.getLocalHost().getHostName();
-
 
247
                        nomeMaquina = getRequest().getLocalName();
-
 
248
                } catch (UnknownHostException e) {
-
 
249
                        e.printStackTrace();
-
 
250
                }
-
 
251
                return nomeMaquina;
-
 
252
        }
-
 
253
       
-
 
254
        public String ipMaquina() {
-
 
255
                String ipMaquina = "";
-
 
256
                try {
-
 
257
                        ipMaquina = InetAddress.getLocalHost().getHostAddress();
-
 
258
                        ipMaquina = getRequest().getLocalAddr();
-
 
259
                } catch (UnknownHostException e) {
-
 
260
                        e.printStackTrace();
-
 
261
                }
-
 
262
                return ipMaquina;
-
 
263
        }
-
 
264
       
-
 
265
        public String ipMaquina2() {
-
 
266
                String ipMaquina = "";
-
 
267
                try {
-
 
268
                        ipMaquina = InetAddress.getLocalHost().getHostAddress();
-
 
269
//                      ipMaquina = getRequest().getRemoteAddr();
-
 
270
                        ipMaquina = getRequest().getHeader("X-FORWARDED-FOR");
-
 
271
                    if (ipMaquina == null) {
-
 
272
                        return getRequest().getRemoteAddr();
-
 
273
                    } else {
-
 
274
                        // As of https://en.wikipedia.org/wiki/X-Forwarded-For
-
 
275
                        // The general format of the field is: X-Forwarded-For: client, prox y1, proxy2 ...
-
 
276
                        // we only want the client
-
 
277
                        return new StringTokenizer(ipMaquina, ",").nextToken().trim();
-
 
278
                    }
-
 
279
                } catch (UnknownHostException e) {
-
 
280
                        e.printStackTrace();
-
 
281
                }
-
 
282
                return ipMaquina;
-
 
283
        }
-
 
284
       
-
 
285
        public String ipMaquina3() {
-
 
286
                String ipMaquina = "";
-
 
287
                try {
-
 
288
                        ipMaquina = InetAddress.getLocalHost().getHostAddress();
-
 
289
//                      ipMaquina = getRequest().getRemoteHost();
-
 
290
                       
-
 
291
            Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces();
-
 
292
            StringBuilder macs = new StringBuilder("");
-
 
293
            while (networks.hasMoreElements()) {
-
 
294
                NetworkInterface network = networks.nextElement();
-
 
295
                byte[] mac = network.getHardwareAddress();
-
 
296
                if (mac != null) {
-
 
297
//                    System.out.print("Current MAC address : ");
-
 
298
                    StringBuilder sb = new StringBuilder();
-
 
299
                    for (int i = 0; i < mac.length; i++) {
-
 
300
                        sb.append(String.format(
-
 
301
                            "%02X%s", mac[i],
-
 
302
                            (i < mac.length - 1) ? "-" : ""));
-
 
303
                    }
-
 
304
                    // MAC Address
-
 
305
//                    System.out.println(sb.toString());
-
 
306
                    macs.append(sb.toString());
-
 
307
                    macs.append("\n");
-
 
308
                }
-
 
309
            }
-
 
310
            ipMaquina = macs.toString();
-
 
311
                } catch (UnknownHostException e) {
-
 
312
                        e.printStackTrace();
-
 
313
                } catch (SocketException e) {
-
 
314
                        // TODO Auto-generated catch block
-
 
315
                        e.printStackTrace();
-
 
316
                }
-
 
317
                return ipMaquina;
-
 
318
        }
-
 
319
       
-
 
320
        public String ipMaquina4() {
-
 
321
                StringBuilder ipMaquina = new StringBuilder("");
-
 
322
                try {
-
 
323
//                      ipMaquina = InetAddress.getLocalHost().getHostAddress();
-
 
324
                        ipMaquina.append("Informacoes");
-
 
325
                        ipMaquina.append("Sistema operacional: " + System.getProperty("os.name"));
-
 
326
                        ipMaquina.append("IP/Localhost: " + InetAddress.getLocalHost().getHostAddress());
-
 
327
                        ipMaquina.append("Nome da maquina: " + InetAddress.getLocalHost().getHostName());
-
 
328
                        ipMaquina.append("Nome completo da maquina: " + InetAddress.getLocalHost().getCanonicalHostName());
-
 
329
                        ipMaquina.append("Nome da maquina2: " + InetAddress.getLoopbackAddress().getHostName());
-
 
330
                        ipMaquina.append("Nome completo da maquina2: " + InetAddress.getLoopbackAddress().getCanonicalHostName());
-
 
331
                        ipMaquina.append("MAC Address: " + getMacAddress());
-
 
332
                } catch (UnknownHostException e) {
-
 
333
                        e.printStackTrace();
-
 
334
                } catch (IOException e) {
-
 
335
                        // TODO Auto-generated catch block
-
 
336
                        e.printStackTrace();
-
 
337
                }
-
 
338
                return ipMaquina.toString();
-
 
339
        }
-
 
340
       
-
 
341
        private final static String windowsParseMacAddress(String ipConfigResponse) throws ParseException {
-
 
342
            String localHost = null;
-
 
343
            try {
-
 
344
                localHost = InetAddress.getLocalHost().getHostAddress();
-
 
345
            } catch(java.net.UnknownHostException ex) {
-
 
346
                ex.printStackTrace();
-
 
347
                throw new ParseException(ex.getMessage(), 0);
-
 
348
            }
-
 
349

-
 
350
            StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "\n");
-
 
351
            String lastMacAddress = null;
-
 
352
            StringBuilder infor = new StringBuilder("");
-
 
353

-
 
354
            while(tokenizer.hasMoreTokens()) {
-
 
355
                String line = tokenizer.nextToken().trim();
-
 
356
                infor.append(line);
-
 
357
                infor.append("\n");
-
 
358
               
-
 
359
                //IP
-
 
360
                if(line.endsWith(localHost) && lastMacAddress != null) {
-
 
361
                    return lastMacAddress;
-
 
362
                }
-
 
363

-
 
364
                //MAC address
-
 
365
                int macAddressPosition = line.indexOf(":");
-
 
366
                if(macAddressPosition <= 0) continue;
-
 
367

-
 
368
                String macAddressCandidate = line.substring(macAddressPosition + 1).trim();
-
 
369
                if(windowsIsMacAddress(macAddressCandidate)) {
-
 
370
                    lastMacAddress = macAddressCandidate;
-
 
371
                    continue;
-
 
372
                }
-
 
373
            }
-
 
374

-
 
375
            return infor.toString();
-
 
376
//          ParseException ex = new ParseException("Nao foi possível ler o MAC address de [" + ipConfigResponse + "]", 0);
-
 
377
//          ex.printStackTrace();
-
 
378
//          throw ex;
-
 
379
        }
-
 
380

-
 
381
        private final static boolean windowsIsMacAddress(String macAddressCandidate) {
-
 
382
            if(macAddressCandidate.length() != 17) return false;
-
 
383

-
 
384
            return true;
-
 
385
        }
-
 
386
       
-
 
387
        private final static String windowsRunIpConfigCommand() throws IOException {
-
 
388
            Process p = Runtime.getRuntime().exec("ipconfig /all");
-
 
389
            InputStream stdoutStream = new BufferedInputStream(p.getInputStream());
-
 
390

-
 
391
            StringBuffer buffer= new StringBuffer();
-
 
392
            for (;;) {
-
 
393
                int c = stdoutStream.read();
-
 
394
                if (c == -1) break;
-
 
395
                buffer.append((char)c);
-
 
396
            }
-
 
397
            String outputText = buffer.toString();
-
 
398

-
 
399
            stdoutStream.close();
-
 
400

-
 
401
            return outputText;
-
 
402
        }
-
 
403
       
-
 
404
       
-
 
405
        private final static String getMacAddress() throws IOException {
-
 
406
            try {
-
 
407
            return windowsParseMacAddress(windowsRunIpConfigCommand());
-
 
408
            } catch(ParseException ex) {
-
 
409
                ex.printStackTrace();
-
 
410
                throw new IOException(ex.getMessage());
-
 
411
            }
-
 
412
        }
-
 
413
       
-
 
414
        public final static void main(String[] args) {
-
 
415
                try {
-
 
416
                        System.out.println("Informacoes");
-
 
417
                        System.out.println("Sistema operacional: " + System.getProperty("os.name"));
-
 
418
                        System.out.println("IP/Localhost: " + InetAddress.getLocalHost().getHostAddress());
-
 
419
                        System.out.println("Nome da maquina: " + InetAddress.getLocalHost().getHostName());
-
 
420
                        System.out.println("Nome completo da maquina: " + InetAddress.getLocalHost().getCanonicalHostName());
-
 
421
                        System.out.println("Nome da maquina2: " + InetAddress.getLoopbackAddress().getHostName());
-
 
422
                        System.out.println("Nome completo da maquina2: " + InetAddress.getLoopbackAddress().getCanonicalHostName());
-
 
423
                        System.out.println("MAC Address: " + getMacAddress());
-
 
424
                       
-
 
425
                   
-
 
426
            // create an Enumeration of type
-
 
427
            // NetworkInterface and store the values
-
 
428
            // returned by
-
 
429
            // NetworkInterface.getNetworkInterfaces()
-
 
430
            // method
-
 
431
            Enumeration<NetworkInterface> networks = NetworkInterface.getNetworkInterfaces();
-
 
432
           
-
 
433
            // for every network in the networks Enumeration
-
 
434
            while (networks.hasMoreElements()) {
-
 
435
                NetworkInterface network
-
 
436
                    = networks.nextElement();
-
 
437
               
-
 
438
                // call getHardwareAddress() method on each
-
 
439
                // network and store the returned value in a
-
 
440
                // byte array
-
 
441
                byte[] mac = network.getHardwareAddress();
-
 
442
 
-
 
443
                if (mac != null) {
-
 
444
                    System.out.print(
-
 
445
                        "Current MAC address : ");
-
 
446
                   
-
 
447
                    // convert the obtained byte array into
-
 
448
                    // a printable String
-
 
449
                    StringBuilder sb = new StringBuilder();
-
 
450
                    for (int i = 0; i < mac.length; i++) {
-
 
451
                        sb.append(String.format(
-
 
452
                            "%02X%s", mac[i],
-
 
453
                            (i < mac.length - 1) ? "-"
-
 
454
                                                 : ""));
-
 
455
                    }
-
 
456
                   
-
 
457
                    // print the final String containing the
-
 
458
                    // MAC Address
-
 
459
                    System.out.println(sb.toString());
-
 
460
                }
-
 
461
            }
-
 
462
                       
-
 
463
                } catch(Throwable t) {
-
 
464
                        t.printStackTrace();
-
 
465
                }
-
 
466
        }
-
 
467
       
-
 
468
        */
212
       
469
       
213
}
470
}