Tuesday, April 8, 2014

Convert Input Stream to ByteArray

Converting the Input Stream to Byte Array is very simple. You can org.apache.commons.io.IOUtils to get the job done quickly.

The IOUtils contains a method toByteArray which takes in a InputStream and converts the input stream into a byte Array.


byte[] byteArray = IOUtils.toByteArray(in);

How to change Byte array byte[] to InputStream



The byte array can be converted into the input stream using the java.io.ByteArrayInputStream class. The ByteArrayInputStream class extends the InputStream class.

In the constructor of the ByteArrayInputStream just pass the byte array (byte[]) which you want to convert into InputStream.

InputStream stream = new ByteArrayInputStream(myBytes);

Friday, March 21, 2014

Remote X11 Over SSH

Remote X11 Over SSH

On the remote machine (Linux):
1) Ensure that X11Forwarding is enabled in /etc/ssh/sshd_conf on the remote machine.

On the local machine (Windows):
2) Install Xming.
 3) Add the Linux machine's DNS name(s) and IP address to the C:\Program Files\xming\X0.hosts file. File should containthe IP of the server for which you want X11 forwarding:
   localhost
   192.168.1.25
4) Start Xming on your Windows machine (NOT XLaunch)
5) Install SSH Secure Shell(if it's not already installed) onto your Windows machine.
6) Open ssh client and fill in the “Host Name” box.
7) Ensure the port is correct (probably 22).
8) Under Settings > Tunneling > X11 check the “Tunnel X11 connections” box.
9) If you create a profile then ensure tunneling is enabled on the profile as well.
10) Start the connection.
11) Log into the remote machine as you would do in a normal SSH session.
12) Start the X application from the command line, a window should open on your local machine with the application.
13) Minimize the SSH session, do not close it. If you close it, your X connections will close.
(It's a regular SSH session with putty but with X11 forwarding enabled.)

*The steps are pretty much the same for putty as well. you just need to switch on X11 forwarding in putty.

1) Open putty and fill in the “Host Name” box.
2) Ensure that SSH is checked and that the port is correct (probably 22).
3) Under Category > Connection > SSH > X11 check the “Enable X11 forwarding” box.
4) Click the “Open” button to start the connection.
5) Log into the remote machine as you would do in a normal SSH session.
6) Start the X application from the command line, a window should open on your local machine with the application.
7) Minimize the SSH session, do not close it. If you close it, your X connections will close.
(It's a regular SSH session with putty but with X11 forwarding enabled.)

Wednesday, March 19, 2014

Java.Lang.NoClassDefFoundError : Org/Apache/Commons/Fileupload/FileUploadException





java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileUploadException
 java.lang.Class.getDeclaredConstructors0(Native Method)
 java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
 java.lang.Class.getConstructor0(Unknown Source)
 java.lang.Class.newInstance0(Unknown Source)
 java.lang.Class.newInstance(Unknown Source)
If you see the above error then you can solve the issue by simply adding the below dependency in your pom.xml

<dependency>
  <groupId>commons-fileupload</groupId>
  <artifactId&gtcommons-fileupload</artifactId>
  <version&gt1.3</version>
</dependency>

Primefaces fileUploadListener not working

Add the following dependency in your pom.xml
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3</version>
</dependency>

Add this in your web.xml

<context-param>
  <param-name>primefaces.UPLOADER</param-name>
  <param-value>commons</param-value>
</context-param>

<filter>
  <filter-name>PrimeFaces FileUpload Filter</filter-name>
  <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
</filter>

<filter-mapping>
  <filter-name>PrimeFaces FileUpload Filter</filter-name>
  <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

In h:form it's important to write enctype="multipart/form-data"

ajax="false" for the command button is necessary

Example Using Simple File Upload

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui"
      >
    <h:head>
    <h:outputStylesheet library="css" name="table-style.css"  />
    </h:head>
 
    <h:body>
<h:form enctype="multipart/form-data">
<p:panel>
<p:messages showDetail="true"/>
<p:fileUpload value="#{fileUploadBean.file}" mode="simple" />
<p:commandButton value="Submit" ajax="false" actionListener="#{fileUploadBean.upload}"/>
</p:panel>
</h:form>
</h:body>

</html>

Bean:
package com.malware;

import java.io.Serializable;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;
import org.primefaces.model.UploadedFile;

@ManagedBean
@SessionScoped
public class FileUploadBean implements Serializable{

private static final long serialVersionUID = 3998498873477818990L;

private UploadedFile file;

public void init(){
}
    public UploadedFile getFile() {
        return file;
    }

    public void setFile(UploadedFile file) {
        this.file = file;
    }
 
    public void upload() {
        if(file != null) {        
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Successfully Uploaded File " , file.getFileName());
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }
}

Wednesday, March 12, 2014

Java.Lang.NoSuchMethodError: Org.Objectweb.Asm.ClassWriter


If you see this error while deploying application with hibernate :

SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'sessionFactory' defined in ServletContext resource 
[/WEB-INF/classes/config/database/spring/HibernateSessionFactory.xml]: 
Invocation of init method failed; nested exception is 
 
Caused by: org.hibernate.HibernateException: 
Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
 ...
 ...
Caused by: java.lang.reflect.InvocationTargetException
 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
 ...
Caused by: java.lang.NoSuchMethodError: org.objectweb.asm.ClassWriter.<init>(I)V
 at net.sf.cglib.core.DebuggingClassWriter.<init>(DebuggingClassWriter.java:47)
 ...
The error can be resolved as follows :

The ASM package is needed by the cglib package, which is part of the Hibernate libraries. If we remove that package, Jersey will work correctly, but Hibernate will stop working. To solve this conflict use cglib-nodep.jar instead of cglib.jar and keep ASM version 3.x with Jersey. cglib-nodep.jar includes some ASM classes demanded by cglib.jar, changing the package name to avoid any class conflict.

If you are using maven then change
<dependency> <groupId>cglib</groupId> <artifactId&gtcglib</artifactId> <version&gt2.2</version> </dependency>
to
<dependency> <groupId>cglib</groupId> <artifactId&gtcglib-nodep</artifactId> <version&gt2.2</version> </dependency>

Tuesday, March 11, 2014

Using to_date function oracle

The to_date function can be used to compare dates :
Usage can be as follows

select
from message 
where message_text like '%888309%' 
and message_received >= to_date('20131119', 'yyyymmdd');

The date format can be changed according to needs.