/**
 * Copyright (C) 2009 - present by OpenGamma Inc. and the OpenGamma group of companies
 *
 * Please see distribution for license.
 */
package com.opengamma.web.bundle;

import javax.servlet.ServletContext;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.UriInfo;

import org.joda.beans.impl.flexi.FlexiBean;

import com.opengamma.util.ArgumentChecker;
import com.opengamma.web.AbstractPerRequestWebResource;
import com.opengamma.web.sass.WebJRubySassCompiler;

/**
 * Abstract base class for RESTful bundle resources.
 */
public abstract class AbstractWebBundleResource extends AbstractPerRequestWebResource {

  /**
   * The backing bean.
   */
  private final WebBundlesData _data;

  /**
   * Creates the resource.
   * 
   * @param bundleManagerFactory  the bundle manager, not null
   * @param compressor  the bundle compressor, not null
   * @param mode  the deploy mode, not null
   */
  protected AbstractWebBundleResource(
      final BundleManagerFactory bundleManagerFactory, final BundleCompressor compressor, final DeployMode mode) {
    ArgumentChecker.notNull(bundleManagerFactory, "bundleManagerFactory");
    ArgumentChecker.notNull(compressor, "compressedBundleSource");
    ArgumentChecker.notNull(mode, "mode");
    _data = new WebBundlesData();
    
    data().setBundleManagerFactory(bundleManagerFactory);
    data().setCompressor(compressor);
    data().setMode(mode);
  }

  /**
   * Creates the resource.
   * 
   * @param parent  the parent resource, not null
   */
  protected AbstractWebBundleResource(final AbstractWebBundleResource parent) {
    super(parent);
    _data = parent.data();
  }

  //-------------------------------------------------------------------------
  /**
   * Setter used to inject the URIInfo.
   * This is a roundabout approach, because Spring and JSR-311 injection clash.
   * DO NOT CALL THIS METHOD DIRECTLY.
   * @param uriInfo  the URI info, not null
   */
  @Context
  public void setUriInfo(final UriInfo uriInfo) {
    data().setUriInfo(uriInfo);
  }

  @Override
  @Context
  public void setServletContext(ServletContext servletContext) {
    super.setServletContext(servletContext);
    
    // initialise the manager now that we have the servlet context
    BundleManager bundleManager = _data.getBundleManagerFactory().get(servletContext);
    data().setBundleManager(bundleManager);
    data().setDevBundleManager(new DevBundleBuilder(bundleManager).getDevBundleManager());
    data().setSassCompiler(WebJRubySassCompiler.of(servletContext));
  }
  
  @Context
  public void setHttpHeaders(HttpHeaders httpHeaders) {
    data().setHttpHeaders(httpHeaders);
  }

  //-------------------------------------------------------------------------
  /**
   * Creates the output root data.
   * 
   * @return the output root data, not null
   */
  protected FlexiBean createRootData() {
    FlexiBean out = getFreemarker().createRootData();
    out.put("ogStyle", new StyleTag(data()));
    out.put("ogScript", new ScriptTag(data()));
    out.put("httpHeaders", data().getHttpHeaders());
    return out;
  }

  //-------------------------------------------------------------------------
  /**
   * Gets the backing bean.
   * 
   * @return the backing bean, not null
   */
  protected WebBundlesData data() {
    return _data;
  }

}

@@ -166,16 +166,40 @@
 undle;%0A%0A
+import java.util.List;%0A%0A
 import j
@@ -346,41 +346,129 @@
 org.
-joda.beans.impl.flexi.FlexiBean;%0A
+apache.commons.lang.StringUtils;%0Aimport org.joda.beans.impl.flexi.FlexiBean;%0A%0Aimport com.google.common.collect.Iterables;
 %0Aimp
@@ -3157,54 +3157,417 @@
     
-out.put(%22httpHeaders%22, data().getHttpHeaders()
+HttpHeaders httpHeaders = data().getHttpHeaders();%0A    String openfin = StringUtils.EMPTY;%0A    if (httpHeaders != null) %7B%0A      out.put(%22httpHeaders%22, data().getHttpHeaders());%0A      List%3CString%3E openfinHeader = httpHeaders.getRequestHeader(%22x-powered-by%22);%0A      if (openfinHeader != null) %7B%0A        openfin = Iterables.getFirst(openfinHeader, StringUtils.EMPTY);%0A      %7D%0A    %7D%0A    out.put(%22openfin%22, openfin
 );%0A 

Add openfin to data model for Freemarker loaded by WebBundleResources