<?xml version="1.0"?>
<!--
  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.
-->
<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>
    <parent>
        <groupId>org.apache.cxf.fediz</groupId>
        <artifactId>fediz</artifactId>
        <version>1.4.2</version>
        <relativePath>../../pom.xml</relativePath>
    </parent>
    <artifactId>fediz-idp</artifactId>
    <name>Apache Fediz IDP</name>
    <packaging>war</packaging>
    
    <properties>
        <swagger-ui.version>2.2.6</swagger-ui.version>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf.fediz</groupId>
            <artifactId>fediz-idp-core</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-rs-service-description-swagger</artifactId>
            <version>${cxf.version}</version>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/realm.properties</include>
                    <include>**/persistence.properties</include>
                    <include>**/web.xml</include>
                    <include>**/restContext.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>false</filtering>
                <excludes>
                    <exclude>**/realm.properties</exclude>
                    <exclude>**/persistence.properties</exclude>
                    <exclude>**/web.xml</exclude>
                    <exclude>**/restContext.xml</exclude>
                </excludes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <!--for mvn tomcat:deploy/:undeploy/:redeploy -->
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>tomcat-maven-plugin</artifactId>
                <version>1.1</version>
                <configuration>
                    <server>myTomcat</server>
                    <url>http://localhost:9080/manager/text</url>
                    <path>/${project.build.finalName}</path>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <webResources>
                        <resource>
                            <directory>src/main/webapp</directory>
                            <filtering>true</filtering>
                            <includes>
                                <include>**/applicationContext.xml</include>
                                <include>**/web.xml</include>
								<include>**/restContext.xml</include>
                            </includes>
                        </resource>
                        <resource>
                            <directory>src/main/webapp</directory>
                            <filtering>false</filtering>
                            <excludes>
                                <exclude>**/applicationContext.xml</exclude>
                                <exclude>**/web.xml</exclude>
								<exclude>**/restContext.xml</exclude>
                            </excludes>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>org.webjars</groupId>
                                    <artifactId>swagger-ui</artifactId>
                                    <version>${swagger-ui.version}</version>
                                    <overWrite>true</overWrite>
                                    <outputDirectory>${project.build.directory}/swagger-ui</outputDirectory>
                                    <excludes>**/*.gz</excludes>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-swagger-resources-in-place</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/${project.build.finalName}/resources/swagger</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${project.build.directory}/swagger-ui/META-INF/resources/webjars/swagger-ui/${swagger-ui.version}</directory>
                                    <excludes>
                                        <exclude>index.html</exclude>
                                        <exclude>swagger-ui.min.js</exclude>
                                    </excludes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <inherited>true</inherited>
                <executions>
                    <execution>
                        <id>addMatrixParamSupport</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target>
                                <replace file="${project.build.directory}/swagger-ui/META-INF/resources/webjars/swagger-ui/${swagger-ui.version}/swagger-ui.js" token="return url + requestUrl + querystring;" value="&#xA;var matrixstring = '';&#xA; for (var i = 0; i &lt; this.parameters.length; i++) {&#xA; var param = this.parameters[i];&#xA; &#xA; if (param.in === 'matrix') {&#xA; matrixstring += ';' + this.encodeQueryParam(param.name) + '=' + this.encodeQueryParam(args[param.name]);&#xA; }&#xA; }&#xA; &#xA; var url = this.scheme + '://' + this.host;&#xA; &#xA; if (this.basePath !== '/') {&#xA; url += this.basePath;&#xA;   }&#xA;   return url + requestUrl + matrixstring + querystring;" />
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>reserve-network-port</id>
                        <goals>
                            <goal>reserve-network-port</goal>
                        </goals>
                        <phase>initialize</phase>
                        <configuration>
                            <portNames>
                                <portName>idp.https.port</portName>
                                <portName>idp.http.port</portName>
                                <portName>idp.stop.port</portName>
                            </portNames>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>properties-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>set-tomcat-properties</id>
                        <goals>
                            <goal>set-system-properties</goal>
                        </goals>
                        <configuration>
                            <properties>
                                <property>
                                    <name>catalina.base</name>
                                    <value>target</value>
                                </property>
                            </properties>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>8.1.16.v20140903</version>
                <configuration>
                    <skip>${skipTests}</skip>
                    <stopPort>${idp.stop.port}</stopPort>
                    <stopKey>STOP</stopKey>
                    <connectors>
                        <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                            <port>${idp.http.port}</port>
                            <maxIdleTime>30000</maxIdleTime>
                        </connector>
                        <connector implementation="org.eclipse.jetty.server.ssl.SslSelectChannelConnector">
                            <port>${idp.https.port}</port>
                            <maxIdleTime>30000</maxIdleTime>
                            <keystore>${basedir}/../../examples/samplekeys/idp-ssl-key.jks</keystore>
                            <keyPassword>tompass</keyPassword>
                            <password>tompass</password>
                        </connector>
                    </connectors>
                    <contextHandlers>
                        <contextHandler implementation="org.eclipse.jetty.webapp.WebAppContext">
                            <war>${basedir}/target/fediz-idp-sts.war</war>
                            <contextPath>/fediz-idp-sts</contextPath>
                            <throwUnavailableOnStartupException>false</throwUnavailableOnStartupException>
                        </contextHandler>
                        <contextHandler implementation="org.eclipse.jetty.webapp.WebAppContext">
                            <war>${basedir}/target/fediz-idp.war</war>
                            <contextPath>/fediz-idp</contextPath>
                        </contextHandler>
                    </contextHandlers>
                </configuration>
                <executions>
                    <execution>
                        <id>start-jetty</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>run-war</goal>
                        </goals>
                        <configuration>
                            <scanIntervalSeconds>0</scanIntervalSeconds>
                            <daemon>true</daemon>
                        </configuration>
                    </execution>
                    <execution>
                        <id>stop-jetty</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.hsqldb</groupId>
                        <artifactId>hsqldb</artifactId>
                        <version>${hsqldb.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <inherited>true</inherited>
                <executions>
                    <execution>
                        <id>integration-test</id>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>integration-test</goal>
                        </goals>
                        <configuration>
                            <skip>${skipTests}</skip>
                            <systemPropertyVariables>
                                <idp.https.port>${idp.https.port}</idp.https.port>
                                <realm>${realm}</realm>
                                <catalina.base>target</catalina.base>
                            </systemPropertyVariables>
                            <includes>
                                <include>**/integrationtests/**</include>
                            </includes>
                            <argLine>-XX:MaxPermSize=512M</argLine>
                        </configuration>
                    </execution>
                    <execution>
                        <id>verify</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <inherited>true</inherited>
                <configuration>
                    <excludes>
                        <exclude>**/integrationtests/**</exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
        <!-- Name of the generated WAR file -->
        <finalName>fediz-idp</finalName>
    </build>

    <profiles>
        <profile>
            <id>default</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <jpa.url>jdbc:hsqldb:target/db/realma/myDB;shutdown=true</jpa.url>
                <realm>realm-a</realm>
            </properties>
            <build>
                <filters>
                    <filter>src/main/filters/realm-a/env.properties</filter>
                </filters>
            </build>
        </profile>
        <profile>
            <id>realm-a</id>
            <properties>
                <jpa.url>jdbc:hsqldb:target/db/realma/myDB;shutdown=true</jpa.url>
                <idp.https.port>9443</idp.https.port>
                <idp.http.port>9080</idp.http.port>
                <realm>realm-a</realm>
            </properties>
            <build>
                <filters>
                    <filter>src/main/filters/realm-a/env.properties</filter>
                </filters>
                <plugins>
                    <plugin>
                        <groupId>org.mortbay.jetty</groupId>
                        <artifactId>jetty-maven-plugin</artifactId>
                        <configuration>
                            <contextHandlers>
                                <contextHandler implementation="org.eclipse.jetty.webapp.WebAppContext">
                                    <war>${basedir}/target/fediz-idp.war</war>
                                    <contextPath>/fediz-idp</contextPath>
                                </contextHandler>
                            </contextHandlers>
                         </configuration>
                     </plugin>
                 </plugins>
            </build>
        </profile>
        <profile>
            <id>realm-b</id>
            <properties>
                <jpa.url>jdbc:hsqldb:target/db/realmb/myDB;shutdown=true</jpa.url>
                <idp.https.port>12346</idp.https.port>
                <idp.http.port>12345</idp.http.port>
                <realm>realm-b</realm>
            </properties>
            <build>
                <filters>
                    <filter>src/main/filters/realm-b/env.properties</filter>
                </filters>
                <plugins>
                    <plugin>
                        <groupId>org.mortbay.jetty</groupId>
                        <artifactId>jetty-maven-plugin</artifactId>
                        <configuration>
                            <contextHandlers>
                                <contextHandler implementation="org.eclipse.jetty.webapp.WebAppContext">
                                    <war>${basedir}/target/fediz-idp-remote.war</war>
                                    <contextPath>/fediz-idp-remote</contextPath>
                                </contextHandler>
                            </contextHandlers>
                         </configuration>
                     </plugin>
                 </plugins>
                <finalName>fediz-idp-remote</finalName>
            </build>
        </profile>
        <profile>
            <id>standalone</id>
            <properties>
                <maven.test.skip>true</maven.test.skip>
            </properties>
            <build>
                <defaultGoal>jetty:run-war</defaultGoal>
            </build>
        </profile>

        <profile>
            <id>sts</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-dependency-plugin</artifactId>
                        <executions>
                            <execution>
                                <id>copy-sts</id>
                                <phase>generate-resources</phase>
                                <goals>
                                    <goal>copy</goal>
                                </goals>
                                <configuration>
                                    <artifactItems>
                                        <artifactItem>
                                            <groupId>org.apache.cxf.fediz</groupId>
                                            <artifactId>fediz-idp-sts</artifactId>
                                            <version>${project.version}</version>
                                            <type>war</type>
                                            <overWrite>true</overWrite>
                                            <outputDirectory>target/</outputDirectory>
                                        </artifactItem>
                                    </artifactItems>
                                    <outputAbsoluteArtifactFilename>true</outputAbsoluteArtifactFilename>
                                    <overWriteSnapshots>true</overWriteSnapshots>
                                    <overWriteIfNewer>true</overWriteIfNewer>
                                    <stripVersion>true</stripVersion>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.mortbay.jetty</groupId>
                        <artifactId>jetty-maven-plugin</artifactId>
                        <configuration>
                            <contextHandlers combine.children="append">
                                <contextHandler implementation="org.eclipse.jetty.webapp.WebAppContext">
                                    <war>${basedir}/target/fediz-idp-sts.war</war>
                                    <contextPath>/fediz-idp-sts</contextPath>
                                </contextHandler>
                            </contextHandlers>
                         </configuration>
                     </plugin>
                 </plugins>
            </build>
        </profile>
    </profiles>
</project>
