Sunday, February 26, 2017

greg turnquist "learning spring boot"

http://greglturnquist.com/learning-spring-boot



on Safari Books online I have watched the entire course by Greg Turnquist, the material is really well done, hands on and practical.... Spring Boot is the best product I have seen since a long time, it has a learning curve but it allows you to write formidably compact applications.





Saturday, February 25, 2017

Lambda basics Java 8 tutorial by Koushik

https://www.youtube.com/playlist?list=PLqq-6Pq4lTTa9YGfyhyW2CqdtW9RtY-I3

I love the hands-on, fresh and immediate way by which Koushik explains these concepts... I wish I had half of his talents...

https://javabrains.io/





Saturday, February 18, 2017

Chrome NET::ERR_CERT_REVOKED for a revoked certificate

connecting Chrome to a local development WebLogic that was using a WebServer certificate that was revoked in the CRL list, I got this message:

Your connection is not private Attackers might be trying to steal your information from localhost (for example, passwords, messages, or credit cards). NET::ERR_CERT_REVOKED Automatically report details of possible security incidents to Google. Privacy policy ReloadHide advanced localhost normally uses encryption to protect your information. When Google Chrome tried to connect to localhost this time, the website sent back unusual and incorrect credentials. This may happen when an attacker is trying to pretend to be localhost, or a Wi-Fi sign-in screen has interrupted the connection. Your information is still secure because Google Chrome stopped the connection before any data was exchanged. You cannot visit localhost right now because this certificate has been revoked. Network errors and attacks are usually temporary, so this page will probably work later.

Once I started Chrome with "chrome.exe --ignore-certificate-errors" the connection is accepted, I just get a warning "you are using an unsupported command-line flag: --ignore-certificate-errors. Stability and security will suffer "



JDB is your friend

We had recently an issue with an XML parser failing to parse a temporary file that was immediately deleted after.
How to stop execution before the file gets deleted?
I am using jdb http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html and I have created a simple test case

Start WebLogic with
set JAVA_OPTIONS=-agentlib:jdwp=transport=dt_shmem,address=jdbconn,server=y,suspend=n %JAVA_OPTIONS%

I deploy a small webapp with:

class com.pierre.MyCounter

package com.pierre;
public class MyCounter {
       static int count = 0;
       public static String getCountAsString() {
             return String.valueOf(count++);
       }
}

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="com.pierre.*"  %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here bla</title>
</head>
<body>
The count is:
<%
       out.write(MyCounter.getCountAsString());
%>
 
<br/>
I hope you are happy.
 
</body>
</html>
 


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<display-name>testjdb</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>


Then run from a command line :
jdb -attach jdbconn


run your case once to make sure the MyCounter class has already been loaded, then run

classes

to check if the MyCounter is there, then run this to set a breakpoint:

stop at com.pierre.MyCounter:6

next time you run the jsp, the breakpoint is hit and execution stops


Sunday, February 12, 2017

Spring JDBC tutorial by JavaBrains


















Spring AOP videos by JavaBrains















Saturday, February 11, 2017

Great Spring basic introduction part 2

part 10

part 11


part 12

part 13

part 14

part 15

part 16

part 17

part 18

part 19

part 20

part 21

part 22

part 23

part 24


make logger.debug() obsolete

IMHO logging is the weakest point in application development, the Cinderella of IT.
Most people don't log enough information to enable effective troubleshooting, the context is lost, changing logging level in PROD is hell, if you enable debug level you end up being flooded with unwanted info, most of the time operators don't even have instructions on how to do it.
Besides one is interested in knowing what happened immediately before an error occurred, so one should always have like a Flight Recorder with some history preceding the error and ready to be dumped when this happens.
Also, frameworks like ByteBuddy or Btrace or AOP products allows you to dynamically define loggers... logging is a cross-cutting concern, it should not be interspersed in the business code cluttering it. Just define what to log in a separate module - configuration and convention over code.
Here at OverOps they seem to bring some value:

https://www.overops.com/java-monitoring

watch the short video:




Friday, February 10, 2017

Great Spring basic introduction (part 1)

part 1

part 2

part 3

part 4

code for part 4 is here https://github.com/vernetto/JavaMonAmour/tree/master/SpringDemo
part 5

part 6

part 7

part 8
part 9






Saturday, February 4, 2017

Cool ReactJS introduction



The guy on the right is really good - however I was surprised, the need for separation between Model and View has been evident for half a century, it should not be matter of a dissertation.

Code is available here https://github.com/AlwaysBCoding/Episodes/tree/master/simplereact

Anyway, I keep thinking that coding large projects in JS is madness. Totally unrefactorable, very hard to debug.

Eclipse M2_REPO

In Eclipse, if you go to Window/Preferences , Java/Build Path/Classpath Variable , there you see M2_REPO as "not modifiable".
to change it, you should to to Window/Preferences/ Maven / User Settings and point to the location of your settings.xml where you have defined localRepository to the value D:\pierre\.m2\repository (for example).

MAVEN = Mad Aberrant Vituperative Endless Nonsense

See also:
https://www.mkyong.com/maven/how-to-configure-m2_repo-variable-in-eclipse-ide/
https://www.mkyong.com/maven/maven-m2_repo-is-non-modifiable/


SnoopServlet

Create a Dynamic Web project "SnoopServlet"

This is the web.xml:


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>SnoopServlet</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>MySnoopServlet</display-name>
    <servlet-name>MySnoopServlet</servlet-name>
    <servlet-class>MySnoopServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>MySnoopServlet</servlet-name>
    <url-pattern>/MySnoopServlet</url-pattern>
  </servlet-mapping>
</web-app>


This is the weblogic.xml:

<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-web-app xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-web-app" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd http://xmlns.oracle.com/weblogic/weblogic-web-app http://xmlns.oracle.com/weblogic/weblogic-web-app/1.9/weblogic-web-app.xsd">
    <wls:weblogic-version>12.2.1.2</wls:weblogic-version>
    <wls:context-root>SnoopServlet</wls:context-root>
</wls:weblogic-web-app>



import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@SuppressWarnings("serial")
public class MySnoopServlet extends HttpServlet
{
    public int mycount = 0;
    public MySnoopServlet()
    {
    }

    public void destroy()
    {
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
     mycount+=1;
        HttpSession session;
        PrintWriter out;
        response.setContentType("text/html");
        session = request.getSession();
        out = response.getWriter();
        try {
         out.println("<html>");
         out.println("<head><title>SnoopServlet</title></head>");
         out.println("<body text='#ffffff' bgcolor='#666699' link='#ffffff' vlink='#ffffff' alink='#ffffff'>");
         out.println("<p>The servlet has received a GET. This is the reply.</p>");
         out.flush();
         out.print("<p>Request");
         out.print("<br>Principal = " + request.getUserPrincipal());
         out.print("<br>URL = " + request.getRequestURL().toString());
         out.print("<br>AuthType = " + request.getAuthType());
         out.print("<br>RemoteUser = " + request.getRemoteUser());
         out.print("<br>ServerName = " + System.getProperty("weblogic.Name"));
         out.print("<br>SessionID = " + session.getId());
         out.println("<br><hr> <br>");
         Enumeration enum1 = request.getHeaderNames();
         out.print("<p>Header");
         String item;
         for(; enum1.hasMoreElements(); out.print("<br>" + item + "=" + request.getHeader(item)))
             item = (String)enum1.nextElement();
 
         out.flush();
         out.println("<br><hr> <br>");
         out.print("<p>Attributes");
         for(enum1 = request.getAttributeNames(); enum1.hasMoreElements(); out.print("<br>" + item + "=" + request.getAttribute(item)))
             item = (String)enum1.nextElement();
 
         out.flush();
         out.println("<br><hr> <br>");
         out.print("<p>Parameters");
         for(enum1 = request.getParameterNames(); enum1.hasMoreElements(); out.print("<br>" + item + "=" + request.getParameter(item)))
             item = (String)enum1.nextElement();
 
         out.println("<br><hr> <br>");
         out.flush();
       }
       catch (Throwable th) {
        out.print("<pre>");
        th.printStackTrace();
        th.printStackTrace(out);
        out.print("</pre>");
      }
      finally {
        out.println("</body></html>");
      }
        return;
    }

    public void init()
        throws ServletException
    {
    }

    
}




http://localhost:7001/SnoopServlet/MySnoopServlet?pippo=pluto

The servlet has received a GET. This is the reply.

Request
Principal = null
URL = http://192.168.56.1:7001/SnoopServlet/MySnoopServlet
AuthType = null
RemoteUser = null
ServerName = AdminServer
SessionID = MHcJQYLAVotakdRTZ2rAwUj_sRjWlQ3Bui-_d50iyOJwAwNJW6B2!837838669!1486213972672

Header
Host=192.168.56.1:7001
User-Agent=Mozilla/5.0 (Windows NT 10.0; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0
Accept=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language=en-US,en;q=0.5
Accept-Encoding=gzip, deflate
Cookie=JSESSIONID=MowJK_Z1Wj2l48jsHZqf21DItW3tklujnqPmzh6Uj9vnI9CEtDfX!-1767948456
Connection=keep-alive
Upgrade-Insecure-Requests=1

Attributes

Parameters
pippo=pluto

The JSP can be found in $WL_HOME/samples/server/examples/src/examples/security/sslclient/src/main/webapp/SnoopServlet.jsp

<!-- Copyright (c) 1999,2015, Oracle and/or its affiliates. All Rights Reserved.-->
<%@ page import="java.util.Enumeration,
      java.io.PrintWriter"%>

<%!
   /**
  * <p>This helper method can be used to help prevent Cross Site Scripting
  * vulnerabilities. Any Servlet or JSP which sends user input (eg.
  * query parameters in HTTP requests) to be rendered into a user's browser
  * needs to use this method to encode the user input.  This ensures that any
  * HTML in their input (either malicious or otherwise) is not executed by
  * the browser.  This is achieved by converting characters to their HTML
  * escaped form.  For example, '&' is converted to '&amp;amp;'.
  * <p>
  * A full description of Cross Site Scripting (XSS) vulnerabilities can
  * be found at
  * <a href="http://www.cert.org/tech_tips/malicious_code_mitigation.html">
  * http://www.cert.org/tech_tips/malicious_code_mitigation.html</a>.
  *
  * @param str
  */
  public String encodeXSS(String str) {
   return weblogic.servlet.security.Utils.encodeXSS(str);
  }
%>

<%
 try {
%>
   <p>
    This servlet returns information about the HTTP request
    itself. You can modify this servlet to take this information
    and store it elsewhere for your HTTP server records. This
    servlet is also useful for debugging.
      </p>
   <h3>
   Servlet Spec Version Implemented
   </h3>
   <pre>
   <%= getServletConfig().getServletContext().getMajorVersion() + "." + getServletConfig().getServletContext().getMinorVersion() %>
   </pre>
   <h3>
   Requested URL
   </h3>
   <pre>
   <%= request.getRequestURL().toString() %>
   </pre>
   <h3>
   Request parameters
   </h3>
   <pre>
<%

   Enumeration enum_ = request.getParameterNames();
   while(enum_.hasMoreElements()){
     String key = (String)enum_.nextElement();
     String[] paramValues = request.getParameterValues(key);
     for(int i=0;i < paramValues.length;i++){
         out.println(key + " : "  + encodeXSS(paramValues[i]));
     }
   }

%>
   </pre>
   <h3>
   Request information
   </h3>
   <pre>
   Request Method: <%= request.getMethod() %>
   Request URI: <%= request.getRequestURI() %>
   Request Protocol: <%= request.getProtocol() %>
   Servlet Path: <%= request.getServletPath() %>
   Path Info: <%= request.getPathInfo() %>
   Path Translated: <%= request.getPathTranslated() %>
   Query String: <%= encodeXSS(request.getQueryString()) %>
   Content Length: <%= request.getContentLength() %>
   Content Type: <%= request.getContentType() %>
   Server Name: <%= request.getServerName() %>
   Server Port: <%= request.getServerPort() %>
   Remote User: <%= request.getRemoteUser() %>
   Remote Address: <%= request.getRemoteAddr() %>
   Remote Host: <%= request.getRemoteHost() %>
   Authorization Scheme: <%= request.getAuthType() %>
   </pre>
   <h3>Certificate Information</h3>
   <pre>
<%
   java.security.cert.X509Certificate certs [];
   certs = (java.security.cert.X509Certificate [])
   request.getAttribute("javax.servlet.request.X509Certificate");
   if ((certs != null) && (certs.length > 0)) {
%>
    Subject Name : <%= certs[0].getSubjectDN().getName() %> <br>
    Issuer Name :<%= certs[0].getIssuerDN().getName() %> <br>
    Certificate Chain Length : <%= certs.length %> <br>
<%

      // List the Certificate chain
      for (int i=0; i<certs.length;i++) {
%>  Certificate[<%= i %>] : <%= certs[i].toString() %>

<%
    } // end of for loop

   }
   else // certs==null
    {
%>
    Not using SSL or client certificate not required.
<%
    } // end of else
%>
   </pre>
   <h3>
   Request headers
   </h3>
   <pre>
<%
   enum_ = request.getHeaderNames();
   while (enum_.hasMoreElements()) {
    String name = (String)enum_.nextElement();
    out.println(name + ": " +encodeXSS(request.getHeader(name)));
   }
%>
   </pre>
  </td>
 </tr>
<%
 }
 catch (Exception ex) {
  ex.printStackTrace(new PrintWriter(out));
 }
%>


http://localhost:7001/SnoopServlet/SnoopServlet.jsp