basic Posts

How to Create A HTTP Request with Proxy Using Apache HTTP Client

In this tutorial, im trying to simuate a http GET request using apache http client, the version im using is 4.5.1

<!-- http client -->
<dependency>
	<groupId>org.apache.httpcomponents</groupId>
	<artifactId>httpclient</artifactId>
	<version>4.5.1</version>
</dependency>
<dependency>
	<groupId>org.apache.httpcomponents</groupId>
	<artifactId>httpmime</artifactId>
	<version>4.5.1</version>
</dependency>

Usually, my application connects well without proxy. But on production environment, I need to put a proxy configuration. So this is my method for making a proxy request connection.

public String process(String lat, String lon) {
	try {
		HttpClient httpclient = HttpClientBuilder.create().build();
		HttpGet httpGet = new HttpGet("<INPUT YOUR URL HERE>");
		
		HttpHost proxy = new HttpHost("127.0.0.1", 3128, "http");
		RequestConfig config = RequestConfig.custom()
				.setProxy(proxy)
				.build();
		httpGet.setConfig(config);
		
		HttpResponse response = httpclient.execute(httpGet);
		HttpEntity resEntity = response.getEntity();

		String responseText = EntityUtils.toString(resEntity);		
		return responseText;
	} catch (IOException e) {
		logger.error(e, e);
	} catch (Exception e) {
		logger.error(e, e);
	}
	return null;
}
Google+

[Java] Create a Simple Http Get Request with GZip Compression Handling using Apache Httpclient

Previous Apache HttpClient use various class to handling compressed http response, such as DecompressingHttpClient and ContentEncodingHttpClient. But on the latest version, compression is handled automatically by using HttpClientBuilder class.

Here is my pom file

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.edw</groupId>
    <artifactId>Edw</artifactId>
    <version>1</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>
    
    <dependencies>                
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.1</version>
        </dependency>        
    </dependencies>
    
</project>

And this is my java class,

package com.edw.main;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

public class Main {

    public static void main(String[] args) throws Exception {
        Main main = new Main();
        System.out.println(main.getContent("http://localhost:8085/"));
    }

    private String getContent(String urlToRead) throws Exception {
        HttpEntity resEntity = null;
        try {
            HttpClient httpclient = HttpClientBuilder.create().build();
            HttpGet httpGet = new HttpGet(urlToRead);

            HttpResponse response = httpclient.execute(httpGet);
            resEntity = response.getEntity();

            return EntityUtils.toString(resEntity);
        } finally {
            if (resEntity != null) {
                EntityUtils.consume(resEntity);
            }
        }
    }
}
Google+

[Weird Javascript Error] Leading Zero on Integer Variable Become Octal on Old Browser

Ive met a very weird error, that somehow never occurs to me on my browser. I found this error only happen on old mozilla browsers, and only happen on javascript’s parseInt method. Here is a simple example,

alert(parseInt("09")-1);

When im running it on my browser (Mozilla version 34), this is what happen,
mozillanew

But when running on old browsers (Mozilla version 3.6), this is what happen,
mozillaold

I finally found out that that on old browsers, leading zeros are treated as octal values instead of plain integers. So this is my workaround,

alert(parseInt("09".replace(/^[0]+/g,""))-1);

I removed the leading zero before parsing it into int.

Hope it can help others :)

Google+

Building A Runnable Maven JavaSE Desktop Application And Including Its Jar Dependencies

Usually it’s very easy to create a runnable jar using Netbean’s ant plugin, but on this project im trying to utilize maven. One painfull thing is somehow maven is unable to build create a runnable jar file which included its jar dependencies.

After several minutes googling, i’ve found a very good solution. Here is the content of my pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.edw</groupId>
    <artifactId>Edw</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>        
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>process-sources</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${targetdirectory}</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>                            
                            <classpathPrefix>lib/</classpathPrefix>                            
                            <mainClass>com.edw.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/assembly/customAssembly.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> 
                        <phase>package</phase> 
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

And dont forget, adding an xml file, customAssembly.xml which located on src/main/assembly/ folder

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>bin</id>
    <formats>
        <format>zip</format>
    </formats>
    <fileSets>
        <fileSet>
            <directory>${project.build.directory}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>*.jar</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}/dependency</directory>
            <outputDirectory>lib</outputDirectory>
        </fileSet>
    </fileSets>
</assembly>

Just build your application from Netbeans, and you will find a zip file on your Netbeans’ target folder, which consist of a runnable jar file and a lib folder containing your jar dependencies. ;-)

Google+

Regular Expression to Check Indonesian MobilePhone Provider

These are simple regex to check Indonesia’s Mobile phone provider based on its number,

telkomsel.regex = ^(\\+62|\\+0|0|62)8(1[123]|52|53|21|22|23)[0-9]{5,9}$

simpati.regex = ^(\\+62|\\+0|0|62)8(1[123]|2[12])[0-9]{5,9}$

as.regex = ^(\\+62|\\+0|0|62)8(52|53|23)[0-9]{5,9}$

indosat.regex= ^(\\+62815|0815|62815|\\+0815|\\+62816|0816|62816|\\+0816|\\+62858|0858|62858|\\+0814|\\+62814|0814|62814|\\+0814)[0-9]{5,9}$

im3.regex = ^(\\+62855|0855|62855|\\+0855|\\+62856|0856|62856|\\+0856|\\+62857|0857|62857|\\+0857)[0-9]{5,9}$

xl.regex = ^(\\+62817|0817|62817|\\+0817|\\+62818|0818|62818|\\+0818|\\+62819|0819|62819|\\+0819|\\+62859|0859|62859|\\+0859|\\+0878|\\+62878|0878|62878|\\+0877|\\+62877|0877|62877)[0-9]{5,9}$

Hope it can help others, ;-)

Google+