Skip to content

Commit

Permalink
Remove dependency on optional dependency org.brotli:dec
Browse files Browse the repository at this point in the history
Fixes #6777
  • Loading branch information
jamesnetherton committed Nov 13, 2024
1 parent 9a1b3f7 commit d401d13
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,24 @@
*/
package org.apache.camel.quarkus.support.httpclient5.deployment;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Modifier;

import io.quarkus.deployment.GeneratedClassGizmoAdaptor;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.GeneratedClassBuildItem;
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem;
import io.quarkus.deployment.builditem.nativeimage.RuntimeInitializedClassBuildItem;
import io.quarkus.deployment.pkg.steps.NativeOrNativeSourcesBuild;
import io.quarkus.gizmo.ClassCreator;
import io.quarkus.gizmo.MethodCreator;
import io.quarkus.gizmo.MethodDescriptor;
import org.apache.camel.quarkus.support.httpclient5.graal.BrotliAbsentBooleanSupplier;

class HttpClient5Processor {
private static final String BROTLI_INPUT_STREAM_CLASS_NAME = "org.brotli.dec.BrotliInputStream";
private static final String NTLM_ENGINE_IMPL = "org.apache.hc.client5.http.impl.auth.NTLMEngineImpl";

@BuildStep
Expand All @@ -34,4 +46,54 @@ NativeImageResourceBuildItem suffixListResource() {
void runtimeInitializedClasses(BuildProducer<RuntimeInitializedClassBuildItem> runtimeInitializedClasses) {
runtimeInitializedClasses.produce(new RuntimeInitializedClassBuildItem(NTLM_ENGINE_IMPL));
}

@BuildStep(onlyIf = { NativeOrNativeSourcesBuild.class, BrotliAbsentBooleanSupplier.class })
void generateBrotliInputStreamClass(BuildProducer<GeneratedClassBuildItem> generatedClass) {
try (ClassCreator classCreator = ClassCreator.builder()
.className(BROTLI_INPUT_STREAM_CLASS_NAME)
.superClass(InputStream.class)
.classOutput(new GeneratedClassGizmoAdaptor(generatedClass, false))
.build()) {

/*
* Creates a simplified impl of BrotliInputStream to satisfy the native compiler:
*
* public class BrotliInputStream extends InputStream {
* public BrotliInputStream() {
* }
*
* public BrotliInputStream(InputStream stream) {
* }
*
* public int read() {
* throw new UnsupportedOperationException();
* }
* }
*/

try (MethodCreator defaultConstructor = classCreator.getMethodCreator("<init>", void.class)) {
defaultConstructor.setModifiers(Modifier.PUBLIC);
defaultConstructor.invokeSpecialMethod(
MethodDescriptor.ofMethod(BROTLI_INPUT_STREAM_CLASS_NAME, "<init>", void.class),
defaultConstructor.getThis());
defaultConstructor.returnValue(null);
}

try (MethodCreator constructorWithInputStreamArg = classCreator.getMethodCreator("<init>", void.class,
InputStream.class)) {
constructorWithInputStreamArg.setModifiers(Modifier.PUBLIC);
constructorWithInputStreamArg.invokeSpecialMethod(
MethodDescriptor.ofMethod(BROTLI_INPUT_STREAM_CLASS_NAME, "<init>", void.class),
constructorWithInputStreamArg.getThis());
constructorWithInputStreamArg.returnValue(null);
}

try (MethodCreator readMethod = classCreator.getMethodCreator("read", int.class)) {
readMethod.setModifiers(Modifier.PUBLIC);
readMethod.addException(IOException.class);
readMethod.throwException(UnsupportedOperationException.class,
"Cannot read from BrotliInputStream. Add org.brotli:dec to the application classpath");
}
}
}
}
5 changes: 3 additions & 2 deletions extensions-support/httpclient5/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@
<artifactId>httpclient5</artifactId>
</dependency>
<dependency>
<groupId>org.brotli</groupId>
<artifactId>dec</artifactId>
<groupId>org.graalvm.sdk</groupId>
<artifactId>nativeimage</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.camel.quarkus.support.httpclient5.graal;

import java.util.function.BooleanSupplier;

public class BrotliAbsentBooleanSupplier implements BooleanSupplier {
@Override
public boolean getAsBoolean() {
try {
Thread.currentThread().getContextClassLoader().loadClass("org.brotli.dec.BrotliInputStream");
return false;
} catch (ClassNotFoundException e) {
return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.camel.quarkus.support.httpclient5.graal;

import java.io.IOException;
import java.io.InputStream;

import com.oracle.svm.core.annotate.Substitute;
import com.oracle.svm.core.annotate.TargetClass;
import org.apache.hc.client5.http.entity.BrotliInputStreamFactory;

/**
* Remove references to optional brotli:dec dependency.
*/
@TargetClass(value = BrotliInputStreamFactory.class, onlyWith = { BrotliAbsentBooleanSupplier.class })
public final class BrotliInputStreamFactorySubstitutions {
@Substitute
public InputStream create(InputStream inputStream) throws IOException {
throw new UnsupportedOperationException(
"Cannot create BrotliInputStream. Add org.brotli:dec to the application classpath.");
}
}
4 changes: 4 additions & 0 deletions extensions/saxon/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-core-deployment</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-support-httpclient5-deployment</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-saxon</artifactId>
Expand Down
8 changes: 4 additions & 4 deletions extensions/saxon/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@
<artifactId>camel-quarkus-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-saxon</artifactId>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-support-httpclient5</artifactId>
</dependency>
<dependency>
<groupId>org.brotli</groupId>
<artifactId>dec</artifactId>
<groupId>org.apache.camel</groupId>
<artifactId>camel-saxon</artifactId>
</dependency>
</dependencies>

Expand Down
4 changes: 4 additions & 0 deletions extensions/xslt-saxon/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-xslt-deployment</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-support-httpclient5-deployment</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-xslt-saxon</artifactId>
Expand Down
13 changes: 4 additions & 9 deletions extensions/xslt-saxon/runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,12 @@
<artifactId>camel-quarkus-xslt</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-xslt-saxon</artifactId>
</dependency>
<dependency>
<groupId>org.brotli</groupId>
<artifactId>dec</artifactId>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-support-httpclient5</artifactId>
</dependency>
<!-- Required for the native build of the simple app with this extension only -->
<dependency>
<groupId>org.apache.httpcomponents.core5</groupId>
<artifactId>httpcore5</artifactId>
<groupId>org.apache.camel</groupId>
<artifactId>camel-xslt-saxon</artifactId>
</dependency>
</dependencies>

Expand Down
5 changes: 0 additions & 5 deletions poms/bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7237,11 +7237,6 @@
<artifactId>bcutil-jdk18on</artifactId>
<version>${bouncycastle.version}</version>
</dependency>
<dependency>
<groupId>org.brotli</groupId>
<artifactId>dec</artifactId>
<version>${brotli.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.angus</groupId>
<artifactId>angus-mail</artifactId>
Expand Down
5 changes: 0 additions & 5 deletions poms/bom/src/main/generated/flattened-full-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7159,11 +7159,6 @@
<artifactId>bcutil-jdk18on</artifactId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
<version>1.79</version><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
</dependency>
<dependency>
<groupId>org.brotli</groupId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
<artifactId>dec</artifactId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
<version>0.1.2</version><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
</dependency>
<dependency>
<groupId>org.eclipse.angus</groupId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
<artifactId>angus-mail</artifactId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
Expand Down
5 changes: 0 additions & 5 deletions poms/bom/src/main/generated/flattened-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7109,11 +7109,6 @@
<artifactId>bcutil-jdk18on</artifactId>
<version>1.79</version>
</dependency>
<dependency>
<groupId>org.brotli</groupId>
<artifactId>dec</artifactId>
<version>0.1.2</version>
</dependency>
<dependency>
<groupId>org.eclipse.angus</groupId>
<artifactId>angus-mail</artifactId>
Expand Down
5 changes: 0 additions & 5 deletions poms/bom/src/main/generated/flattened-reduced-verbose-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7109,11 +7109,6 @@
<artifactId>bcutil-jdk18on</artifactId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
<version>1.79</version><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
</dependency>
<dependency>
<groupId>org.brotli</groupId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
<artifactId>dec</artifactId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
<version>0.1.2</version><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
</dependency>
<dependency>
<groupId>org.eclipse.angus</groupId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
<artifactId>angus-mail</artifactId><!-- org.apache.camel.quarkus:camel-quarkus-bom:${project.version} -->
Expand Down

0 comments on commit d401d13

Please sign in to comment.