Saturday, 15 October 2016

Rest API Mock Response Example via Interceptor in Spring MVC with minimal code changes

We had a requirement to mock our rest api feeding to mobile client to be able to mock the responses, since maximum time there were issues to the integrated systems, which would cause the mobile developers to loose time.

I thought about making the least possible changes to our existing service code to return responses.

  1. Client request will add a mock header attribute to the request true/false.
  2. Create a mock Interceptor which should the maximum work for you. In case its not mock forward to designated controller and execute - in this case we will add  an   attribute to the request session , name it the probably your api method eg :- validateNumber.
  3. In your controller you will need to set another attribute as the responseString (json string of the response ) your planning to set for the mock request. (so basically your controller will check for attribute in request session set in Step 2) if exists you set the response String in session.
  4. Post Handling of request in  interceptor will use the session variable for you request api name and response string and save to local file on a common path.
  5. Case 2 :- client request for mock requests , we pick up the same file as the api name requested and display the content directly from the interceptor itself.


       
// Save file for mock response 
String fileName = (String)request.getSession().getAttribute("service");
String responseString = (String)request.getSession().getAttribute("responseString");
if(fileName!=null && responseString!=null ){
 FileUtils.writeStringToFile(new File("D://"+fileName+".txt"), responseString); 
}

// Retrieve file for mock response 
File file = new File("D://"+request.getRequestURL().substring(34)+".txt");
String string = FileUtils.readFileToString(file);
response.getWriter().write(string);


       
 

Captcha Example for Java

Requirement is to authenticate a  rest service via captha alphanumeric text image to protect against robot site crawlers.

1) Create a alphanumeric string and add it to the application session.
2) Create a service to generate an image and send it as the bytestream.
3) Validate the string and the image text and authorize the user request.


       

import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.font.TextAttribute;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import javax.imageio.ImageIO;
import org.apache.commons.lang.RandomStringUtils;


public class Captcha {

    public static void main(String[] args) throws IOException {

          System.out.println("CAPTCHA START");
          BufferedImage image = new BufferedImage(200, 50,
          BufferedImage.TYPE_INT_RGB);
          Graphics2D graphics2D = image.createGraphics();
          Map map = new HashMap();
          Color c = new Color(0.6662f, 0.4569f, 0.3232f);
          GradientPaint gp = new GradientPaint(30, 30, c, 15, 25,Color.white, true);
          graphics2D.setPaint(gp);
          Font font=new Font("Verdana", Font.CENTER_BASELINE , 26);
          graphics2D.setFont(font);
          graphics2D.drawString( RandomStringUtils.randomAlphanumeric(7),2,20);
          graphics2D.dispose();
          File outputfile = new File("capctha.png");
          ImageIO.write(image, "jpeg", outputfile);
          System.out.println("CAPTCHA END" +outputfile.getAbsolutePath());
    }
}

       
 

AWS Certificate Manager - Import Certificate

How to import a certificate. Use Key Store Explorer to open the cert and export keypair in PEM format  This will save a file cert.pem. Make ...