package com.fsenablers.saml; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.FileReader; import java.io.PrintWriter; import java.io.StringWriter; import java.util.zip.Deflater; import java.util.zip.DeflaterOutputStream; import java.util.zip.Inflater; import java.util.zip.InflaterInputStream; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public class SAMLUtilities { public static String base64Inflate( String src ) throws Exception { BASE64Decoder base64Decoder = new BASE64Decoder(); byte[] decodeBytes = base64Decoder.decodeBuffer( src ); ByteArrayInputStream bais = new ByteArrayInputStream( decodeBytes ); InflaterInputStream iis = new InflaterInputStream( bais, new Inflater(true) ); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int b = iis.read(); while( b != -1 ) { baos.write( b ); b = iis.read(); } iis.close(); baos.close(); String s = baos.toString(); return s; } public static String base64Deflate( String src ) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DeflaterOutputStream dos = new DeflaterOutputStream( baos, new Deflater( Deflater.BEST_COMPRESSION, true ) ); dos.write( src.getBytes()); dos.close(); byte compressedBytes[] = baos.toByteArray(); BASE64Encoder base64Encoder = new BASE64Encoder(); String base64EncodedString = base64Encoder.encode( compressedBytes ); return base64EncodedString; } /** * @param args */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub BufferedReader reader = new BufferedReader( new FileReader( "authnrequest.xml" )); String line = reader.readLine(); StringBuffer buffer = new StringBuffer(); StringWriter writer = new StringWriter(); PrintWriter out = new PrintWriter( writer ); while( line != null ) { out.println( line ); line = reader.readLine(); } String s = writer.toString(); System.out.println( s ); String s2 = base64Deflate( s ); System.out.println( s2 ); String s3 = base64Inflate( s2 ); System.out.println( s3 ); reader = new BufferedReader( new FileReader( "authnrequest.b64" )); line = reader.readLine(); String s4 = base64Inflate( line ); System.out.println( line ); System.out.println( s4 ); } }