<?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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>io.github.atkawa7</groupId>
    <artifactId>polly-http</artifactId>
    <version>0.0.21</version>

    <name>polly-http</name>
    <description>polly-http</description>
    <url>https://github.com/atkawa7/polly-http</url>
    <licenses>
        <license>
            <name>Apache License</name>
            <url>https://raw.githubusercontent.com/atkawa7/polly-http/LICENSE.txt</url>
            <distribution>https://github.com/atkawa7/polly-http</distribution>
        </license>
    </licenses>

    <developers>
        <developer>
            <name>atkawa7</name>
            <email>atkawa7@yahoo.com</email>
            <organization>atkawa7</organization>
            <organizationUrl>https://github.com/atkawa7</organizationUrl>
        </developer>
    </developers>

    <scm>
        <connection>scm:git:git://github.com/atkawa7/polly-http.git</connection>
        <developerConnection>scm:git:git@github.com:atkawa7/polly-http.git</developerConnection>
        <tag>polly-http-0.0.21</tag>
        <url>https://github.com/atkawa7/polly-http</url>
    </scm>

    <distributionManagement>
        <repository>
            <id>ossrh</id>
            <name>Central Repository OSSRH</name>
            <url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
        </repository>
    </distributionManagement>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.38</version>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.36</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.36</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.19.2</version>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>4.12.0</version>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents.client5</groupId>
            <artifactId>httpclient5</artifactId>
            <version>5.5</version>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.jqwik</groupId>
            <artifactId>jqwik</artifactId>
            <version>1.9.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <version>3.27.6</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.code-intelligence</groupId>
            <artifactId>jazzer-junit</artifactId>
            <version>0.24.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>aws-core</artifactId>
            <version>2.34.5</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>s3</artifactId>
            <version>2.34.5</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>oauth.signpost</groupId>
            <artifactId>signpost-core</artifactId>
            <version>2.1.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.github.scribejava</groupId>
            <artifactId>scribejava-core</artifactId>
            <version>8.3.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.tomitribe</groupId>
            <artifactId>tomitribe-http-signatures</artifactId>
            <version>1.8</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.jalg</groupId>
            <artifactId>hawkj</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>3.4.0</version>
                <executions>
                    <execution>
                        <id>regex-property</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>regex-property</goal>
                        </goals>
                        <configuration>
                            <name>groupId.path</name>
                            <value>${project.groupId}</value>
                            <regex>\.</regex>
                            <replacement>/</replacement>
                            <failIfNoMatch>true</failIfNoMatch>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar-no-fork</goal>
                        </goals>
                        <phase>deploy</phase>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>3.0.0-M1</version>
                <executions>
                    <execution>
                        <id>deploy</id>
                        <goals>
                            <goal>deploy</goal>
                        </goals>
                        <phase>deploy</phase>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <source>8</source>
                    <detectJavaApiLink>false</detectJavaApiLink>
                    <additionalJOption>-Xdoclint:none</additionalJOption>
                </configuration>
                <executions>
                    <execution>
                        <id>attach-javadocs</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <phase>deploy</phase>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
                <version>3.0.0-M4</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.11.0</version>
                <executions>
                    <execution>
                        <id>default-compile</id>
                        <phase>compile</phase>
                        <goals><goal>compile</goal></goals>
                        <configuration>
                            <release>8</release>
                        </configuration>
                    </execution>
                    <execution>
                        <id>java-11</id>
                        <phase>compile</phase>
                        <goals><goal>compile</goal></goals>
                        <configuration>
                            <release>11</release>
                            <compileSourceRoots>
                                <compileSourceRoot>${project.basedir}/src/main/java11</compileSourceRoot>
                            </compileSourceRoots>
                            <multiReleaseOutput>true</multiReleaseOutput>
                        </configuration>
                    </execution>
                    <execution>
                        <id>default-testCompile</id>
                        <phase>test-compile</phase>
                        <goals><goal>testCompile</goal></goals>
                        <configuration>
                            <compileSourceRoots>
                                <compileSourceRoot>${project.basedir}/src/test/java</compileSourceRoot>
                                <compileSourceRoot>${project.basedir}/src/main/java11</compileSourceRoot>
                            </compileSourceRoots>
                            <release>11</release>
                        </configuration>
                    </execution>
                </executions>

            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <archive>
                        <manifestEntries>
                            <Multi-Release>true</Multi-Release>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-gpg-plugin</artifactId>
                <version>1.6</version>
                <configuration>
                    <gpgArguments>
                        <arg>--pinentry-mode</arg>
                        <arg>loopback</arg>
                    </gpgArguments>
                </configuration>
                <executions>
                    <execution>
                        <id>sign-artifacts</id>
                        <goals>
                            <goal>sign</goal>
                        </goals>
                        <phase>verify</phase>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <id>hash-artifacts</id>
                        <phase>verify</phase>
                        <configuration>
                            <target>
                                <checksum fileext=".sha256" algorithm="SHA-256">
                                    <fileset dir="${project.build.directory}">
                                        <include name="*.jar" />
                                        <include name="*.pom" />
                                    </fileset>
                                </checksum>
                                <checksum fileext=".md5" algorithm="MD5">
                                    <fileset dir="${project.build.directory}">
                                        <include name="*.jar" />
                                        <include name="*.pom" />
                                    </fileset>
                                </checksum>
                                <checksum fileext=".sha1" algorithm="SHA-1">
                                    <fileset dir="${project.build.directory}">
                                        <include name="*.jar" />
                                        <include name="*.pom" />
                                    </fileset>
                                </checksum>
                            </target>
                        </configuration>
                        <goals>
                            <goal>run</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.5.0</version>
                <executions>
                    <execution>
                        <id>make-project-zip</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <finalName>${project.artifactId}-${project.version}</finalName>
                            <appendAssemblyId>false</appendAssemblyId>
                            <descriptors>
                                <descriptor>src/assembly/central-upload.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.gmavenplus</groupId>
                <artifactId>gmavenplus-plugin</artifactId>
                <version>3.0.2</version>
                <executions>
                    <execution>
                        <id>upload-bundle</id>
                        <phase>none</phase>
                        <goals>
                            <goal>execute</goal>
                        </goals>
                        <configuration>
                            <scripts>
                                <script>import java.net.HttpURLConnection
                                    import java.net.URL
                                    import java.nio.file.Files
                                    import java.nio.file.Paths
                                    import java.util.Base64
                                    import java.io.OutputStreamWriter
                                    import java.security.MessageDigest

                                    // ----------------------
                                    // Helper: mask secrets
                                    // ----------------------
                                    def maskSecret = { value, visibleStart = 2, visibleEnd = 2 -&gt;
                                    if (value == null || value.length() &lt;= visibleStart + visibleEnd) {
                                    return "*" * (value?.length() ?: 0)
                                    }
                                    value.substring(0, visibleStart) + ("*" * (value.length() - visibleStart - visibleEnd)) + value.substring(value.length() - visibleEnd)
                                    }

                                    // ----------------------
                                    // Helper: strip ${env. ... } syntax
                                    // ----------------------
                                    def stripEnvSyntax = { value -&gt;
                                    if (value == null) return null
                                    value = value.trim()
                                    if (value.startsWith('${env.') &amp;&amp; value.endsWith('}')) {
                                    return value.substring(6, value.length() - 1)
                                    }
                                    return value
                                    }

                                    // ----------------------
                                    // Paths &amp; artifact info
                                    // ----------------------
                                    def artifactId = project.artifactId
                                    def version = project.version
                                    def zipPath = Paths.get(project.basedir.toString())
                                    .resolve("target")
                                    .resolve("${artifactId}-${version}.zip")

                                    if (!Files.exists(zipPath)) throw new FileNotFoundException(zipPath.toString())
                                    println "Found ZIP: ${zipPath}"

                                    // ----------------------
                                    // Load credentials
                                    // ----------------------
                                    def server = session.settings.getServer("ossrh")
                                    if (!server) throw new RuntimeException("Missing &lt;server&gt; with id='ossrh' in settings.xml")

                                    // Strip env syntax if needed
                                    def username = stripEnvSyntax(server.username)?.trim()
                                    def password = stripEnvSyntax(server.password)?.trim()

                                    if (!username || !password) throw new RuntimeException("Username or password is empty")

                                    println "Username: ${maskSecret(username, visibleStart = 1, visibleEnd = 1)}"
                                    println "Password: ${maskSecret(password)}"
                                    def credentials = "${username}:${password}"
                                    // ----------------------
                                    // Prepare multipart request
                                    // ----------------------
                                    def boundary = "----Boundary${System.currentTimeMillis()}"
                                    def lineEnd = "\r\n"
                                    def twoHyphens = "--"

                                    def url = new URL("https://central.sonatype.com/api/v1/publisher/upload")
                                    def connection = (HttpURLConnection) url.openConnection()
                                    connection.setDoOutput(true)
                                    connection.setRequestMethod("POST")

                                    // Base64 encode username:password for Bearer token
                                    def base64Token = credentials.bytes.encodeBase64().toString()
                                    connection.setRequestProperty("Authorization", "Bearer ${base64Token}")
                                    connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=${boundary}")

                                    def output = connection.outputStream
                                    def writer = new OutputStreamWriter(output, "UTF-8")

                                    // -- bundle (zip file)
                                    writer.write(twoHyphens + boundary + lineEnd)
                                    writer.write("Content-Disposition: form-data; name=\"bundle\"; filename=\"${zipPath.fileName}\"" + lineEnd)
                                    writer.write("Content-Type: application/zip" + lineEnd + lineEnd)
                                    writer.flush()
                                    Files.copy(zipPath, output)
                                    output.flush()
                                    writer.write(lineEnd)

                                    // -- name field
                                    writer.write(twoHyphens + boundary + lineEnd)
                                    writer.write("Content-Disposition: form-data; name=\"name\"" + lineEnd + lineEnd)
                                    writer.write("${artifactId}-${version}" + lineEnd)

                                    // -- publishingType field
                                    writer.write(twoHyphens + boundary + lineEnd)
                                    writer.write("Content-Disposition: form-data; name=\"publishingType\"" + lineEnd + lineEnd)
                                    writer.write("USER_MANAGED" + lineEnd)

                                    // -- end boundary
                                    writer.write(twoHyphens + boundary + twoHyphens + lineEnd)
                                    writer.flush()
                                    writer.close()

                                    // ----------------------
                                    // Execute request
                                    // ----------------------
                                    def responseCode = connection.responseCode
                                    def responseBody = responseCode &gt;= 200 &amp;&amp; responseCode &lt; 300 ? connection.inputStream.text : connection.errorStream.text

                                    println "Response Code: ${responseCode}"
                                    println "Response Body: ${responseBody}"

                                    if (responseCode &lt; 200 || responseCode &gt;= 300) {
                                    throw new RuntimeException("Upload failed with HTTP ${responseCode}: ${responseBody}")
                                    }

                                    connection.disconnect()
                                    println "Upload successful!"
                                </script>
                            </scripts>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy</artifactId>
                        <version>3.0.25</version>
                    </dependency>
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-ant</artifactId>
                        <version>3.0.25</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
</project>
