Unable to find valid certification path to requested target

Most of you must be familiar with the below exception message:

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

When trying to open an SSL connection to a host using Java.

Sample Code(source):

What this usually means is that the server is using a test certificate (possibly generated using keytool) rather than a certificate from a well known commercial Certification Authority such as Verisign or GoDaddy.

Web browsers display warning dialogs in this case, but since JSSE cannot assume an interactive user is present it just throws an exception by default.

To bypass this issue and be able to get the contents of a HTTPS url here is the steps:

First: use the below class code to install the required certificates(Source):

I was testing this on wikipedia.org, which is on https, thus facing the https issue.
You can modify the same as per requirement.

Next, set the following arguments while running JVM(source):

-Djavax.net.ssl.trustStore=<local path to certs>/jssecacerts

Next use the below code to get the contents(Source):

Create Virtual Hosts with Apache

Apache is very flexible, and it’s easy to configure Apache to handle several domains even when your Web server only has one IP address to share between them. You can use this to host multiple sites, or just to provide a sandbox for development rather than making changes on your live site. This weekend, we’ll learn how to create virtual hosts with Apache.

Setting up virtual hosts might seem like a big challenge, but it’s not. In fact, you can set up a virtual host with just a few edits to Apache’s configuration and by setting up additional directories for the documents. For this how to, I want to use an Apache installation on a Ubuntu server. Please be aware, the instructions for this may require modification if being done on a non-Debian distribution because of the way that Apache is packaged. However, the Apache directives should be standard across distributions and should work even if Apache isn’t running on Linux.

Creating the Directory Structure

Before the configurations can be tackled, the directory structure for the virtual site must be created. We are going to be working with Apache as installed on a Ubuntu server, so the Apache document root will be/var/www. The directory structure for the new Web site can be created anywhere. Some create those directories in their home (~/) directory, some create them in /usr/local/apache, and other, various locations. For the sake of simplicity, We are going to illustrate setting the virtual host in the document root of Apache (in Ubuntu that would be /var/www). By doing this, it will not be necessary to change ownership of the newly created directory or the parent directory housing the virtual host (since Apache must have access to the directories and files.)

We are going to set up the virtual host demosite.com. So to create the directories for the virtual host, the following commands must be run:

  • sudo mkdir /var/www/demosite.com
  • sudo chmod -R 755 /var/www/demosite.com

Now that the home directory for the virtual host is done, it’s time to start configuring Apache so it is aware of the new site. After configuring Apache, the site can then be built within /var/www/demosite.com.

Apache Configuration

The first step in the Apache configuration is to make sure Apache knows virtual hosts are enabled. In the Ubuntu set up look for a line (near the bottom) of /etc/apache/apache2.conf that looks like:

Include sites-enabled/

Make sure that line is not commented out (does not begin with a ‘#‘ character). That sites-enabled include points to /etc/apache/sites-enabled. A look in that directory will reveal a file called 000-default. That file should contain all of the directory containers needed for virtual sites on the server. To create a new virtual host (to map the example demosite.com), create a new file within sites-available called /etc/apache/sites-available/demosite.com. The contents of that file will look something similar to this (depending upon the needs and application of the virtual host):


<VirtualHost test.domain.com>
   ServerAdmin webmaster@localhost
   #We want to be able to access the web site using www.test.domain.com or test.domain.com
   ServerAlias www.test.domain.com
   DocumentRoot /var/www/demosite.com
   #log file for this server
   CustomLog /var/log/apache2/www.test.domain.com-access.log combined
</VirtualHost>

The above code assumes that domain.com is the actual domain to be used (insert proper domain where necessary). It’s almost ready to fire up, but we have two simple steps left before restarting Apache. The first step is to create a link in /etc/apache/sites-enabled to the file we just created. This is done with the following steps:

  1. Change to the /etc/apache/sites-enabled directory with the command cd /etc/apache/sites-enabled .
  2. Create the link with the command sudo ln -s /etc/apache/sites-available demosite.com .

It is also possible to create the links automatically with the command sudo a2ensite demosite.com and then, should the virtual site need to be disabled, remove the link with the command sudo a2dissite demosite.com.

The last step is to make sure the server knows that our new virtual site is to be found on the server and not out on the Internet. For this, do the following:

  • Open up the /etc/hosts file in a text editor (with administrative rights).
  • Add a line like 127.0.0.1 localhost.localdomain localhost test.domain.com www.demosite.com .
  • Save and close that file.

Now it’s time to restart Apache with the command sudo /etc/init.d/apache2 restart and test virtual host by pointing a browser to the address.

Non-Debian Servers

If the server hosting the virtual sites is a non-Debian distribution, the steps are different for hosting virtual sites. Here’s how it works:

  1. Create the directory container for the virtual site in /etc/httpd/conf/httpd.conf. This container will look similar to that used for the Debian-based server.
  2. Make sure the Apache configuration file is aware of virtual hosts by making sure the line Include conf.d/*.conf is not commented out.
  3. Create the new virtual hosts file (we’ll call it vhosts.conf) in /etc/httpd/conf.d/ .
  4. Add the virtual site to the /etc/hosts file.
  5. Restart Apache with the command /etc/rc.d/init.d/httpd restart.

The directory container, for the non-Debian host, will look similar to the very basic container below:


<VirtualHost *:80>
   ServerName demosite.com
   DocumentRoot /var/www/html/demosite.com
</VirtualHost>

The virtual hosts file mentioned above, will need to look like this:


<VirtualHost 127.0.0.1> 
   DocumentRoot /var/http/www/demosite.com
   ServerName www.demosite.com
</VirtualHost>  

<VirtualHost 127.0.0.1>
   DocumentRoot /var/http/www/demosite.com
   ServerName test.demosite.com
</VirtualHost>

Building Time

Now that the virtual host is up and running, it is now possible to build the site within the/var/www/demosite.com directory.

You can do this for as many virtual hosts as you need (assuming the server can take the added load).

Source

How to read a properties file in a web application

Let’s consider that you have a war file named SampleApp.war which has a properties file named MyApp.properties at it’s root

SampleApp.war
   |- myApp.properties
   |- WEB-INF
     |- classes
       |- org
          |- myApp
          |- MyPropertiesReader.class


Here root folder in war is equivalent to Source Folder in Netbeans.
That means you have to keep your properties file in default package of your Source Files folder.
Let’s assume that you want to read the property named “abc” present in the properties file:


myApp.properties:

abc=some value
xyz=some other value

Let’s consider that the class org.myApp. MyPropertiesReader present in your application wants to read the property. Here’s the code for the same:

package org.myapp;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class MyPropertiesReader {

    public MyPropertiesReader() {
    }

    public void doSomeOperation() throws IOException {
        InputStream inputStream = this.getClass().getClassLoader()
                .getResourceAsStream("myApp.properties");
        Properties properties = new Properties();
        System.out.println("InputStream is: " + inputStream);
        properties.load(inputStream);
        String propValue = properties.getProperty("abc");
        System.out.println("Property value is: " + propValue);
    }
}

Now suppose the properties file is not at the root of the application, but inside a folder (let’s name it config) in the web application, something like:

SampleApp.war
   |- config
    |- myApp.properties   
   |- WEB-INF
    |- classes
      |- org
         |- myApp
           |- MyPropertiesReader.class

There will just be one line change in the above code:

public void doSomeOperation() throws IOException {
        InputStream inputStream = this.getClass().getClassLoader()
                .getResourceAsStream("config/myApp.properties");
        Properties properties = new Properties();
        System.out.println("InputStream is: " + inputStream);
        properties.load(inputStream);
        String propValue = properties.getProperty("abc");
        System.out.println("Property value is: " + propValue);
    }

 

Source

Oracle ADF Features

Oracle Application Development Framework (Oracle ADF) is an advanced Rapid Application Development(RAD) Framework for J2EE applications mainly targeted for B2B Applications.

It is a proprietary software maintained by Oracle in 3 flavors:

Features-

  • RAD Framework with most commonly used components of any Front End Application available as inbuilt feature
  • Model-View-Controller (MVC) design pattern and offers an integrated solution that covers all the layers of this architecture with solution to such areas as:
    • ORM and Data Persistence
    • Reusable Task-flow approach,
    • Rich Web user interface elements,
    • Data binding to UI,
    • Security
    • Customization via sandboxes etc
  • Separation of Business Logic implementation details, via metadata and use of this metadata-driven architecture enables developers to focus on the business logic and user experience.
  • Drag-n-Drop features, via JDeveloper IDE, provides the ease to use and customize.
  • ADF Skin Editor: New ADF Skin editor for visually creating and modifying the appearance of ADF Faces applications.(JDeveloper 11G and above)
  • JSF 2.0: JSF 2.0 support in the IDE and for ADF applications. (JDev 11g R2 release)
  • ADF Faces and Data Visualization Tools (DVT) support touch gestures for interactivity on the touch-enabled devices, such as touch support for drag and drop, tooltips, data tips, and context menus.
  • ADF Faces and DVT components are certified on iPad. Additionally, ADF DVT Graph and Gauge components support rendering in HTML5.
  • MDS is metadata service that is used to store metadata information. When you use ADF, it generates lot of internal files about its own components and DB binding details. All these are stored internally by ADF into MDS.
  • ADS (Active Data service)- This is used in ADF to push any data changes automatically from the back end into the portal layer. This is different from Ajax which is used to send request from UI to data service layer.

How to create multilevel menu’s in JSP using recursion..

There was a requirement where in I need to create multi-level menu’s in a JSP

A role has certain set of Privileges.
Each privilege may or may not have child privileges..

Where Privilege has child privileges, it was desired to display a sub menu..

So this is how I have achieved…
First thing you need to know about this is how to create multi level menu’s using HTML and CSS.
(http://javakafunda.blogspot.in/2014/08/how-to-create-multi-level-menus-using.html)

and once you learn that you’ll have to use recursion in JSP to achieve the above requirement.

I’ll just put the core logic here which is the base of this..

Create a parent jsp (lets say LoginSuccess.jsp)

Create a showPrivileges.jsp as follows:








How to implement a self join in hibernate

Source : http://viralpatel.net/blogs/hibernate-self-join-annotations-one-to-many-mapping/

We have a create table in the database.

CREATE TABLE employee (
employee_id NUMBER(10) NOT NULL,
firstname VARCHAR2(50) NULL DEFAULT NULL,
lastname VARCHAR(50) NULL DEFAULT NULL,
manager_id NUMBER(10) NULL DEFAULT NULL,
PRIMARY KEY (’employee_id’),
CONSTRAINT ‘FK_MANAGER’ FOREIGN KEY (‘manager_id’) REFERENCES ’employee’ (’employee_id’)
);

Here in Employee table, we defined a column MANAGER_ID which is mapped to the same table’s primary key. Thus for each employee we will store its manager’s id also. Manager will be yet another employee in this table

We will be using annotations to implement this in hibernate:

@Entity
@Table(name="EMPLOYEE")
public class Employee {
@Id
@Column(name="EMPLOYEE_ID")
@GeneratedValue
private Long employeeId;

@Column(name="FIRSTNAME")
private String firstname;

@Column(name="LASTNAME")
private String lastname;

@ManyToOne(cascade={CascadeType.ALL})
@JoinColumn(name="manager_id")
private Employee manager;

@OneToMany(mappedBy="manager")
private Set subordinates = new HashSet();

public Employee() {
}

public Employee(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
}

// Getter and Setter methods
}

Note that in Employee entity class, we defined two new attributes: Employee manager and Set subordinates. Attribute manager is mapped with @ManyToOne annotation and subordinates is mapped with @OneToMany. Also within @OneToMany attribute we defined mappedBy=”manager” making manager as the relationship owner and thus which manages the foreign relationship within table.

Also the annotation @JoinColumn is defined on manager making it the relationship owner. @JoinColumn defines the joining column which in our case is manager_id.

How to integrate Spring and Hibernate

Here are the few tips I have gathered on how to integrate spring and hibernate.

Create your bean.xml something like this…

 












Connection.properties




${db.driverClassName}


${db.url}


${db.username}


${db.password}








com.usermanagement.pojo.User
com.usermanagement.pojo.Role
com.usermanagement.pojo.Privilege
com.usermanagement.pojo.Password
com.usermanagement.pojo.PasswordPolicy





org.hibernate.dialect.Oracle10gDialect
update
true

true
<!--org.hibernate.cache.SingletonEhCacheProvider-->
org.hibernate.cache.EhCacheProvider
































The list of jars that you might need are

  1. antlr-2.7.6.jar
  2. asm-1.5.3.jar
  3. axis2-spring-1.6.1.jar
  4. cglib-2.1_3.jar
  5. commons-collections-3.1.jar
  6. commons-dbcp-1.4.jar
  7. commons-logging-1.0.4.jar
  8. commons-pool-1.6.jar
  9. dom4j-1.6.1.jar
  10. hibernate-3.2.0.ga.jar
  11. hibernate-3.2.6.jar
  12. hibernate-annotations-3.4.0.GA.jar
  13. hibernate-commons-annotations-3.1.0.GA.jar
  14. hibernate-core-3.3.0.SP1.jar
  15. hibernate-entitymanager.jar
  16. hibernate-jpa-2.0-api-1.0.0.Final.jar
  17. jta-1.1.jar
  18. log4j-1.2.14.jar
  19. ojdbc14.jar
  20. org.springframework.asm-3.0.0.RELEASE.jar
  21. org.springframework.beans-3.0.0.RELEASE.jar
  22. org.springframework.context-3.0.0.RELEASE.jar
  23. org.springframework.core-3.0.0.RELEASE.jar
  24. org.springframework.expression-3.0.0.RELEASE.jar
  25. org.springframework.jdbc-3.0.0.RELEASE.jar
  26. org.springframework.orm-3.0.0.RELEASE.jar
  27. org.springframework.web.servlet-3.0.1.RELEASE-A.jar
  28. slf4j-api-1.6.1.jar
  29. spring-tx-3.0.0.RELEASE.jar

In JSTL/JSP when do I have to use and when can I just say ${myVar}

Source : http://stackoverflow.com/questions/6574776/in-jstl-jsp-when-do-i-have-to-use-cout-value-myvar-and-when-can-i-just

In JSTL/JSP when do I have to use and when can I just say ${myVar}

I’ve been doing this the whole time in my JSP code:

<c:out value="${myVar}"/>

Today I just realized for the first time that I seem to be able to use this shorter version just as well:

${myVar}

It works without !

Perhaps this is because my page is declared like this:

<%@ page language="java" contentType="text/html; 
charset=utf-8" pageEncoding="utf-8" isELIgnored="false" %>

So, my question is, can I replace in my code with this shorter version? Is there any reason to keep using ? Or are there places where I might still need it?

Solution:

does more than simply outputting the text. It escapes the HTML special chars.
Use it (or ${fn:escapeXml()}) every time you’re not absolutely sure that the text doesn’t contain any of these characters: “, ‘, , &. Else, you’ll have invalid HTML (in the best case), a broken page, or cross-site scripting attacks (in the worst case).

I’ll give you a simple example so that you understand.
If you develop a forum, and someone posts the following message, and you don’t use to display this message, you’ll have a problem:

while (true) alert("problem");

 

duplicate import try, using auto-import=”false”

How to use same entity class in two different packages in hibernate?

Source : http://isolasoftware.it/2011/10/14/hibernate-and-jpa-error-duplicate-import-try-using-auto-importfalse/

Using Hibernate and JPA you cannot have two classes with the same name (on different packages) mapped. This raise an error at runtime:
Caused by: org.hibernate.DuplicateMappingException: duplicate import: MyClass refers to both

To solve this issue on the Entity annotation of com.intre.MyClass and com.dummy.Class add the property name.

package com.intre;
@Entity(name = "com.intre.myclass")
@Table(name = "MyClass")
public class MyClass

package com.dummy;
@Entity(name = "com.dummy.myclass")
@Table(name = "MyClass")
public class MyClass