Java tutorial
import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URLDecoder; import java.util.Properties; import java.util.Scanner; import javax.servlet.ServletContext; import org.apache.commons.codec.binary.Base64; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author sankalpkulshrestha,reevalohia,nikhil */ public class HackathonSupporter implements InputInterface { @Override /** * Returns the banner after searching in the file. * @param advId The forced advertisement id you get from GET request * @param width the requested width * @param height the request height * @param segmentId the segment ID you get by reading the cookie * @context object of ServletContext which is needed to read the config.properties file */ public String readFromFile(String advId, int width, int height, int segmentId, ServletContext context, int callingFlag) { File file = null; Scanner fis = null; String banner = null; try { //read the filename and mappingFilename form the config.properties file Properties prop = new Properties(); if (callingFlag == 0) { prop.load(new InputStreamReader(context.getResourceAsStream("/WEB-INF/config.properties"))); } else if (callingFlag == 1) { prop.load(new FileReader( "/home/sankalpkulshrestha/NetBeansProjects/AdServer/web/WEB-INF/config.properties")); } else { return DEFAULT_BANNER; } //filename contains the list of advId, width, height, banner,segmentID. The filename is input.txt String filename = prop.getProperty("filename"); //mappingFilename contains the mapping of the advId and the default banner address String mappingFilename = prop.getProperty("mappingFilename"); file = new File(filename); fis = new Scanner(file); String line = null; //read the each line of input.txt, split it by comma and store it in param String array String[] param = new String[5]; //w and h hold the width and height respectively. //flag keeps track of whether a corresponding advId is found for a segnment ID, in case the advId is null int w = -1, h = -1, flag = 0; while (fis.hasNextLine()) { //read each line and split by comma line = fis.nextLine(); param = line.split(","); //read the width and height from the input.txt w = Integer.parseInt(param[1]); h = Integer.parseInt(param[2]); //in case we are not getting and forced advertisement ID, we keep searching for the corresponding advId is found for a segnment ID if ((advId == null || advId.length() == 0) && flag == 0 && segmentId == Integer.parseInt(param[4])) { flag = 1; advId = param[0]; } //in case segment ID is not 0 and segmentId is same as the segment ID found from the file of same width //and height as the requested width and height, we set the corresponding banner from the file and break away if (segmentId != 0 && segmentId == Integer.parseInt(param[4]) && w == width && h == height) { banner = param[3]; break; } } //close the input.txt file if (fis != null) { fis.close(); } //if till now the banner is still null and the advId is not null //then we check the mapping.txt file for finding the default banner of the campaign //the advId points to if (banner == null && advId != null) { File file2 = new File(mappingFilename); Scanner fis2 = null; fis2 = new Scanner(file2); param = new String[2]; while (fis2.hasNextLine()) { line = fis2.nextLine(); param = line.split(","); if (param[0].equals(advId)) { banner = param[1]; break; } } //close the mapping.txt file if (fis2 != null) { fis2.close(); } } else if (banner == null && advId == null) { //in case the banner is null and the advId is null, we return the default banner return DEFAULT_BANNER; } } catch (IOException e) { //in case of any exception, we return the default banner return DEFAULT_BANNER; } finally { //close the file if (fis != null) { fis.close(); } } return banner; }//To change body of generated methods, choose Tools | Templates. @Override /** * Return the banner after reading from database. This function has not been implemented yet * and just return the default banner right now */ public String readFromDB(String advId, int width, int height, int segmentId) { return DEFAULT_BANNER; } /** * Returns the response to be echoed out * @param banner * @param width * @param height * @return */ public String responseBuilder(String banner, int width, int height) { //Build the response that is to be echod out String response = "document.write(\"<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0' width='300' height='250' id='passing'>"; response += "<embed src='" + banner + "' FlashVars='clickTAG=http://www.vizury.com/vizserver//www/delivery/ck.php?oaparams=2__bannerid=-1__zoneid=0__vzimid=538c2627a1d375193557281NR__expid=e1_m__expidbid=__expiddev=:1__mc=__devID=__sfpc=-__maxdest=http://www.junglee.com/?tag=vizurycom-21&gCity=bangalore&gCountry=india&configPath=&cdnDefImg=http://cdn3.vizury.com' quality='high' width='" + width + "' height='" + height + "' wmode='opaque' name='passing' allowScriptAccess='always' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer'></embed>"; response += "</object>\")"; System.out.println(response); return response; } String decrypt(String encryptedCookie) { try { if (encryptedCookie == null || encryptedCookie.trim().equals("")) { return ""; } String key = "SOMEKEY"; //PHP cookie setting has some % encoding in them and hence this byte[] bDecodedCookie = Base64.decodeBase64(URLDecoder.decode(encryptedCookie, "UTF-8").getBytes()); ByteArrayOutputStream resBytes = new ByteArrayOutputStream(); for (int i = 0; i < bDecodedCookie.length; i++) { char ch = (char) bDecodedCookie[i]; int keyIndex = (i % key.length()) - 1; if (keyIndex < 0) { keyIndex = keyIndex + key.length(); } char keyChar = key.charAt(keyIndex); resBytes.write(ch - keyChar); } String result = resBytes.toString("UTF-8"); if (result.length() >= 5 && result.substring(0, 5).equals("CODE1")) { result = result.substring(5); System.out.println("--------DECRYPT-------" + encryptedCookie + " -->" + result); return result; } else { System.out.println("ERROR!!! Cookie Encryption is not ours\t " + encryptedCookie); return ""; } } catch (Exception e) { System.out.println("Exception while processing cookie\t " + encryptedCookie + "\t" + e); return ""; } } public boolean validate(String parameter) { try { if (parameter == null || parameter.length() == 0) { return false; } int number = Integer.parseInt(parameter); if (number <= 0) { return false; } } catch (Exception e) { return false; } // only got here if we didn't return false return true; } }