-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhances extension compatibility with file upload changes from vaadin/hilla#3165. Updates EndpointController to utilize a custom HttpServletRequest, ensuring seamless integration with Quarkus Multipart Form data handling.
- Loading branch information
1 parent
5e2a573
commit 90d6b6b
Showing
8 changed files
with
396 additions
and
24 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
...in/java/com/github/mcollovati/quarkus/hilla/deployment/asm/EndpointControllerVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2025 Marco Collovati, Dario Götze | ||
* | ||
* Licensed 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 com.github.mcollovati.quarkus.hilla.deployment.asm; | ||
|
||
import io.quarkus.gizmo.Gizmo; | ||
import org.objectweb.asm.ClassVisitor; | ||
import org.objectweb.asm.MethodVisitor; | ||
import org.objectweb.asm.Opcodes; | ||
import org.objectweb.asm.tree.MethodInsnNode; | ||
import org.objectweb.asm.tree.MethodNode; | ||
import org.objectweb.asm.tree.TypeInsnNode; | ||
|
||
public class EndpointControllerVisitor extends ClassVisitor { | ||
|
||
public static final String SPRING_MULTIPART_HTTP_SERVLET_REQUEST = | ||
"org/springframework/web/multipart/MultipartHttpServletRequest"; | ||
public static final String QH_MULTIPART_HTTP_SERVLET_REQUEST = | ||
"com/github/mcollovati/quarkus/hilla/multipart/MultipartRequest"; | ||
|
||
public EndpointControllerVisitor(ClassVisitor classVisitor) { | ||
super(Gizmo.ASM_API_VERSION, classVisitor); | ||
} | ||
|
||
@Override | ||
public MethodVisitor visitMethod( | ||
int access, String name, String descriptor, String signature, String[] exceptions) { | ||
MethodVisitor superVisitor = super.visitMethod(access, name, descriptor, signature, exceptions); | ||
if ("doServeEndpoint".equals(name)) { | ||
return new MethodNode(Gizmo.ASM_API_VERSION, access, name, descriptor, signature, exceptions) { | ||
@Override | ||
public void visitEnd() { | ||
var iterator = instructions.iterator(); | ||
TypeInsnNode checkCastNode = AsmUtils.findNextInsnNode( | ||
iterator, | ||
node -> node.getOpcode() == Opcodes.CHECKCAST | ||
&& node.desc.equals(SPRING_MULTIPART_HTTP_SERVLET_REQUEST), | ||
TypeInsnNode.class); | ||
checkCastNode.desc = QH_MULTIPART_HTTP_SERVLET_REQUEST; | ||
MethodInsnNode getParameterNode = AsmUtils.findNextInsnNode( | ||
iterator, | ||
node -> node.getOpcode() == Opcodes.INVOKEINTERFACE | ||
&& node.owner.equals(SPRING_MULTIPART_HTTP_SERVLET_REQUEST) | ||
&& node.name.equals("getParameter"), | ||
MethodInsnNode.class); | ||
getParameterNode.setOpcode(Opcodes.INVOKEVIRTUAL); | ||
getParameterNode.owner = QH_MULTIPART_HTTP_SERVLET_REQUEST; | ||
getParameterNode.itf = false; | ||
|
||
MethodInsnNode getFileMapNode = AsmUtils.findNextInsnNode( | ||
iterator, | ||
node -> node.getOpcode() == Opcodes.INVOKEINTERFACE | ||
&& node.owner.equals(SPRING_MULTIPART_HTTP_SERVLET_REQUEST) | ||
&& node.name.equals("getFileMap"), | ||
MethodInsnNode.class); | ||
getFileMapNode.setOpcode(Opcodes.INVOKEVIRTUAL); | ||
getFileMapNode.owner = QH_MULTIPART_HTTP_SERVLET_REQUEST; | ||
getFileMapNode.itf = false; | ||
accept(superVisitor); | ||
} | ||
}; | ||
} | ||
return superVisitor; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
...runtime/src/main/java/com/github/mcollovati/quarkus/hilla/multipart/MultipartRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
* Copyright 2025 Marco Collovati, Dario Götze | ||
* | ||
* Licensed 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 com.github.mcollovati.quarkus.hilla.multipart; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletRequestWrapper; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.Serializable; | ||
import java.io.UncheckedIOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Collection; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
import org.jboss.resteasy.reactive.server.multipart.FormValue; | ||
import org.jboss.resteasy.reactive.server.multipart.MultipartFormDataInput; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
public class MultipartRequest extends HttpServletRequestWrapper { | ||
|
||
private Map<String, Collection<FormValue>> formData; | ||
|
||
/** | ||
* Constructs a request object wrapping the given request. | ||
* | ||
* @param request the {@link HttpServletRequest} to be wrapped. | ||
* @throws IllegalArgumentException if the request is null | ||
*/ | ||
public MultipartRequest(HttpServletRequest request, MultipartFormDataInput formData) { | ||
super(request); | ||
this.formData = formData.getValues(); | ||
} | ||
|
||
@Override | ||
public String getParameter(String name) { | ||
Collection<FormValue> values = formData.get(name); | ||
return Optional.ofNullable(values).stream() | ||
.flatMap(Collection::stream) | ||
.filter(fv -> !fv.isFileItem()) | ||
.findFirst() | ||
.map(FormValue::getValue) | ||
.orElseGet(() -> super.getParameter(name)); | ||
} | ||
|
||
public Map<String, MultipartFile> getFileMap() { | ||
return formData.entrySet().stream() | ||
.map(e -> Map.entry(e.getKey(), e.getValue().iterator().next())) | ||
.filter(e -> e.getValue().isFileItem()) | ||
.map(e -> new MultipartFileImpl(e.getKey(), e.getValue())) | ||
.collect(Collectors.toMap(MultipartFileImpl::getName, Function.identity())); | ||
} | ||
|
||
private static class MultipartFileImpl implements MultipartFile, Serializable { | ||
|
||
private final String name; | ||
private final FormValue formValue; | ||
|
||
public MultipartFileImpl(String name, FormValue formValue) { | ||
this.name = name; | ||
this.formValue = formValue; | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public String getOriginalFilename() { | ||
return formValue.getFileName(); | ||
} | ||
|
||
@Override | ||
public String getContentType() { | ||
return formValue.getHeaders().getFirst(HttpHeaders.CONTENT_TYPE); | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return getSize() <= 0L; | ||
} | ||
|
||
@Override | ||
public long getSize() { | ||
try { | ||
return formValue.getFileItem().getFileSize(); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public byte[] getBytes() throws IOException { | ||
return formValue.getFileItem().getInputStream().readAllBytes(); | ||
} | ||
|
||
@Override | ||
public InputStream getInputStream() throws IOException { | ||
return formValue.getFileItem().getInputStream(); | ||
} | ||
|
||
@Override | ||
public void transferTo(File dest) throws IOException, IllegalStateException { | ||
transferTo(dest.toPath()); | ||
} | ||
|
||
@Override | ||
public void transferTo(Path dest) throws IOException, IllegalStateException { | ||
if (dest.toFile().exists()) { | ||
Files.delete(dest); | ||
} | ||
formValue.getFileItem().write(dest); | ||
} | ||
} | ||
} |
Oops, something went wrong.