Thursday, 19 September 2013

Upload via SFTP in Java

1. Add Maven dependency:
        <dependency>
            <groupId>com.jcraft</groupId>
            <artifactId>jsch</artifactId>
            <version>0.1.42</version>
        </dependency>

2. Java code:

import java.io.File;
import java.io.FileInputStream;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;


public class TestJSftp {
      
    public static void upload(String username,
                              String password,
                              String host,
                              int port,
                              String workingDir,
                              String localFilename){
        try {
            JSch jsch = new JSch();
            Session session = jsch.getSession(username, host, port);
            session.setPassword(password);
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
            session.connect();
            Channel channel = session.openChannel("sftp");
            channel.connect();
            ChannelSftp channelSftp = (ChannelSftp) channel;
            channelSftp.cd(workingDir);
   
            File f1 = new File(localFilename);
            channelSftp.put(new FileInputStream(f1), f1.getName(), ChannelSftp.OVERWRITE);
               
            channelSftp.exit();
            session.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}







Thursday, 18 July 2013

Diacritics problem in Spring web app and solution

I had a diacritics problem in Spring Web application. The XML file is stored in UTF-8 and the XML parser read it properly with UTF-8 encoding - if I log the texts just read, it shows fine with diacritics in the log. However, when I display it in the web page, it shows some strange '?' which means the diacritics can not be passed properly.

Here is the solution:

1. Put encodingFilter as the first filter in web.xml:
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

2. Put 'UTF-8' in both contentType and pageEncoding
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>

It solved my problem!

Thursday, 2 August 2012

Search diacritics in solr

The problem: 
You want to search text without diacritic but Solr will response diacritics and non-diacritic text (English). For example, You search for "solr index", Solr should response "solr index", "sôlr index", "sòlr index", "sólr indèx",...

Solution:
<fieldType name="text" class="solr.TextField" positionIncrementGap="100">
      <analyzer type="index">
        <tokenizer class="solr.WhitespaceTokenizerFactory"/>
        <filter class="solr.StopFilterFactory"  ignoreCase="true"  words="stopwords.txt" enablePositionIncrements="true" />
        <filter class="solr.WordDelimiterFilterFactory" generateWordParts="1" generateNumberParts="1" catenateWords="1" catenateNumbers="1" catenateAll="0" splitOnCaseChange="1"/>
        <filter class="solr.ASCIIFoldingFilterFactory"/>
        <filter class="solr.LowerCaseFilterFactory"/>

        <filter class="solr.SnowballPorterFilterFactory" language="English" protected="protwords.txt"/>
      </analyzer>
      <analyzer type="query">
        <tokenizer class="solr.WhitespaceTokenizerFactory"/>
        <filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/>
        <filter class="solr.StopFilterFactory"
                ignoreCase="true"
                words="stopwords.txt"
                enablePositionIncrements="true"
                />
        <filter class="solr.WordDelimiterFilterFactory" generateWordParts="1" generateNumberParts="1" catenateWords="0" catenateNumbers="0" catenateAll="0" splitOnCaseChange="1"/>
          <filter class="solr.ASCIIFoldingFilterFactory"/>        

        <filter class="solr.LowerCaseFilterFactory"/>
        <filter class="solr.SnowballPorterFilterFactory" language="English" protected="protwords.txt"/>
      </analyzer>
    </fieldType>

Wednesday, 1 August 2012

String concatenation problem and its fix in Python

When read string from keyboard or file, e.g.
file_date = str(input("Enter file date: "))
It may cause the cursor to go back to the start of the line when you try to print it out. You may want to trim the return value of the string.

For example, STRING_VALUE.rstrip()

Wednesday, 25 July 2012

Send email via gmail smtp in Javamail

@Test
public void testGMail() throws Exception {
        Properties props = System.getProperties();
       
        String[] tos = {"XXX,YYY,ZZZ"};
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");
        user = "Gmail email address";
        pass = "XXXX";
       
        Session session = Session.getInstance(props,
                  new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(user, pass);
                    }
                  });
          
        javax.mail.internet.MimeMessage message = new javax.mail.internet.MimeMessage(session); 
        message.setFrom(new javax.mail.internet.InternetAddress(from)); 
        for(String to : tos) {
            message.addRecipient(Message.RecipientType.TO, new javax.mail.internet.InternetAddress(to)); 
        } 
        message.setSubject("Test gmail");
        message.setContent("Test gmail content", "text/html");
      
        Transport.send(message);
        System.out.println("Done.");
}

Thursday, 19 July 2012

Compile mulgara 2.1.4 from source

1. in line 73 of build.sh, add:
CLASSPATH="${CLASSPATH}:lib/servlet-api-2.5-6.1.11.jar"

2. make sure you've installed JDK5 (not working in JDK6/7)

3. run ./build.sh clean, then ./build.sh dist

Wednesday, 11 July 2012

Java Mail does not set a subject problem (Conflicting with openejb)

Considering the following code:

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Test{
    private static Properties props = System.getProperties();

    public static void main(String[] args) throws Exception {
        String     host  = "xxxx";
        String     port = "xx";
        String     user = "xxxx";
        String     from = "xxxx";
        String     pass = "xxxx";
        String     text = "Test mail";
        String     to   = "xxxx";
       
        javax.mail.Session session = javax.mail.Session.getDefaultInstance(props, null); 
        javax.mail.internet.MimeMessage message = new javax.mail.internet.MimeMessage(session); 
        message.setFrom(new javax.mail.internet.InternetAddress(from)); 
        message.addRecipient(javax.mail.Message.RecipientType.TO, new javax.mail.internet.InternetAddress(to));
        message.setSubject("Test subject"); 
        message.setText(text); 
               
        javax.mail.Transport transport = session.getTransport("smtp"); 
        transport.connect(host, Integer.parseInt(port), user, pass);
        transport.sendMessage(message, message.getAllRecipients()); 
        transport.close();
    }
}

It works fine as a standalone Java app, but not working with Apache openejb. When I commented out this dependency in Maven pom.xml, it works again :-)
       <dependency>
            <groupId>org.apache.openejb</groupId>
            <artifactId>javaee-api</artifactId>
            <version>5.0-1</version>
            <scope>provided</scope>
        </dependency>

A related post is here: http://bit.ly/NSIoyx. The text is copied below:
----------------------------------------------------------------------------------

Strange javamail behaviour inside tomcat

In the series: 'weird problems you'd rather not spend your valuable time on' today I present a strange javamail/tomcat related problem and its solution.

While preparing the next version of our java web app I noticed that emails sent by our app were missing the mail subject. Moreover the message appeared to get sent as plain text instead of HTML so the message body was displaying ugly html. 

While debugging everything seemed OK and the javamail API (invoked via Spring) was invoked with the correct parameters and a non-null subject.

So then I wrote a jUnit test to further isolate the problem and of course the unit test, invoking the same server-side java code as before, worked like a charm: the subject was present and the message body was interpreted as HTML.

I was now faced with a configuration problem because the exact same code was working fine from a unit test but was failing when executing from within Tomcat. After some googling I found the advice to check the classpath for duplicate or conflicting javamail implementations. I use the very handy maven command:


mvn dependency:tree

which shows the full dependency tree of your referenced libraries including implicit references, i.e. a jar required for one of my own dependencies. Then I noticed that axis-2 uses a geronimo-javamail implementation; in addition to the 'standard' javax.mail javamail. Sure enough when I excluded this implicit dependency like so:
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-kernel</artifactId>
            <version>1.4.1</version>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.geronimo.specs</groupId>
                    <artifactId>geronimo-activation_1.1_spec</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.apache.geronimo.specs</groupId>
                    <artifactId>geronimo-javamail_1.4_spec</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

the mail got sent correctly.