Showing posts with label URL. Show all posts
Showing posts with label URL. Show all posts

Wednesday, 22 June 2011

A short unique string identifier for shorten URL

How to shorten URLs?
As far as you know, there is a lot of Short URL Redirection Services, such as "bit.ly", which used to convert a long url to some short url.

Its Mechanism seems simple. The sequential key for long URL is enough.
For example, the "Wa0e" is the key of "http://bit.ly/Wa0e"
for "http://www.beachbody.com/product/fitness_programs/p90x.do?code=P90XDOTCOM".
The mod_rewrite module could be used to remove file extension and parameters (e.g., short.php?key=Wa0e)

Key looks like a random string, but I guess it's just a sequential key. Because, that way is simple, and same with total number of random combination in conclusion.

How to generate sequential key? Below is my example code.

INDEX.length is 62. So, (62^4 -1) URLs could stored in the combination of four ciphers.

private static String[] INDEX = new String[] { "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
"k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x",
"y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L",
"M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };

private static String getNextURL(String in) {
if (in == null) {
return INDEX[0];
}

char[] result = new char[in.length()];
boolean rounded = false;

for (int i = in.length() - 1; i > -1; i--) {
String subStr = Character.toString(in.charAt(i));
try {
if (!rounded) {
result[i] = getNext(subStr).charAt(0);
rounded = true;
} else {
result[i] = in.charAt(i);
}
} catch (ArrayIndexOutOfBoundsException e) {
result[i] = INDEX[0].charAt(0);
}
}

return new String(result);
}

private static String getNext(String subStr) {
if (subStr.equals("Z")) {
throw new ArrayIndexOutOfBoundsException();
}

int x = 0;
for (int i = 0; i < INDEX.length; i++) {
if (INDEX[i].equals(subStr))
break;

x++;
}

return INDEX[x + 1];
}




Friday, 8 April 2011

Sending a soap message from soap client in java

Sending a soap message requires a server name, like --
http://localhost:13000/myservicename
13000 is port no.
Now just create a connection and write soap message to it.
We are using following imports:
import java.net.*;
import java.io.*;
Now just write the code:
URL u = new URL(server);
URLConnection uc = u.openConnection();
HttpURLConnection connection = (HttpURLConnection) uc;

connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("SOAPAction", SOAP_ACTION);

OutputStream out = connection.getOutputStream();
Writer wout = new OutputStreamWriter(out);

wout.write("<?xml version='1.0'?>\r\n");
wout.write("<SOAP-ENV:Envelope ");
wout.write("xmlns:SOAP-ENV=");
wout.write(
"'http://schemas.xmlsoap.org/soap/envelope/' "
);
wout.write("xmlns:xsi=");
wout.write(
"'http://www.w3.org/2001/XMLSchema-instance'>\r\n");
wout.write(" <SOAP-ENV:Body>\r\n");
wout.write(" <calculateFibonacci ");
wout.write(
"xmlns='http://namespaces.cafeconleche.org/xmljava/ch3/'\r\n"
);
wout.write(" type='xsi:positiveInteger'>" + input
+ "</calculateFibonacci>\r\n");
wout.write(" </SOAP-ENV:Body>\r\n");
wout.write("</SOAP-ENV:Envelope>\r\n");

wout.flush();
wout.close();

InputStream in = connection.getInputStream();
int c;
while ((c = in.read()) != -1) System.out.write(c);
in.close();

Sunday, 13 March 2011

How to validate URL in Java?

You can use org.apache.commons.validator.routines.UrlValidator class (ver 1.4) or apache.commons.validator.UrlValidator class to validate a given URL. The new UrlValidator is in the routines package and the apache.commons.validator.UrlValidator class will be removed in a future release. Here is an example,
public class ValidateUrlExample{

public static void main(String[] args) {
System.out.println("*** doUserSpecifiedSchemes ***");
doUserSpecifiedSchemes();
System.out.println("\n*** doDefaultSchemes ***");
doDefaultSchemes();
}

public static void doUserSpecifiedSchemes() {

String[] schemes = {"http","https"};
UrlValidator urlValidator = new UrlValidator(schemes);

String url = "http://foo.bar.com/";
if (urlValidator.isValid(url)) {
System.out.println(url + " is valid");
}
else {
System.out.println(url + " is invalid");
}

url = "ftp://foo.bar.com/";
if (urlValidator.isValid(url)) {
System.out.println(url + " is valid");
}
else {
System.out.println(url + " is invalid");
}

}

public static void doDefaultSchemes() {

UrlValidator urlValidator = new UrlValidator();

String url = "http://foo.bar.com/";
if (urlValidator.isValid(url)) {
System.out.println(url + " is valid");
}
else {
System.out.println(url + " is invalid");
}

url = "ftp://foo.bar.com/";
if (urlValidator.isValid(url)) {
System.out.println(url + " is valid");
}
else {
System.out.println(url + " is invalid");
}
}

}

output:
*** doUserSpecifiedSchemes ***
http://foo.bar.com/ is valid
ftp://foo.bar.com/ is invalid

*** doDefaultSchemes ***
http://foo.bar.com/ is valid
ftp://foo.bar.com/ is valid