Rendering Complex Scripts (Indic or Brahmic, Devangiri, Bi-Directional, Arabic, Thai, Khmer and etc)

There are so many languages in the world that need special attention while drawing or rendering those scripts. Java has provided an excellent feature to deal with this complexity. By using java.awt.font.TextLayout class, we can easily render these scripts elegantly.

Continue reading “Rendering Complex Scripts (Indic or Brahmic, Devangiri, Bi-Directional, Arabic, Thai, Khmer and etc)” »

Bookmark and Share
Leave a comment

WordPress blog issue: Recovering the blog posts after updating wordpress plugins

Recently, I have faced an issue with my blogging site, after applying few WordPress plugin updates. The problem was that, the posts are not being displayed either on the home page or on the individual post link (permalink).

I just wanted to share how did I resolve this issue, so that some one who is facing the similar problem might be able to solve with this tip.

I just changed my permalinks format from the one I am using to the custom one and again reverted the format back to the earlier one. This is pretty simple change, It has resolved the issue. You can update permalink format via Settings > Permalinks option.

I did this because, I have read somewhere that after applying some plugins, the blog posts permalinks might get corrupted. That is the reason I tried this to reset those permalinks.

That’s it. Please share if you have had similar experiences.

Bookmark and Share
Leave a comment

Working with JAX-WS Web Services: Brief History

In the previous 2 articles, we learned how to build a simple JAX-WS web service and a client to interact with it without any introduction to web services concept and the technologies or protocols involved. This article is targeted to those who wants to know the basic concepts and technologies involved in building the web services. If you are already aware of how web services are evolved and the technologies involved, I suggest you to skip this, and wait for the next article on this series to know more about JAX-WS services.

Continue reading “Working with JAX-WS Web Services: Brief History” »

Bookmark and Share
Leave a comment

File upload with Apache HttpClient Library

File Upload or Attachments are common in most of the applications. In this tip, I would like to show how to do file upload using Apache HttpClient version 4.1.3. You can download it at http://hc.apache.org/downloads.cgi

With normal requests, we send multiple parameters to the server by setting a request entity(usually URLEncodedFormEntity) to the http reqeust. For file upload or attachments we need to set a multi-part request entity to the http request. With this MultipartEntity, we would be able to send the usual form parameters and as well as the file content. The following code snippet shows how to do this.

 

package com.acc.blogs.httpclient;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;

public class SampleFileUpload {

	/**
	 * A generic method to execute any type of Http Request and constructs a response object
	 * @param requestBase the request that needs to be exeuted
	 * @return server response as <code>String</code>
	 */
	private static String executeRequest(HttpRequestBase requestBase){
		String responseString = "" ;

		InputStream responseStream = null ;
		HttpClient client = new DefaultHttpClient () ;
		try{
			HttpResponse response = client.execute(requestBase) ;
			if (response != null){
				HttpEntity responseEntity = response.getEntity() ;

				if (responseEntity != null){
					responseStream = responseEntity.getContent() ;
					if (responseStream != null){
						BufferedReader br = new BufferedReader (new InputStreamReader (responseStream)) ;
						String responseLine = br.readLine() ;
						String tempResponseString = "" ;
						while (responseLine != null){
							tempResponseString = tempResponseString + responseLine + System.getProperty("line.separator") ;
							responseLine = br.readLine() ;
						}
						br.close() ;
						if (tempResponseString.length() > 0){
							responseString = tempResponseString ;
						}
					}
				}
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if (responseStream != null){
				try {
					responseStream.close() ;
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		client.getConnectionManager().shutdown() ;

		return responseString ;
	}

	/**
	 * Method that builds the multi-part form data request
	 * @param urlString the urlString to which the file needs to be uploaded
	 * @param file the actual file instance that needs to be uploaded
	 * @param fileName name of the file, just to show how to add the usual form parameters
	 * @param fileDescription some description for the file, just to show how to add the usual form parameters
	 * @return server response as <code>String</code>
	 */
	public String executeMultiPartRequest(String urlString, File file, String fileName, String fileDescription) {

		HttpPost postRequest = new HttpPost (urlString) ;
		try{

			MultipartEntity multiPartEntity = new MultipartEntity () ;

			//The usual form parameters can be added this way
			multiPartEntity.addPart("fileDescription", new StringBody(fileDescription != null ? fileDescription : "")) ;
			multiPartEntity.addPart("fileName", new StringBody(fileName != null ? fileName : file.getName())) ;

			/*Need to construct a FileBody with the file that needs to be attached and specify the mime type of the file. Add the fileBody to the request as an another part.
			This part will be considered as file part and the rest of them as usual form-data parts*/
			FileBody fileBody = new FileBody(file, "application/octect-stream") ;
			multiPartEntity.addPart("attachment", fileBody) ;

			postRequest.setEntity(multiPartEntity) ;
		}catch (UnsupportedEncodingException ex){
			ex.printStackTrace() ;
		}

		return executeRequest (postRequest) ;
	}

	public static void main(String args[]){
		SampleFileUpload fileUpload = new SampleFileUpload () ;
		File file = new File ("Hydrangeas.jpg") ;

		String response = fileUpload.executeMultiPartRequest("<Request URL>", file, file.getName(), "File Upload test Hydrangeas.jpg description") ;
		System.out.println("Response : "+response);
	}	

}

Make sure you have added all the required HttpClient libraries to the classpath before executing this snippet. Also, change the request URI to point to your server URL.

Hope this would save some of your quality time.

Bookmark and Share
Leave a comment

Working with JAX-WS Web Services: Building a Web Service Client

In the previous article, we have learned how to create a simple web service. In this article, we discuss how to access this web service and use it.

To access the web service, we need to know the service artifacts and it’s location.

The service artifacts are described in a document called WSDL(Web Service Description Language) file. The WSDL file contains information pertaining to the service, such as the types, operations of the service, binding and port type information. More details on this and other technologies involved will be discussed in next article. But, some details on WSDL are discussed below.

Continue reading “Working with JAX-WS Web Services: Building a Web Service Client” »

Bookmark and Share
Leave a comment