// Copyright (c) 2002 Oracle Corporation

package oracle.portal.sample.v2.devguide.esi;

import java.util.ResourceBundle;
import javax.servlet.http.HttpServletRequest;
import oracle.portal.provider.v2.PortletDefinition;
import oracle.portal.provider.v2.personalize.PortletReference;
import oracle.portal.provider.v2.personalize.NameValuePersonalizationObject;
import oracle.portal.sample.v2.devguide.esi.resource.ESIBundle;
import oracle.webdb.utils.SimpleStringBuffer;

/**
 * A class that implements the sample ESI portlet's customization storage.
 */
public class ESICustomization extends NameValuePersonalizationObject
{
    // Preference store Keys for the invalidation details.
    public static final String INVHOST_KEY          = "INVHOST";
    public static final String INVPREFIX_KEY        = "INVPREFIX";
    public static final String INVFILE_KEY          = "INVFILE";
    public static final String DEFAULTFILENAME_KEY  = "DEFFILENAME";
    public static final String DEFAULTPREFIX_KEY    = "DEFPREFIX";
    
    // no translateable strings
    public static final String DEFAULTFILENAME  = "fragment.jsp";
    public static final String DEFAULTPREFIX    = "/htdocs/esi/";

    /**
     * Sample event portlet customization constructor.
     */
    public ESICustomization()
    {
    }

    /**
     * Set initial values for customizable values.
     */
    public void init(PortletReference ref)
    {
      super.init(ref);

      PortletDefinition portletDef = ref.getPortletDefinition();

      putString(DEFAULTFILENAME_KEY, DEFAULTFILENAME);
      putString(DEFAULTPREFIX_KEY, DEFAULTPREFIX);
    }

    /**
     * Returns the host of the fragment included using ESI.
     *
     * <p> Example: if the included fragment has the URL 
     *     <pre>http://myhost.oracle.com:1234/htdocs/esi/fragment.jsp</pre>
     * then the host would be
     *     <pre>myhost.oracle.com:1234</pre>
     *
     * @param  request the HTTP servlet request
     * @return the host of the fragment included using ESI
     */
    public String getInvalidationHost(HttpServletRequest request)
    {
      String invalidationHost = getString(INVHOST_KEY);
      if (invalidationHost != null && invalidationHost.length() > 0)
      {
        return invalidationHost;
      }

      SimpleStringBuffer invalidationHostBuffer = new SimpleStringBuffer(200);
      invalidationHostBuffer.append(request.getServerName())
                      .append(":")
                      .append(request.getServerPort());
      return invalidationHostBuffer.toString();
    }

    /**
     * Sets the host of the fragment included using ESI.
     *
     * <p> Example: if the included fragment has the URL 
     *     <pre>http://myhost.oracle.com:1234/htdocs/esi/fragment.jsp</pre>
     * then the host would be
     *     <pre>myhost.oracle.com:1234</pre>
     *
     * @param  invalidationHost host of the fragment included using ESI.
     */
    public void setInvalidationHost(String invalidationHost)
    {
      putString(INVHOST_KEY, invalidationHost);
    }

    /**
     * Returns the prefix of the fragment included using ESI.
     *
     * <p> Example: if the included fragment has the URL 
     *     <pre>http://myhost.oracle.com:1234/htdocs/esi/fragment.jsp</pre>
     * then the host would be
     *     <pre>/htdocs/esi/</pre>
     *
     * @param  request the HTTP servlet request
     * @return the prefix of the fragment included using ESI
     */
    public String getInvalidationPrefix(HttpServletRequest request)
    {
      String invalidationPrefix = getString(INVPREFIX_KEY);
      if (invalidationPrefix != null && invalidationPrefix.length() > 0)
      {
        return invalidationPrefix;
      }

      SimpleStringBuffer invalidationPrefixBuffer = new SimpleStringBuffer(200);
      invalidationPrefixBuffer.append(request.getContextPath())
                              .append(getString(DEFAULTPREFIX_KEY));
      return invalidationPrefixBuffer.toString();
    }

    /**
     * Sets the prefix of the fragment included using ESI.
     *
     * <p> Example: if the included fragment has the URL 
     *     <pre>http://myhost.oracle.com:1234/htdocs/esi/fragment.jsp</pre>
     * then the host would be
     *     <pre>/htdocs/esi/</pre>
     *
     * @param  invalidationPrefix prefix of the fragment included using ESI.
     */
    public void setInvalidationPrefix(String invalidationPrefix)
    {
      putString(INVPREFIX_KEY, invalidationPrefix);
    }

    /**
     * Returns the filename of the fragment included using ESI.
     *
     * <p> Example: if the included fragment has the URL 
     *     <pre>http://myhost.oracle.com:1234/htdocs/esi/fragment.jsp</pre>
     * then the host would be
     *     <pre>fragment.jsp</pre>
     *
     * @param  request the HTTP servlet request
     * @return the filename of the fragment included using ESI
     */
    public String getInvalidationFile(HttpServletRequest request)
    {
      String invalidationFile = getString(INVFILE_KEY);
      if (invalidationFile == null)
      {
        invalidationFile = getString(DEFAULTFILENAME_KEY);
      }
      return invalidationFile;
    }

    /**
     * Sets the filename of the fragment included using ESI.
     *
     * <p> Example: if the included fragment has the URL 
     *     <pre>http://myhost.oracle.com:1234/htdocs/esi/fragment.jsp</pre>
     * then the host would be
     *     <pre>fragment.jsp</pre>
     *
     * @param  invalidationFile filename of the fragment included using ESI.
     */
    public void setInvalidationFile(String invalidationFile)
    {
      putString(INVFILE_KEY, invalidationFile);
    }

    /**
     * Returns the fragment's URL using the customized host, 
     * prefix, and filename.
     *
     * @param  request the HTTP servlet request
     * @return the fragment's URL
     */
    public String getFragmentURL(HttpServletRequest request)
    {
      String host       = getInvalidationHost(request);
      String prefix     = getInvalidationPrefix(request);
      String file       = getInvalidationFile(request);

      SimpleStringBuffer fragmentUrl = new SimpleStringBuffer("http://");
      fragmentUrl.append(host)
                 .append(prefix)
                 .append(file);

      return fragmentUrl.toString();
    }
}

