Domanda

Alfresco 3.2c has a tracking image that's injected into the page footer using Javascript that I need to remove for a project. The javascript is actually hard coded in the SDK in the alfresco-share-src.zip in the class org/alfresco/web/scripts/MessagesWebScript.java.

We're currently building Alfresco using a Maven project and it pulls most of Alfresco and Share from maven plugins and repositories, giving us a clean root build additions in. However since this class is hard coded and we don't want to touch the original jars/zips, I thought I could just add a new copy of the file to share/src/main/java/org/alfresco/web/scripts/MessagesWebScript.java, compiling it into the war file's WEB-INF and thereby overriding what would get loaded from the jar (yes, I know a bad way of doing it).

However, if I just add the file, I get the error /share/src/main/java/org/alfresco/web/scripts/MessagesWebScript.java:[48,80] error: cannot find symbol on the line

public class MessagesWebScript extends org.springframework.extensions.webscripts.MessagesWebScript

Which leads me to believe it's not pulling in the same dependencies used to build the war file (namely the spring-surf-parent dependency). If I try to add that dependency to the share.pom file (shown below), maven successfully builds, but the dependency somehow pulls in the servlet API jar files, adds them to the war and then I get the expected The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory errors.

    <dependency>
            <groupId>org.springframework.extensions.surf</groupId>
            <artifactId>spring-surf-parent</artifactId>
            <version>1.2.0-M3</version>
    </dependency>

My share.pom looks like the following:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>share</artifactId>
<name>Alfresco Share Client</name>
<packaging>war</packaging>
<description>Alfresco Share Client</description>
<parent>
    <groupId>nz.net.mycompany</groupId>
    <artifactId>custom-alfresco</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>


<dependencies>
    <dependency>
        <groupId>${alfresco.groupId}</groupId>
        <artifactId>share</artifactId>
        <type>war</type>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.1</version><!--$NO-MVN-MAN-VER$-->
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.2.5</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>com.github.searls</groupId>
        <artifactId>jasmine-maven-plugin</artifactId>
        <version>1.3.1.2</version>
    </dependency>

</dependencies>
<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
        <resource>
            <directory>jetty</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>exec</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <executable>${basedir}/src/scripts/less2css.sh</executable>
                <arguments>
                    <argument>${basedir}</argument>
                </arguments>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <excludes>
                    <!-- Integration Tests should not be run here -->
                    <exclude>**/IT*.java</exclude>
                </excludes>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.14.1</version>
            <executions>
                <execution>
                    <goals>
<!--                             <goal>integration-test</goal> -->
<!--                             <goal>verify</goal> -->
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>com.github.searls</groupId>
            <artifactId>jasmine-maven-plugin</artifactId>
            <version>1.3.1.2</version>
            <executions>
                <execution>
                    <goals>
                        <goal>test</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <webDriverClassName>org.openqa.selenium.phantomjs.PhantomJSDriver</webDriverClassName>
                <webDriverCapabilities>
                    <phantomjs.binary.path>${project.basedir}/src/test/bin/phantomjs</phantomjs.binary.path>
                </webDriverCapabilities>
                <preloadSources>
                    <source>${project.basedir}/src/test/javascript/fixtures/fixture_messages.js</source>
                </preloadSources>
                <jsSrcDir>${project.basedir}/target/share/js/</jsSrcDir>
                <jsTestSrcDir>${project.basedir}/src/test/javascript/</jsTestSrcDir>
                <sourceIncludes>
                    <!-- add the ones we want first -->
                    <include>**/yui-common.js</include>
                    <include>**/alfresco.js</include>
                    <!-- Then the default -->
                    <include>**/*.js</include>
                </sourceIncludes>
                <haltOnFailure>false</haltOnFailure>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
                <!-- Here is can control the order of overlay of your (WAR, AMP, etc.) dependencies
                | NOTE: At least one WAR dependency must be uncompressed first
                | NOTE: In order to have a dependency effectively added to the WAR you need to
                | explicitly mention it in the overlay section.
                | NOTE: First-win resource strategy is used by the WAR plugin
                -->
                <overlays>
                    <!-- The current project customizations -->
                    <overlay />
                    <!-- The Share WAR -->
                    <overlay>
                        <groupId>${alfresco.groupId}</groupId>
                        <artifactId>share</artifactId>
                        <type>war</type>
                        <!-- To allow inclusion of META-INF -->
                        <excludes />
                    </overlay>
                </overlays>
            </configuration>
        </plugin>
    </plugins>
</build>
</project>
È stato utile?

Soluzione

If I understand correctly, you want to override a single Java class, which in this case, happens to implement a web script, but your solution of repackaging the share WAR with a forked copy of this class is not working.

Re-defining core Alfresco (or Share in this case) classes is a bad idea. This web script is declared in the Spring config file alfresco/slingshot-application-context.xml in the webapp classpath, and therefore what you should be doing is overriding it in your own *-context.xml file under alfresco/web-extension, e.g.

<?xml version='1.0' encoding='UTF-8'?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
   <!--  I18N resources and messages Web Script -->
   <bean id="webscript.org.springframework.extensions.messages.get" 
         parent="webscript" 
         class="my.custom.namespace.MessagesWebScript">
      <property name="webFrameworkConfigElement" ref="webframework.config.element"/>
      <property name="dependencyHandler" ref="dependency.handler"/>
   </bean>
</beans>

There is no reason why you should not implement the override through Spring from the start. Spring beans are designed for this purpose and it adds very little to the effort while giving you the ability to more effectively debug if it does not work as you expect.

Obviously you will also need to make sure that your custom class my.custom.namespace.MessagesWebScript compiles as part of your build, and it sounds like it is not doing so at present. This is probably because you are missing some JAR (not WAR) dependencies in your POM - take a look at the Google Docs integration Share POM for the set which we use.

Lastly I would suggest that your package your customisation as an AMP file. The Alfresco Maven SDK provides full support for this and you just need to declare it as a parent - see the Google Docs parent POM for an example.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top