From 73f0e70cc29065cb1f9bda6eb13d7314859bc20c Mon Sep 17 00:00:00 2001
From: Pawel GUTOWSKI <Pawel.GUTOWSKI@ext.ec.europa.eu>
Date: Thu, 4 Jan 2018 14:49:05 +0100
Subject: [PATCH] EDELIVERY-1978 Fixed WebLogic issue with spring-web.jar

---
 .../smp/config/SmpServicesTestConfig.java     |   2 +-
 smp-webapp/src/main/webapp/WEB-INF/web.xml    |  15 ++-
 .../src/main/webapp/WEB-INF/weblogic.xml      |   3 +
 smp-webapp/src/test/java/ClassLoaderTest.java | 115 ++++++++++++++++++
 .../smp/server/util/DefaultHttpHeader.java    |  88 --------------
 5 files changed, 131 insertions(+), 92 deletions(-)
 create mode 100644 smp-webapp/src/test/java/ClassLoaderTest.java
 delete mode 100644 smp-webapp/src/test/java/eu/europa/ec/cipa/smp/server/util/DefaultHttpHeader.java

diff --git a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/config/SmpServicesTestConfig.java b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/config/SmpServicesTestConfig.java
index ba8023b8f..0f9dbbced 100644
--- a/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/config/SmpServicesTestConfig.java
+++ b/smp-server-library/src/test/java/eu/europa/ec/edelivery/smp/config/SmpServicesTestConfig.java
@@ -35,7 +35,7 @@ import javax.sql.DataSource;
 @ComponentScan(basePackages = {
         "eu.europa.ec.edelivery.smp.services",
         "eu.europa.ec.edelivery.smp.data.dao",
-        "eu.europa.ec.cipa.smp.server.hook",
+        "eu.europa.ec.edelivery.smp.sml",
         "eu.europa.ec.edelivery.smp.conversion",
         "eu.europa.ec.cipa.smp.server.util"})
 @PropertySource(value = "classpath:config.properties")
diff --git a/smp-webapp/src/main/webapp/WEB-INF/web.xml b/smp-webapp/src/main/webapp/WEB-INF/web.xml
index 3b70ef17d..c8bc20c50 100644
--- a/smp-webapp/src/main/webapp/WEB-INF/web.xml
+++ b/smp-webapp/src/main/webapp/WEB-INF/web.xml
@@ -11,9 +11,18 @@
   ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   ~ See the Licence for the specific language governing permissions and limitations under the Licence.
   -->
-<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
-         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
-         id="SMP_WebApp" version="3.0">
+<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
+         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
+         version="3.1" metadata-complete="true">
+    <!--
+    WebLogic scans all application's JAR files to search for some annotated classes.
+    This scanning has an issue with spring-web-5.0.2.RELEASE.jar -
+    that results in deployment failure with strange error "array index out of bounds".
+    metadata-complete="true" is set to inform WebLogic to NOT scan JARs
+    -->
+
 
     <display-name>SMP - OASIS REST services</display-name>
 
diff --git a/smp-webapp/src/main/webapp/WEB-INF/weblogic.xml b/smp-webapp/src/main/webapp/WEB-INF/weblogic.xml
index ddba27828..4a51dcecb 100644
--- a/smp-webapp/src/main/webapp/WEB-INF/weblogic.xml
+++ b/smp-webapp/src/main/webapp/WEB-INF/weblogic.xml
@@ -24,6 +24,9 @@
             <package-name>org.slf4j.*</package-name>
             <package-name>org.apache.commons.*</package-name>
 
+            <!-- used to switch off bean validation which resulted in failing Spring context to load -->
+            <package-name>javax.validation.*</package-name>
+
         </prefer-application-packages>
         <prefer-application-resources>
             <resource-name>org/slf4j/impl/StaticLoggerBinder.class</resource-name>
diff --git a/smp-webapp/src/test/java/ClassLoaderTest.java b/smp-webapp/src/test/java/ClassLoaderTest.java
new file mode 100644
index 000000000..4bd0cb7b4
--- /dev/null
+++ b/smp-webapp/src/test/java/ClassLoaderTest.java
@@ -0,0 +1,115 @@
+/*
+ * Copyright 2018 European Commission | CEF eDelivery
+ *
+ * Licensed under the EUPL, Version 1.2 or - as soon they will be approved by the European Commission - subsequent versions of the EUPL (the "Licence");
+ * You may not use this work except in compliance with the Licence.
+ *
+ * You may obtain a copy of the Licence attached in file: LICENCE-EUPL-v1.2.pdf
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the Licence is distributed on an "AS IS" basis,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the Licence for the specific language governing permissions and limitations under the Licence.
+ */
+
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.jar.JarEntry;
+import java.util.jar.JarInputStream;
+
+
+public class ClassLoaderTest {
+
+    static int loadableClassesCnt = 0;
+    static int notLoadableClassesCnt = 0;
+
+    public static void main(String [] args) {
+        try {
+            List classList = null;
+            String jarPath = "E:\\src\\smp\\smp-webapp\\target\\cipa-smp-full-webapp-4.0.0-SNAPSHOT\\WEB-INF\\lib\\spring-webmvc-5.0.2.RELEASE.jar";
+            classList = getClassesFromJARFile (jarPath);
+        }
+        catch(Exception e) {
+            System.out.println("Exception = " + e);
+        }
+        System.out.println("Loadable / not loadable classes: "+loadableClassesCnt + " / "+notLoadableClassesCnt);
+    }
+
+    private static List getClassesFromJARFile(String jar)
+    {
+        final List classes = new ArrayList();
+        JarInputStream jarFile = null;
+        try
+        {
+            jarFile = new JarInputStream(new FileInputStream(jar));
+            JarEntry jarEntry = null;
+            do
+            {
+                try
+                {
+                    jarEntry = jarFile.getNextJarEntry();
+                }
+                catch(Exception ioe)
+                {
+                    System.out.println("Unable to get next jar entry from jar file '"+jar+"'");
+                    ioe.printStackTrace();
+                }
+                if (jarEntry != null)
+                {
+                    extractClassFromJar(jar, classes, jarEntry);
+                }
+            } while (jarEntry != null);
+            closeJarFile(jarFile);
+        }
+        catch(Exception ioe)
+        {
+            System.out.println("Unable to get Jar input stream from '"+jar+"'");
+            ioe.printStackTrace();
+        }
+        finally
+        {
+            closeJarFile(jarFile);
+        }
+        return classes;
+    }
+
+    private static void extractClassFromJar(final String jar, final List classes, JarEntry jarEntry) throws IOException
+    {
+        String className = jarEntry.getName();
+        if (className.endsWith(".class"))
+        {
+            className = className.substring(0, className.length() - ".class".length());
+            try
+            {
+                classes.add(Class.forName(className.replace('/', '.')));
+                loadableClassesCnt++;
+                System.out.println("Successfully loaded class " + className);
+            } catch (ClassNotFoundException | NoClassDefFoundError ncdfe)
+            {
+                notLoadableClassesCnt++;
+
+                System.err.println("Failed to load class " + className + "       reason: "+ncdfe.getMessage());
+                //cnfe.printStackTrace();
+                //throw new IOException("unable to find class named " + className.replace('/', '.') + "' within jar '" + jar + "'", cnfe);
+            }
+        }
+    }
+
+    private static void closeJarFile(final JarInputStream jarFile)
+    {
+        if(jarFile != null)
+        {
+            try
+            {
+                jarFile.close();
+            }
+            catch(Exception ioe)
+            {
+                System.out.println("Unable to Close Jar File '"+jarFile+"'");
+                ioe.printStackTrace();
+            }
+        }
+    }
+
+} 
\ No newline at end of file
diff --git a/smp-webapp/src/test/java/eu/europa/ec/cipa/smp/server/util/DefaultHttpHeader.java b/smp-webapp/src/test/java/eu/europa/ec/cipa/smp/server/util/DefaultHttpHeader.java
deleted file mode 100644
index b993b9abf..000000000
--- a/smp-webapp/src/test/java/eu/europa/ec/cipa/smp/server/util/DefaultHttpHeader.java
+++ /dev/null
@@ -1,88 +0,0 @@
-/*
- * Copyright 2017 European Commission | CEF eDelivery
- *
- * Licensed under the EUPL, Version 1.2 or – as soon they will be approved by the European Commission - subsequent versions of the EUPL (the "Licence");
- * You may not use this work except in compliance with the Licence.
- *
- * You may obtain a copy of the Licence attached in file: LICENCE-EUPL-v1.2.pdf
- *
- * Unless required by applicable law or agreed to in writing, software distributed under the Licence is distributed on an "AS IS" basis,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the Licence for the specific language governing permissions and limitations under the Licence.
- */
-
-package eu.europa.ec.cipa.smp.server.util;
-
-import javax.ws.rs.core.Cookie;
-import javax.ws.rs.core.HttpHeaders;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.MultivaluedMap;
-import java.util.*;
-
-/**
- * Created by rodrfla on 13/01/2017.
- */
-public class DefaultHttpHeader implements HttpHeaders {
-
-    private Map<String, List<String>> headerMap;
-    private List<String> requestHeaders;
-
-    {
-        headerMap = new HashMap<>();
-        requestHeaders = new ArrayList<>();
-    }
-
-    public void addRequestHeader(String headerParameter, List<String> values) {
-        headerMap.put(headerParameter, values);
-    }
-
-    @Override
-    public List<String> getRequestHeader(String headerParameter) {
-        return headerMap.get(headerParameter);
-    }
-
-    @Override
-    public String getHeaderString(String name) {
-        return null;
-    }
-
-    @Override
-    public MultivaluedMap<String, String> getRequestHeaders() {
-        return null;
-    }
-
-    @Override
-    public List<MediaType> getAcceptableMediaTypes() {
-        return null;
-    }
-
-    @Override
-    public List<Locale> getAcceptableLanguages() {
-        return null;
-    }
-
-    @Override
-    public MediaType getMediaType() {
-        return null;
-    }
-
-    @Override
-    public Locale getLanguage() {
-        return null;
-    }
-
-    @Override
-    public Map<String, Cookie> getCookies() {
-        return null;
-    }
-
-    @Override
-    public Date getDate() {
-        return null;
-    }
-
-    @Override
-    public int getLength() {
-        return 0;
-    }
-}
-- 
GitLab