Posts

Validation in Java

Validations: 1. If word contains consecutive characters String userWord = "Pankaj"; Pattern pattern = Pattern.compile("(\\w)\\1{2,}"); Matcher matcher = pattern.matcher(userWord); if (matcher.find()) { System.out.println("Found value is: " + matcher.group(0)); return true; } else { System.out.println("NO MATCH"); return false; } 2. If word contains more instance of characters String userWord = "Pankaj"; Pattern pattern = Pattern.compile("(\\w)(\\w*\\1){4}"); Matcher matcher = pattern.matcher(userWord); if (matcher.find()) { System.out.println("Found value is: " + matcher.group(0)); return true; } else { System.out.println("NO MATCH"); return false; } 3. If word contains any name String firstName = "Pankaj"; String emailAddress = "pankaj.lilhare@gmail.com"; // Here I want to check first na...

Pass complex object into RESTful service

Suppose, I have Employee.java class. public class Employee {      private String firstName;      private String lastName;      private String city;     // getter     // setter     // override toString() to print the variables } I want to set values to Employee class through REST service: @POST @Path("/saveemployee") @Consumes(MediaType.APPLICATION_JSON) public Response saveEmployee(Employee employee){     Response response  = null;     System.out.println(employee.toString());     // further procees to store into db     return response; } Now, I want to call this rest service inside my java class: import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.RequestEntity; import org.apache.commons.httpclient.method...

How to call secured RESTful service in java?

import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.apache.commons.httpclient.auth.AuthScope; import org.apache.commons.httpclient.methods.GetMethod; ... ... String url = "pankaj-lilhare.blogspot";    /* http://pankaj-lilhare.blogspot:8080/test */ int portNumber = 8080;    /* depends on deployed service port   */ String username = "test"; String password = "test123"; HttpClient client = null; try {     client = new HttpClient();     client.getState().setCredentials(     new AuthScope( url , portNumber ,AuthScope.ANY_REALM),     new UsernamePasswordCredentials( username , password ));     GetMethod method = new GetMethod(restUrl);     int returnCode = client.executeMethod(method);     if ( returnCode>= 200) {         Stri...