/*
 * Copyright 2012 Wyona
 */
package org.wyona.yanel.impl.resources.cas;

import org.wyona.yanel.impl.resources.BasicXMLResource;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.apache.log4j.Logger;

/**
 * A simple Resource which extends BasicXMLResource
 */
public class CASProxyCallbackResource extends BasicXMLResource {
    
    private static Logger log = Logger.getLogger(CASProxyCallbackResource.class);
    
    /**
     * This method overrides the method to create the InputStream called by BasicXMLResource
     * Since you extend the BasicXMLResource this has to contain well-formed xml.
     * Should return a InputStream which contains XML. 
     * Use StringBuilder, dom, jdom, org.apache.commons.io.IOUtils and so on to generate the XML.
     *
     * @see org.wyona.yanel.impl.resources.BasicXMLResource#getContentXML(String)
     */
    @Override
    protected InputStream getContentXML(String viewId) throws Exception {
        if (log.isDebugEnabled()) {
            log.debug("requested viewId: " + viewId);
        }

        String pgtId = getEnvironment().getRequest().getParameter("pgtId");
        if (pgtId != null) {
            log.warn("DEBUG: pgt Id: " + pgtId);
            java.io.FileOutputStream out = new java.io.FileOutputStream("/Users/michaelwechner/pgt-id.txt");
            out.write(pgtId.getBytes());
            out.close();
        } else {
            log.warn("No pgt Id!");
        }

        StringBuilder sb = new StringBuilder("<?xml version=\"1.0\"?>");
        sb.append("<root/>");

        return new ByteArrayInputStream(sb.toString().getBytes());
    }
}

@@ -1144,86 +1144,671 @@
 d%22);
-%0A        if (pgtId != null) %7B%0A            log.warn(%22DEBUG: pgt Id: %22 + pgtId);
+ // INFO: http://www.jusfortechies.com/java/cas/protocol.php#tgt%0A        String pgtIou = getEnvironment().getRequest().getParameter(%22pgtIou%22); // INFO: http://www.jusfortechies.com/java/cas/protocol.php#pgt-iou%0A        if (pgtId != null && pgtIou != null) %7B%0A            log.warn(%22DEBUG: pgt Id: %22 + pgtId + %22, pgt Iou: %22 + pgtIou);%0A            // TODO: Make it synchronized/multi-threaded. Also add the parameter 'pgtIou'%0A            java.io.File proxyIdFile = new java.io.File(System.getProperty(%22java.io.tmpdir%22), %22cas-pgt-id.txt%22); // INFO: The class src/webapp/src/java/org/wyona/yanel/servlet/security/impl/CASWebAuthenticatorImpl.java will read it from here
 %0A   
@@ -1880,42 +1880,19 @@
 eam(
-%22/Users/michaelwechner/pgt-id.txt%22
+proxyIdFile
 );%0A 

write pgt Id to temporary directory