mcardle     2005/08/19 10:45:31 CEST

  Added files:
    core/src/java/org/jahia/operations/valves 
                                              EsiInvalidateValve.java 
  Log:
  ESI update
  
  Revision  Changes    Path
  1.1       +166 -0    
jahia/core/src/java/org/jahia/operations/valves/EsiInvalidateValve.java (new)
http://jahia.mine.nu:8080/cgi-bin/cvsweb.cgi/jahia/core/src/java/org/jahia/operations/valves/EsiInvalidateValve.java?rev=1.1&content-type=text/plain
  
  
  
  Index: EsiInvalidateValve.java
  ====================================================================
  package org.jahia.operations.valves;
  
  import org.jahia.pipelines.*;
  import org.jahia.pipelines.valves.*;
  import org.jahia.params.ParamBean;
  import org.jahia.params.ProcessingContext;
  import org.jahia.settings.SettingsBean;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.HttpServletRequest;
  import java.util.Vector;
  import java.util.Iterator;
  import javax.servlet.jsp.JspWriter;
  import java.io.PrintWriter;
  import org.jahia.registries.ServicesRegistry;
  import org.jahia.services.esi.EsiService.*;
  import java.util.HashMap;
  import org.jahia.services.esi.Template;
  import org.jahia.services.esi.Fragment;
  //import javax.servlet.jsp.JspWriter;
  
  import java.rmi.RemoteException;
  import javax.xml.rpc.ServiceException;
  import javax.xml.soap.Name;
  import javax.xml.soap.SOAPBody;
  import javax.xml.soap.SOAPBodyElement;
  import javax.xml.soap.SOAPException;
  
  import org.apache.axis.client.Call;
  import org.apache.axis.client.Service;
  import org.apache.axis.message.SOAPEnvelope;
  import org.jahia.services.applications.ServletIncludeResponseWrapper;
  import java.io.OutputStreamWriter;
  import javax.servlet.ServletOutputStream;
  import org.jahia.exceptions.JahiaException;
  
  
  import org.jahia.services.esi.EsiInvalidation;
  
  /**
   * <p>Title: </p>
   *
   * <p>Description: Carries out ESI cache invalidation </p>
   *
   * <p>Copyright: Jahia Copyright (c) 2005</p>
   *
   * <p>Company: Jahia Ltd</p>
   *
   * @author MC
   * @version 1.0
   */
  public class EsiInvalidateValve implements Valve {
  
      private static org.apache.log4j.Logger logger =
              org.apache.log4j.Logger.getLogger(EsiInvalidateValve.class);
  
      public EsiInvalidateValve () {
          //logger.debug("EsiInvalidateValve()");
      }
  
      /**
       * initialize
       *
       */
      public void initialize () {
          logger.debug("  EsiInvalidateValve.initialize() ********************* 
");
      }
  
      /**
       * invoke Carries out ESI invalidation.
       * Each Content object that is referenced in a Fragment/Template
       * and which is marked as invalid by @see EsiInvalidationEventListener 
(e.g. it's been updated or deleted)
       * will generate the appropriate ESI XML invalidation message. These 
messages are aggregated
       * together into one ESI invalidation message, which is then appended to 
the current
       * page response (i.e. invalidation piggy-backing). The ESI server will 
parse the invalidation message, execute it, remove it and then
       * server the page to the client.
       *
       * It is also possible to use SOAP invalidation so that each invalidation 
connects to the ESI server invalidation socket
       * via the SOAP XML protocol.
       *
       * @param context Object
       * @param valveContext ValveContext
       * @throws PipelineException
       */
      public void invoke (Object context, ValveContext valveContext)
              throws PipelineException {
  
          ParamBean paramBean = (ParamBean) context;  //using parambean since 
we need the getrealresponse() method, not present in porcessingContext
  
          StringBuffer strFinal = null;
  
  
          if (paramBean.settings().lookupBoolean(SettingsBean.
                  ESI_CACHE_ACTIVATED)) {
  
              //Get ESI invalidation message for all objects to invalidate
              //invMessage = 
EsiInvalidation.getPendingObjectsToInvalidate(paramBean);
              Object[] objReturned = 
EsiInvalidation.getPendingObjectsToInvalidate(paramBean);
              String invMessage = (String) objReturned[0];
              HashMap TempsToInvalidate =  (HashMap)objReturned[1];
              HashMap FragsToInvalidate =  (HashMap)objReturned[2];
  
  
              //TODO: might wanna check if the current contenttype is of 
"charset=" type
              //since we don't want to add ESI commands to pics which will be 
ignored by ESI server
              if (invMessage != null) {
                  strFinal = new StringBuffer();
                  strFinal.append("\n <!-- START of ESI invalidation message 
--> \n");
                  strFinal.append("<pre>      \n");
                  //TODO: REMOVE output=yes param. Only for debug purposes
                  strFinal.append("<esi:invalidate output=\"yes\">      \n");
                  //str.append("<esi:invalidate>      \n");
                  
strFinal.append(EsiInvalidation.addSOAPXMLWrapper(invMessage.toString()));
                  strFinal.append("</esi:invalidate>      \n");
                  strFinal.append("</pre>      \n");
                  strFinal.append("<!-- END of ESI invalidation message -->");
  
                  logger.debug("PiggyBack Invalidation Message : \n" + 
strFinal.toString() );
  
                  //Add invalidation message as piggy-back to current HTML 
response
                  try {
                      HttpServletResponse realResp = 
paramBean.getRealResponse();
                      ServletOutputStream outputStream = 
realResp.getOutputStream();
                      OutputStreamWriter streamWriter = new OutputStreamWriter(
                              outputStream);
                      streamWriter.write(strFinal.toString()
                              , 0, strFinal.length());
                      logger.debug("ESI Invalidation Piggy-back Message: \n" +
                              strFinal);
                      //out.print(strFinal.toString());
                      streamWriter.flush();
                  } catch (java.io.IOException ioe) {
                      logger.error(
                              "Error writing cache output, IOException 
generated error",
                              ioe);
                      JahiaException outputException = new
                              JahiaException(
                              "OperationsManager.handleOperations",
                              "Error writing ESI content to writer",
                              JahiaException.SECURITY_ERROR,
                              JahiaException.ERROR_SEVERITY,
                              ioe);
                      throw new PipelineException(outputException);
                  }
  
                  //TODO: Really should get confirmation from SOAP that objects 
have actually been invalidated
                  //TODO: before doing any of the operations below:
  
                  //clear any contentIDs that caused an ESI Fragment/Template 
invalidation
                  //DON'T GET RID OF THEM ALL, only those that have actually 
been invalidated
                  
ServicesRegistry.getInstance().getEsiService().clearContentToInvalidate();
  
                  //clear the contentIDs contained in any Fragments/Templates 
that were invalidated
                  //so that AOP will redetect all the contentIDs of these any 
Fragments/Templates
                  // during the execution of the
                  //the JSP page
                  
ServicesRegistry.getInstance().getEsiService().emptyInvalidatedTemplates(TempsToInvalidate);
                  
ServicesRegistry.getInstance().getEsiService().emptyInvalidatedFragments(FragsToInvalidate);
  
  
              }
          }
          valveContext.invokeNext(context);
      }
  
  } //end EsiInvalidateValve
  
  

Reply via email to