portals-jetspeed-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From smi...@apache.org
Subject svn commit: r588428 - in /portals/jetspeed-2/branches/JETSPEED-2.1.3/applications/gems/src/java/org/apache/portals/gems/flash: ./ FlashPortlet.java PackedBitObj.java SWFHeader.java templates/ templates/edit-prefs.vm templates/flash-demo.vm
Date Fri, 26 Oct 2007 00:05:24 GMT
Author: smilek
Date: Thu Oct 25 17:05:23 2007
New Revision: 588428

URL: http://svn.apache.org/viewvc?rev=588428&view=rev
Log:
FlashPortlet which uses SWFObject library (http://code.google.com/p/swfobject) to dynamically
load flash content - replaces previous FlashPortlet in applications/demo

added to support loading flash content dynamically - this solves the IE click to load active
content probem (Microsoft/Eolas patent issue), and also nicely handles cases where client
flash player upgrade is needed

reads swf file header to obtain player version requirements and height/width of flash content

supports init-params object element param children and object element attributes - values
for these init-params use semicolon to separate each pair and equals to separate name and
value:

     object-params
     object-attributes
     flashvars

gems jar contains default ViewPage and EditPage templates - if ViewPage/EditPage are not specified
in portlet.xml, the jar based versions will be used

preferences are used for src, height and width:

     SRC
     HEIGHT
     WIDTH

     MAX-SRC
     MAX-HEIGHT
     MAX-WIDTH


Added:
    portals/jetspeed-2/branches/JETSPEED-2.1.3/applications/gems/src/java/org/apache/portals/gems/flash/
    portals/jetspeed-2/branches/JETSPEED-2.1.3/applications/gems/src/java/org/apache/portals/gems/flash/FlashPortlet.java
    portals/jetspeed-2/branches/JETSPEED-2.1.3/applications/gems/src/java/org/apache/portals/gems/flash/PackedBitObj.java
    portals/jetspeed-2/branches/JETSPEED-2.1.3/applications/gems/src/java/org/apache/portals/gems/flash/SWFHeader.java
    portals/jetspeed-2/branches/JETSPEED-2.1.3/applications/gems/src/java/org/apache/portals/gems/flash/templates/
    portals/jetspeed-2/branches/JETSPEED-2.1.3/applications/gems/src/java/org/apache/portals/gems/flash/templates/edit-prefs.vm
    portals/jetspeed-2/branches/JETSPEED-2.1.3/applications/gems/src/java/org/apache/portals/gems/flash/templates/flash-demo.vm

Added: portals/jetspeed-2/branches/JETSPEED-2.1.3/applications/gems/src/java/org/apache/portals/gems/flash/FlashPortlet.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/branches/JETSPEED-2.1.3/applications/gems/src/java/org/apache/portals/gems/flash/FlashPortlet.java?rev=588428&view=auto
==============================================================================
--- portals/jetspeed-2/branches/JETSPEED-2.1.3/applications/gems/src/java/org/apache/portals/gems/flash/FlashPortlet.java
(added)
+++ portals/jetspeed-2/branches/JETSPEED-2.1.3/applications/gems/src/java/org/apache/portals/gems/flash/FlashPortlet.java
Thu Oct 25 17:05:23 2007
@@ -0,0 +1,522 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.portals.gems.flash;
+
+import java.io.IOException;
+import java.io.StringWriter;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.StringTokenizer;
+import java.util.Collections;
+import java.util.Properties;
+
+import javax.portlet.ActionRequest;
+import javax.portlet.ActionResponse;
+import javax.portlet.PortletException;
+import javax.portlet.PortletPreferences;
+import javax.portlet.PortletRequest;
+import javax.portlet.RenderRequest;
+import javax.portlet.RenderResponse;
+import javax.portlet.WindowState;
+import javax.portlet.PortletConfig;
+
+import org.apache.portals.bridges.velocity.GenericVelocityPortlet;
+
+import org.apache.velocity.context.Context;
+import org.apache.velocity.app.VelocityEngine;
+import org.apache.velocity.Template;
+import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import org.apache.jetspeed.PortalReservedParameters;
+import org.apache.jetspeed.desktop.JetspeedDesktop;
+import org.apache.jetspeed.headerresource.HeaderResourceLib;
+import org.apache.jetspeed.request.RequestContext;
+
+public class FlashPortlet extends GenericVelocityPortlet
+{
+    public static final String HEIGHT_PREF = "HEIGHT";
+    public static final String WIDTH_PREF = "WIDTH";
+    public static final String SRC_PREF = "SRC";
+    public static final String MAX_SRC_PREF = "MAX-SRC";
+    public static final String MAX_HEIGHT_PREF = "MAX-HEIGHT";
+    public static final String MAX_WIDTH_PREF = "MAX-WIDTH";
+ 
+    public static final String OBJECT_PARAMS_INITPARAM = "object-params";
+    public static final String OBJECT_ATTRIBUTES_INITPARAM = "object-attributes";
+    public static final String FLASHVARS_INITPARAM = "flashvars";
+    public static final String VIEW_PAGE_INITPARAM = "ViewPage";
+
+    public static final String PARAM_VIEW_PAGE = "ViewPage";
+    public static final String PARAM_EDIT_PAGE = "EditPage";
+    public static final String PARAM_VIEW_PAGE_DEFAULT = "org/apache/portals/gems/flash/templates/flash-demo.vm";
+    public static final String PARAM_EDIT_PAGE_DEFAULT = "org/apache/portals/gems/flash/templates/edit-prefs.vm";
+    
+    public static final String CODEBASE = "codebase";
+    public static final String CLASSID = "classid";
+    public static final String NODEID = "id";
+    
+    public static final String SRC = "SRC";
+    public static final String WIDTH = "WIDTH";
+    public static final String HEIGHT = "HEIGHT";
+
+    public static final String WIDTH_ACTUAL = "widthActual";
+    public static final String HEIGHT_ACTUAL = "heightActual";
+    public static final String HEIGHT_PERCENT = "heightPercent";
+    
+    public static final String OBJECT_PARAMS = "PARAMS";
+    public static final String OBJECT_ATTRIBUTES = "ATTRIBUTES";
+    public static final String FLASHVARS = "FLASHVARS";
+    public static final String EXTRA_SIZE_INFO = "EXTRA_SIZE_INFO";
+    
+    public static final String WINDOW_STATE = "windowState";
+    public static final String NAMESPACE = "NAMESPACE";
+    public static final String REPLACECONTENT_NODEID = "REPLACECONTENT_NODEID";
+    public static final String SWF_VERSION = "SWF_VERSION";
+    public static final String SWF_VERSION_DEFAULT = "9.0.0";
+    
+    public static final String SWFOBJECTS_LIB_URL = "SWFOBJECTS_URL";
+    public static final String EXPRESS_INSTALL_URL = "EXPRESS_INSTALL_URL";
+    
+    public static final String IS_DESKTOP = "IS_DESKTOP";
+
+    protected Log log = LogFactory.getLog( FlashPortlet.class );
+    
+    protected String viewPage = null;
+    protected String editPage = null;
+
+    private Map object_parameters = null;
+    private Map object_attributes = null;    
+    private Map flash_vars = null;
+
+    private VelocityEngine engine;
+
+
+	public void init( PortletConfig config ) throws PortletException
+    { 
+        super.init(config);
+
+        String viewPage = config.getInitParameter( PARAM_VIEW_PAGE );
+        if ( viewPage == null || viewPage.length() == 0 )
+            viewPage = null;
+        this.viewPage = viewPage;
+
+        String editPage = config.getInitParameter( PARAM_EDIT_PAGE );
+        if ( editPage == null || editPage.length() == 0 )
+            editPage = null;
+        this.editPage = editPage;
+                
+        Map objParams = parseSemicolonEqualsDelimitedProps( config.getInitParameter( OBJECT_PARAMS_INITPARAM
) );
+        Map objAttrs = parseSemicolonEqualsDelimitedProps( config.getInitParameter( OBJECT_ATTRIBUTES_INITPARAM
) );
+        Map flashVars = parseSemicolonEqualsDelimitedProps( config.getInitParameter( FLASHVARS_INITPARAM
) );
+        
+        if ( objAttrs != null )
+        {
+        	objAttrs.remove( CODEBASE );
+        	objAttrs.remove( CLASSID );
+        	objAttrs.remove( NODEID );
+        	this.object_attributes = Collections.unmodifiableMap( objAttrs );
+        }
+        
+        if ( objParams != null )
+        {
+        	objParams.remove( CODEBASE );
+        	objParams.remove( CLASSID );
+        	objParams.remove( NODEID );
+        	this.object_parameters = Collections.unmodifiableMap( new HashMap( objParams ) );
+        }
+        
+        if ( flashVars != null )
+        	this.flash_vars = Collections.unmodifiableMap( flashVars );
+    }
+	
+	protected final Map getDefaultObjectParameters()
+	{
+		return this.object_parameters;
+	}
+	protected final Map getDefaultObjectAttributes()
+	{
+		return this.object_attributes;
+	}
+	protected final Map getDefaultFlashVars()
+	{
+		return this.flash_vars;
+	}
+	protected String getDefaultSwfVersion()
+	{
+		return SWF_VERSION_DEFAULT;
+	}
+	
+	protected Map getObjectParameters( RenderRequest request, RenderResponse response, SWFContext
swfContext )
+	{
+		return this.object_parameters;
+	}
+	protected Map getObjectAttributes( RenderRequest request, RenderResponse response, SWFContext
swfContext )
+	{
+		return this.object_attributes;
+	}
+	protected Map getFlashVars( RenderRequest request, RenderResponse response, SWFContext swfContext
)
+	{
+		return this.flash_vars;
+	}
+	
+	protected void setContextVars( RenderRequest request, RenderResponse response, Context context,
SWFContext swfContext )
+	{
+		setParameterContextVars( request, response, context, swfContext );
+		readSwfFileInfo( request, response, context, swfContext );
+		setSizeContextVars( request, response, context, swfContext );
+		setFinalContextVars( request, response, context, swfContext );	
+	}
+	
+	protected void setFinalContextVars( RenderRequest request, RenderResponse response, Context
context, SWFContext swfContext )
+	{
+		String namespace = response.getNamespace();
+		context.put( NAMESPACE, namespace );
+		context.put( REPLACECONTENT_NODEID, namespace + "_flash_replace" );
+		
+		RequestContext requestContext = (RequestContext)request.getAttribute( PortalReservedParameters.REQUEST_CONTEXT_ATTRIBUTE
);
+
+        String portalBaseUrl = HeaderResourceLib.getPortalBaseUrl( requestContext );
+		context.put( SWFOBJECTS_LIB_URL, portalBaseUrl + "/javascript/swfobject/swfobject.js" );
+		context.put( EXPRESS_INSTALL_URL, portalBaseUrl + "/javascript/swfobject/expressInstall.swf"
);
+		
+        Boolean desktopEnabled = (Boolean)requestContext.getAttribute( JetspeedDesktop.DESKTOP_ENABLED_REQUEST_ATTRIBUTE
);
+		context.put( IS_DESKTOP, ( (desktopEnabled == null) ? Boolean.FALSE : desktopEnabled )
);
+		
+		int swfVersion = ( swfContext.getHeader() != null ? swfContext.getHeader().getVersion()
: -1 );
+		if ( swfVersion > 0 )
+		{
+			context.put( SWF_VERSION, new Integer( swfVersion ).toString() + ".0.0" );
+		}
+		else
+		{
+			context.put( SWF_VERSION, getDefaultSwfVersion() );
+		}
+		context.put( SRC, swfContext.getSrc() );
+	}
+	
+	protected void setParameterContextVars( RenderRequest request, RenderResponse response,
Context context, SWFContext swfContext )
+	{
+		context.put( OBJECT_PARAMS, HeaderResourceLib.makeJSONObject( getObjectParameters( request,
response, swfContext ), true ).toString() );
+		
+		Map objNodeIdMap = new HashMap();
+		objNodeIdMap.put( "id", response.getNamespace() + "_objnode" );
+		Map[] attrMaps = new Map[] { getObjectAttributes( request, response, swfContext ), objNodeIdMap
};
+		context.put( OBJECT_ATTRIBUTES, HeaderResourceLib.makeJSONObject( attrMaps, true ).toString()
);
+		
+		context.put( FLASHVARS, HeaderResourceLib.makeJSONObject( getFlashVars( request, response,
swfContext ), true ).toString() );
+	}
+	
+	protected void readSwfFileInfo( RenderRequest request, RenderResponse response, Context
context, SWFContext swfContext )
+	{
+        String swfSrc = swfContext.getSrc();
+        int swfSrcLen = ( swfSrc != null ? swfSrc.length() : 0 );
+        if ( swfSrcLen > 0 )
+        {
+        	SWFHeader swfH = new SWFHeader();
+            String contextPath = request.getContextPath();
+            int contextPathLen = ( contextPath != null ? contextPath.length() : 0 );
+            if ( contextPathLen > 0 && swfSrcLen > contextPathLen &&
swfSrc.startsWith( contextPath ) )
+            {
+            	swfSrc = swfSrc.substring( contextPathLen );
+            }
+            if ( swfH.parseHeader( this.getPortletContext().getResourceAsStream( swfSrc )
) )
+            {
+            	swfContext.setHeader( swfH );
+            }
+        }
+	}
+	
+	protected void setSizeContextVars( RenderRequest request, RenderResponse response, Context
context, SWFContext swfContext )
+	{
+		String swfHeight = swfContext.getHeight();
+		String swfWidth = swfContext.getWidth();
+		String swfHeightActual = null;
+		String swfWidthActual = null;
+		SWFHeader header = swfContext.getHeader();
+		if ( header != null )
+		{
+			if ( header.getHeight() > 0 )
+				swfHeightActual = new Integer( header.getHeight() ).toString();
+        	if ( header.getWidth() > 0 )
+        		swfWidthActual = new Integer( header.getWidth() ).toString();
+		}
+
+		boolean isMaximized = swfContext.isMaximized();
+		if ( swfHeight == null )
+		{
+			if ( swfHeightActual != null )
+				swfHeight = swfHeightActual;
+			else
+				swfHeight = ( isMaximized ? "800" : "250" );
+			swfContext.setHeight( swfHeight );
+		}
+		if ( swfWidth == null )
+		{
+			swfWidth = "100%";    // ( isMaximized ? "600" : "250" );
+			swfContext.setWidth( swfWidth );
+		}
+		context.put( HEIGHT, swfHeight );
+        context.put( WIDTH, swfWidth );
+        
+        Map extraSizeVars = new HashMap();
+        if ( swfHeightActual != null )
+        	extraSizeVars.put( HEIGHT_ACTUAL, swfHeightActual );
+        if ( swfWidthActual != null )
+        	extraSizeVars.put( WIDTH_ACTUAL, swfWidthActual );
+        
+        String heightPercent = swfContext.getHeightPercentage();
+        if ( heightPercent != null )
+        	extraSizeVars.put( HEIGHT_PERCENT, heightPercent );
+        
+        context.put( EXTRA_SIZE_INFO, HeaderResourceLib.makeJSONObject( extraSizeVars, true
).toString() );
+	}
+       
+    public void doView(RenderRequest request, RenderResponse response)
+        throws PortletException, IOException
+    {
+        Context context = super.getContext(request);
+        PortletPreferences prefs = request.getPreferences();
+        
+        String swfSrc = null;
+        String swfHeight = null;
+        String swfWidth = null;
+        boolean isMaximized = false;
+        
+        if ( request.getWindowState().toString().equals( WindowState.MAXIMIZED.toString()
) )
+        {
+        	isMaximized = true;
+        	swfHeight = prefs.getValue( MAX_HEIGHT_PREF, null );
+            swfWidth = prefs.getValue( MAX_WIDTH_PREF, null );
+            swfSrc = prefs.getValue( MAX_SRC_PREF, null );
+        }
+        
+        if ( swfHeight == null || swfHeight.length() == 0 )
+        	swfHeight = prefs.getValue( HEIGHT_PREF, null );
+        
+        if ( swfWidth == null || swfWidth.length() == 0 )
+        	swfWidth = prefs.getValue( WIDTH_PREF, null );
+        
+        if ( swfSrc == null || swfSrc.length() == 0 )
+        	swfSrc = prefs.getValue( SRC_PREF, null );
+
+        context.put( WINDOW_STATE, ( (! isMaximized) ? "normal" : "max" ) );
+        
+        SWFContext swfContext = new SWFContext( swfSrc, swfHeight, swfWidth, isMaximized
);
+        setContextVars( request, response, context, swfContext );
+        
+        if ( this.viewPage != null )
+        {
+            super.doView( request, response );
+        }
+        else
+        {
+            processClasspathTemplate( PARAM_VIEW_PAGE_DEFAULT, context, response );
+        }
+    }
+
+    public void doEdit( RenderRequest request, RenderResponse response ) throws PortletException,
IOException
+    {
+        if ( this.editPage != null )
+        {
+            response.setContentType("text/html");
+            doPreferencesEdit(request, response);
+        }
+        else
+        {
+            setupPreferencesEdit( request, response );
+            processClasspathTemplate( PARAM_EDIT_PAGE_DEFAULT, getContext( request ), response
);
+        }
+    }
+
+    protected void processClasspathTemplate( String classpathTemplate, Context context, RenderResponse
response ) throws PortletException
+    {
+        response.setContentType("text/html");
+        VelocityEngine vEngine = null;
+        synchronized ( this )
+        {
+            vEngine = this.engine;
+            if ( vEngine == null )
+            {
+                vEngine = new VelocityEngine();
+                configureClasspathVelocityEngine( vEngine );
+                this.engine = vEngine;
+            }
+        }
+
+        try
+        {
+            Template template = vEngine.getTemplate( classpathTemplate );
+        
+            StringWriter writer = new StringWriter();
+            template.merge( context, writer );
+            writer.close();
+            
+            response.getPortletOutputStream().write( writer.getBuffer().toString().getBytes()
);
+            response.getPortletOutputStream().flush();
+        }
+        catch ( Exception ex )
+        {
+            String errMsg = "Failed to generate content with classpath based VelocityEngine
for " + this.getClass().getName() + " due to " + ex.getClass().getName() + " " + ex.getMessage();
+            log.error( errMsg );
+            throw new PortletException( errMsg );
+        }
+    }
+
+    protected void configureClasspathVelocityEngine( VelocityEngine vEngine ) throws PortletException
+    {
+        try
+        {
+            Properties props = new Properties();
+            props.setProperty( VelocityEngine.RESOURCE_LOADER, "classpath" );
+            props.setProperty( "classpath." + VelocityEngine.RESOURCE_LOADER + ".class",
ClasspathResourceLoader.class.getName() );
+            vEngine.init( props );
+        }
+        catch ( Exception ex )
+        {
+            String errMsg = "Failed to configure classpath based VelocityEngine for " + this.getClass().getName()
+ " due to " + ex.getClass().getName() + " " + ex.getMessage();
+            log.error( errMsg );
+            throw new PortletException( errMsg );
+        }
+    }
+
+
+    /* (non-Javadoc)
+     * @see org.apache.portals.bridges.velocity.GenericVelocityPortlet#processAction(javax.portlet.ActionRequest,
javax.portlet.ActionResponse)
+     */
+    public void processAction(ActionRequest request, ActionResponse response) throws PortletException,
IOException
+    {
+        String source = request.getParameter(SRC_PREF);
+        String height = request.getParameter(HEIGHT_PREF);
+        String width = request.getParameter(WIDTH_PREF);
+        String maxSource = request.getParameter(MAX_SRC_PREF);
+        String maxHeight = request.getParameter(MAX_HEIGHT_PREF);
+        String maxWidth = request.getParameter(MAX_WIDTH_PREF);
+        
+        PortletPreferences prefs = request.getPreferences();
+        prefs.setValue(SRC_PREF, source);
+        prefs.setValue(HEIGHT_PREF, height);
+        prefs.setValue(WIDTH_PREF, width);
+        prefs.setValue(MAX_SRC_PREF, maxSource);
+        prefs.setValue(MAX_HEIGHT_PREF, maxHeight);
+        prefs.setValue(MAX_WIDTH_PREF, maxWidth);        
+        prefs.store();
+        super.processAction(request, response);
+    }
+    
+    public Map parseSemicolonEqualsDelimitedProps( String propsStr )
+    {
+        if ( propsStr == null || propsStr.length() == 0 )
+            return null;
+        Map props = new HashMap();
+        StringTokenizer parser = new StringTokenizer( propsStr, ";" );
+        String token, propNm, propVal;
+        int eqPos;
+        while ( parser.hasMoreTokens() )
+        {
+            token = parser.nextToken();
+            eqPos = token.indexOf( '=' );
+            if ( eqPos > 0 )
+            {
+            	propNm = token.substring( 0, eqPos );
+            	if ( eqPos < (token.length() -1) )
+            	{
+            		propVal = token.substring( eqPos + 1 );
+            		props.put( propNm.toLowerCase(), propVal );
+            	}
+            }
+        }
+        return props;
+    }
+    
+    protected class SWFContext
+    {
+    	private String src;
+    	private SWFHeader header;
+    	private String height;
+    	private String height_percentage;
+    	private String width;
+    	private boolean is_maximized;
+    	public SWFContext( String swfSrc, String swfHeight, String swfWidth, boolean isMaximized
)
+    	{
+    		setSrc( swfSrc );
+    		setHeight( swfHeight );
+    		setWidth( swfWidth );
+    		this.is_maximized = isMaximized;
+    	}
+		public String getSrc()
+		{
+        	return src;
+        }
+		public void setSrc( String src )
+		{
+			if ( src == null || src.length() == 0 )
+				src = null;
+        	this.src = src;
+        }
+		public SWFHeader getHeader()
+		{
+        	return header;
+        }
+		public void setHeader( SWFHeader swfHeader )
+		{
+        	this.header = swfHeader;
+        }
+		public String getHeight()
+		{
+        	return height;
+        }
+		public void setHeight( String height )
+		{
+			if ( height == null || height.length() == 0 )
+				height = null;
+			else
+			{
+				height = height.trim();
+				if ( height.endsWith("%") )
+				{
+					if ( height.length() > 1 )
+					{
+						this.height_percentage = height;
+					}
+					height = null;
+				}
+			}
+        	this.height = height;
+        }
+		public String getHeightPercentage()
+		{
+        	return height_percentage;
+        }
+		public String getWidth()
+		{
+        	return width;
+        }
+		public void setWidth( String width )
+		{
+			if ( width == null || width.length() == 0 )
+				width = null;
+        	this.width = width;
+        }
+		public boolean isMaximized()
+		{
+        	return is_maximized;
+        }
+    }
+}

Added: portals/jetspeed-2/branches/JETSPEED-2.1.3/applications/gems/src/java/org/apache/portals/gems/flash/PackedBitObj.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/branches/JETSPEED-2.1.3/applications/gems/src/java/org/apache/portals/gems/flash/PackedBitObj.java?rev=588428&view=auto
==============================================================================
--- portals/jetspeed-2/branches/JETSPEED-2.1.3/applications/gems/src/java/org/apache/portals/gems/flash/PackedBitObj.java
(added)
+++ portals/jetspeed-2/branches/JETSPEED-2.1.3/applications/gems/src/java/org/apache/portals/gems/flash/PackedBitObj.java
Thu Oct 25 17:05:23 2007
@@ -0,0 +1,54 @@
+/**
+ * PackedBitObj is (c) 2006 Paul Brooks Andrus and is released under the MIT License:
+ * http://www.opensource.org/licenses/mit-license.php
+ * 
+ * http://www.brooksandrus.com/blog/2006/08/01/lightweight-swf-header-reader-java/
+ * package com.brooksandrus.utils.swf
+ */
+package org.apache.portals.gems.flash;
+
+/**
+ * @author brooks
+ * 
+ */
+public class PackedBitObj
+{
+    public int    bitIndex         = 0;
+    public int    byteIndex        = 0;
+    public int    value            = 0;
+    public int    nextBitIndex     = 0;
+    public int    nextByteIndex    = 0;
+    public int    nextByteBoundary = 0;
+
+    /**
+     * @param bitIndex
+     *           The index of the last bit read
+     * @param byteMarker
+     *           The index of the last byte read
+     * @param decimalValue
+     *           The decimal value of the packed bit sequence
+     * @param binaryString
+     *           
+     */
+    public PackedBitObj( int bitMarker, int byteMarker, int decimalValue )
+    {
+        bitIndex = bitMarker;
+        byteIndex = byteMarker;
+        value = decimalValue;
+        nextBitIndex = bitMarker;
+
+        if ( bitMarker <= 7 )
+        {
+            nextBitIndex++;
+            nextByteIndex = byteMarker;
+            nextByteBoundary = byteMarker++;
+        }
+        else
+        {
+            nextBitIndex = 0;
+            nextByteIndex++;
+            nextByteBoundary = nextByteIndex;
+        }
+    }
+
+}

Added: portals/jetspeed-2/branches/JETSPEED-2.1.3/applications/gems/src/java/org/apache/portals/gems/flash/SWFHeader.java
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/branches/JETSPEED-2.1.3/applications/gems/src/java/org/apache/portals/gems/flash/SWFHeader.java?rev=588428&view=auto
==============================================================================
--- portals/jetspeed-2/branches/JETSPEED-2.1.3/applications/gems/src/java/org/apache/portals/gems/flash/SWFHeader.java
(added)
+++ portals/jetspeed-2/branches/JETSPEED-2.1.3/applications/gems/src/java/org/apache/portals/gems/flash/SWFHeader.java
Thu Oct 25 17:05:23 2007
@@ -0,0 +1,470 @@
+/**
+ * SWFHeader is (c) 2006 Paul Brooks Andrus and is released under the MIT License:
+ * http://www.opensource.org/licenses/mit-license.php
+ * 
+ * http://www.brooksandrus.com/blog/2006/08/01/lightweight-swf-header-reader-java/
+ * com.brooksandrus.utils.swf
+ * 
+ * Modified to efficiently read only the swf header (Steve Milek)
+ * 
+ */
+package org.apache.portals.gems.flash;
+
+import java.io.*;
+import java.util.zip.*;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+
+/**
+ * @author brooks
+ * 
+ */
+public class SWFHeader
+{
+    protected Log log = LogFactory.getLog( SWFHeader.class );
+    public static final String COMPRESSED   = "compressed";
+    public static final String UNCOMPRESSED = "uncompressed";
+    
+    private String       signature;
+    private String       compressionType;
+    private int          version;
+    private long         size;
+    private int          nbits;
+    private int          xmax;
+    private int          ymax;
+    private int          width;
+    private int          height;
+    private int          frameRate;
+    private int          frameCount;
+
+    public SWFHeader()
+    {
+        super();
+    }   
+   
+    public boolean parseHeader( String fileName )
+    {
+        if ( fileName == null )
+        {
+            log.error( "Name for SWF file is null" );
+            return false;
+        }
+        return manageInputStreamAndParseHeader( null, new File( fileName ) );
+    }
+   
+    public boolean parseHeader( File file )
+    {
+        return manageInputStreamAndParseHeader( null, file );
+    }
+   
+    public boolean parseHeader( InputStream is )
+    {
+        return manageInputStreamAndParseHeader( is, null );
+    }
+   
+    private boolean manageInputStreamAndParseHeader( InputStream is, File file )
+    {
+        boolean inputIsSWF = false;
+        try
+        {
+            if ( is == null && file != null )
+            {
+                is = new FileInputStream( file );
+            }
+            inputIsSWF = doParseHeader( is );
+        }
+        catch ( FileNotFoundException fnfEx )
+        {
+            log.error( "SWF file could not be found", fnfEx );
+            inputIsSWF = false;
+        }
+        catch ( Exception e )
+        {
+            log.error( "Failed to parse SWF input", e );
+            inputIsSWF = false;
+        }
+        finally
+        {
+            try
+            {
+                if ( is != null )
+                {
+                    is.close();
+                }
+            }
+            catch ( Exception ex )
+            {
+                log.error( "Failed to close SWF InputStream", ex );
+                inputIsSWF = false;
+            }
+        }
+        return inputIsSWF;
+    }
+  
+    private boolean doParseHeader( InputStream is ) throws Exception
+    {
+        byte[] temp = new byte[128];   // header is 47 bytes - we must read more in case
compression is used (see uncompressHeader comments)
+        
+        byte[] swf = null;
+
+        is.read( temp );
+
+        if ( !isSWF( temp ) )
+        {
+            log.error( "Input does not match SWF format - incorrect file signature" );
+            return false;
+        }
+        else
+        {
+            signature = "" + ( char ) temp[0] + ( char ) temp[1]
+                + ( char ) temp[2];
+        }
+
+        if ( isCompressed( temp[0] ) )
+        {
+            swf = uncompressHeader( temp );
+            compressionType = SWFHeader.COMPRESSED;
+        }
+        else
+        {
+            swf = temp;
+            compressionType = SWFHeader.UNCOMPRESSED;
+        }
+
+        //System.out.println( "swf byte array length: " + swf.length );
+       
+        // version is the 4th byte of a swf; 
+        version = swf[3];
+
+        // bytes 5 - 8 represent the size in bytes of a swf
+        size = readSize( swf );
+
+        // Stage dimensions are stored in a rect
+
+        nbits = ( ( swf[8] & 0xff ) >> 3 );
+
+        PackedBitObj pbo = readPackedBits( swf, 8, 5, nbits );
+       
+        PackedBitObj pbo2 = readPackedBits( swf, pbo.nextByteIndex,
+                                            pbo.nextBitIndex, nbits );
+
+        PackedBitObj pbo3 = readPackedBits( swf, pbo2.nextByteIndex,
+                                            pbo2.nextBitIndex, nbits );
+
+        PackedBitObj pbo4 = readPackedBits( swf, pbo3.nextByteIndex,
+                                            pbo3.nextBitIndex, nbits );
+
+        xmax = pbo2.value;
+        ymax = pbo4.value;
+
+        width = convertTwipsToPixels( xmax );
+        height = convertTwipsToPixels( ymax );
+
+        int bytePointer = pbo4.nextByteIndex + 2;
+
+        frameRate = swf[bytePointer];
+        bytePointer++;
+       
+       
+        int fc1 = swf[bytePointer] & 0xFF;
+        bytePointer++;
+       
+        int fc2 = swf[bytePointer] & 0xFF;
+        bytePointer++;
+       
+        frameCount = ( fc2 << 8 ) + fc1;
+       
+        dumpHeaderToStdOut();
+
+        return true;
+    }
+
+    public void read( byte[] output, byte[] input, int offset )
+    {
+        System.arraycopy( input, offset, output, 0, output.length - offset );
+    }
+
+    public PackedBitObj readPackedBits( byte[] bytes, int byteMarker,
+                                        int bitMarker, int length )
+    {
+        int total = 0;
+        int shift = 7 - bitMarker;
+        int counter = 0;
+        int bitIndex = bitMarker;
+        int byteIndex = byteMarker;
+      
+        while ( counter < length )
+        {
+            for ( int i = bitMarker; i < 8; i++ )
+            {
+                int bit = ( ( bytes[byteMarker] & 0xff ) >> shift ) & 1;
+                total = ( total << 1 ) + bit;
+                bitIndex = i;
+                shift--;
+                counter++;
+            
+                if ( counter == length )
+                {
+                    break;
+                }
+            }
+            byteIndex = byteMarker;
+            byteMarker++;
+            bitMarker = 0;
+            shift = 7;
+        }
+        return new PackedBitObj( bitIndex, byteIndex, total );
+    }
+
+    public int convertTwipsToPixels( int twips )
+    {
+        return twips / 20;
+    }
+
+    public int convertPixelsToTwips( int pixels )
+    {
+        return pixels * 20;
+    }
+
+    public boolean isSWF( byte[] signature )
+    {
+        String sig = "" + ( char ) signature[0] + ( char ) signature[1]
+            + ( char ) signature[2];
+
+        if ( sig.equals( "FWS" ) || sig.equals( "CWS" ) )
+        {
+            return true;
+        }
+        else
+        {
+            return false;
+        }
+    }
+
+    public boolean isCompressed( int firstByte )
+    {
+        if ( firstByte == 67 )
+        {
+            return true;
+        }
+        else
+        {
+            return false;
+        }
+    }
+   
+    public boolean isCompressed()
+    {
+        boolean result = false;
+        if ( signature.equalsIgnoreCase( "CWS" ) )
+        {
+            result = true;
+        }
+        return result;
+    }
+
+    // Inflator class will be upset if we let it read to the end of input and it discovers
that
+    // the zip format is not properly terminated - therefore, we must provide Inflater with
more input
+    // than what we intend to inflate
+    protected byte[] uncompressHeader( byte[] bytes ) throws DataFormatException
+    {  
+        Inflater decompressor = new Inflater();
+        byte[] compressed = strip( bytes );
+        decompressor.setInput( compressed );   // feed the Inflater the bytes
+        byte[] buffer = new byte[ 56 ];
+        int count = decompressor.inflate( buffer );   // decompress the data into the buffer
+        decompressor.end();
+        
+        //create an array to hold the header and body bytes
+        byte[] swf = new byte[ 8 + count ];
+        //copy the first 8 bytes which are uncompressed into the swf array
+        System.arraycopy( bytes, 0, swf, 0, 8 );
+        //copy the uncompressed data into the swf array
+        System.arraycopy( buffer, 0, swf, 8, count );
+        //the first byte of the swf indicates whether the swf is compressed or not
+        swf[0] = 70;
+      
+        return swf;
+    }
+
+    // This version of uncompressHeader may be safer (because it uses InflaterInputStream),
+    // but until there is some evidence of this we will stick with the direct approach of
+    // using Inflater (above).
+    /*
+      protected byte[] uncompressHeader( byte[] headerBytes ) throws IOException
+      {
+      byte[] compressed = strip( headerBytes );
+      InflaterInputStream iis = new InflaterInputStream( new ByteArrayInputStream( compressed
) );
+      byte[] buffer = new byte[56];
+      int bytesRead = iis.read( buffer, 0, buffer.length );
+      iis.close();
+
+      byte[] swfHeader = new byte[ 8 + bytesRead ];
+        
+      // copy the first 8 bytes which are uncompressed into the swf array
+      System.arraycopy( headerBytes, 0, swfHeader, 0, 8 );
+        
+      // copy the uncompressed data into the swf array
+      System.arraycopy( buffer, 0, swfHeader, 8, bytesRead );
+        
+      // the first byte of the swf indicates whether the swf is compressed or not
+      swfHeader[0] = 70;
+        
+      return swfHeader;
+      }
+    */
+
+    public int readSize( byte[] bytes )
+    {
+        int s = 0;
+        for ( int i = 0; i < 4; i++ )
+        {
+            s = ( s << 8 ) + bytes[i + 4];
+        }
+
+        s = Integer.reverseBytes( s ) - 1;
+
+        return s;
+    }
+    
+    public byte[] strip( byte[] bytes )
+    {
+        byte[] compressable = new byte[bytes.length - 8];
+        System.arraycopy( bytes, 8, compressable, 0, bytes.length - 8 );//fills a byte array
with data needing decompression
+        return compressable;
+    }
+
+    
+    /**
+     * @param args
+     */
+    public static void main( String[] args )
+    {
+        if ( args.length != 1 )
+        {
+            System.err.println( "usage: swf_file" );
+        }
+        else
+        {
+            try
+            {
+                SWFHeader swfH = new SWFHeader();
+                if ( swfH.parseHeader( args[0] ) )
+                {
+                    swfH.dumpHeaderToStdOut();
+                }
+            }
+            catch ( Exception e )
+            {
+                System.err.println( e.getMessage() );
+            }
+        }
+        
+    }
+
+    public void dumpHeaderToStdOut()
+    {
+        System.out.println( "signature:   " + getSignature() );
+        System.out.println( "version:     " + getVersion() );
+        System.out.println( "compression: " + getCompressionType() );
+        System.out.println( "size:        " + getSize() );
+        System.out.println( "nbits:       " + getNbits() );
+        System.out.println( "xmax:        " + getXmax() );
+        System.out.println( "ymax:        " + getYmax() );
+        System.out.println( "width:       " + getWidth() );
+        System.out.println( "height:      " + getHeight() );
+        System.out.println( "frameRate:   " + getFrameRate() );
+        System.out.println( "frameCount:  " + getFrameCount() );
+    }
+
+    /**
+     * @return the frameCount
+     */
+    public int getFrameCount()
+    {
+        return frameCount;
+    }
+
+    /**
+     * @return the frameRate
+     */
+    public int getFrameRate()
+    {
+        return frameRate;
+    }
+
+    /**
+     * @return the nbits
+     */
+    public int getNbits()
+    {
+        return nbits;
+    }
+
+    /**
+     * @return the signature
+     */
+    public String getSignature()
+    {
+        return signature;
+    }
+
+    /**
+     * @return the size
+     */
+    public long getSize()
+    {
+        return size;
+    }
+
+    /**
+     * @return the version
+     */
+    public int getVersion()
+    {
+        return version;
+    }
+
+    /**
+     * @return the xmax
+     */
+    public int getXmax()
+    {
+        return xmax;
+    }
+
+    /**
+     * @return the ymax
+     */
+    public int getYmax()
+    {
+        return ymax;
+    }
+
+    /**
+     * @return the compressionType
+     */
+    public String getCompressionType()
+    {
+        return compressionType;
+    }
+
+    /**
+     * @return the height
+     */
+    public int getHeight()
+    {
+        return height;
+    }
+
+    /**
+     * @return the width
+     */
+    public int getWidth()
+    {
+        return width;
+    }
+
+}

Added: portals/jetspeed-2/branches/JETSPEED-2.1.3/applications/gems/src/java/org/apache/portals/gems/flash/templates/edit-prefs.vm
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/branches/JETSPEED-2.1.3/applications/gems/src/java/org/apache/portals/gems/flash/templates/edit-prefs.vm?rev=588428&view=auto
==============================================================================
--- portals/jetspeed-2/branches/JETSPEED-2.1.3/applications/gems/src/java/org/apache/portals/gems/flash/templates/edit-prefs.vm
(added)
+++ portals/jetspeed-2/branches/JETSPEED-2.1.3/applications/gems/src/java/org/apache/portals/gems/flash/templates/edit-prefs.vm
Thu Oct 25 17:05:23 2007
@@ -0,0 +1,62 @@
+
+#*
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+*#
+
+#*
+   edit-prefs.vm copied from jetspeed demo portlet-app
+   prefField macro copied from demo velocity-macros.vm
+*#
+
+#macro (prefField $key $values $size)
+#if ($ERRORS)
+#set ($eflag = "$!ERRORS.get($id)")
+#else
+#set ($eflag = "")
+#end
+#set ($pv = $pf.Value)
+#foreach ($x in $pv)
+#set ($extr = $x)
+#end
+#foreach ($x in $values)
+#set ($extr = $x)
+#end
+  <tr colspan="4" align="right">
+  #if ($!eflag == "")
+    <td width="5%" align="left">&nbsp;</td>
+  #else
+    <td width="5%" class="portlet-msg-error" align="left">&nbsp;</td>  
+  #end
+    <td nowrap class="portlet-section-alternate" align="right">$!key:&nbsp;</td>
+    <td class="portlet-section-body" align="left">
+      <input id="$!id" type="text" name="$!key" size="$!size" value="$!extr" class="portlet-form-field-label">
+    </td>
+    <td width="5%" class="portlet-form-label" align="left">&nbsp;</td>
+  </tr>
+#end
+
+<h2 class="portlet-section-header">Edit Preferences</h2>
+
+<form action="$renderResponse.createActionURL()" method="post">
+<table>
+#foreach ($pref in $prefs)
+#prefField($pref.Key $pref.Value "40")
+#end
+</table>
+<input type="submit" name="Save" value="Save" />
+</form>
+
+

Added: portals/jetspeed-2/branches/JETSPEED-2.1.3/applications/gems/src/java/org/apache/portals/gems/flash/templates/flash-demo.vm
URL: http://svn.apache.org/viewvc/portals/jetspeed-2/branches/JETSPEED-2.1.3/applications/gems/src/java/org/apache/portals/gems/flash/templates/flash-demo.vm?rev=588428&view=auto
==============================================================================
--- portals/jetspeed-2/branches/JETSPEED-2.1.3/applications/gems/src/java/org/apache/portals/gems/flash/templates/flash-demo.vm
(added)
+++ portals/jetspeed-2/branches/JETSPEED-2.1.3/applications/gems/src/java/org/apache/portals/gems/flash/templates/flash-demo.vm
Thu Oct 25 17:05:23 2007
@@ -0,0 +1,32 @@
+#*
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+*#
+
+#if ( ! $IS_DESKTOP )
+   <script type="text/javascript" src="$SWFOBJECTS_URL"></script>
+#end
+
+
+<script type="text/javascript">
+    ( ( window.jetspeed && window.jetspeed.ui ) ? window.jetspeed.ui.swfobject :
swfobject ).embedSWF( "$SRC", "$REPLACECONTENT_NODEID", "$WIDTH", "$HEIGHT", "$SWF_VERSION",
"$EXPRESS_INSTALL_URL", $FLASHVARS, $PARAMS, $ATTRIBUTES, $EXTRA_SIZE_INFO );
+</script>
+
+<center>
+    <div id="$REPLACECONTENT_NODEID">
+        <p><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif"
alt="Get Adobe Flash player" /></a></p>
+    </div>
+</center>
+



---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@portals.apache.org
For additional commands, e-mail: jetspeed-dev-help@portals.apache.org


Mime
View raw message