This is an automated email from the ASF dual-hosted git repository.
reta pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cxf.git
The following commit(s) were added to refs/heads/main by this push:
new f599b61e67 CXF-9113: EntityPart.Builder does not seem to work in a
Java SE environment (#2404)
f599b61e67 is described below
commit f599b61e67b0cc9661b9ccf72ce2976af72b1ff7
Author: Andriy Redko <[email protected]>
AuthorDate: Thu May 15 18:41:04 2025 -0400
CXF-9113: EntityPart.Builder does not seem to work in a Java SE environment
(#2404)
---
.../cxf/jaxrs/impl/EntityPartBuilderImpl.java | 14 ++++-
.../apache/cxf/jaxrs/impl/EntityPartImplTest.java | 59 ++++++++++++++++++++++
2 files changed, 71 insertions(+), 2 deletions(-)
diff --git
a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/EntityPartBuilderImpl.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/EntityPartBuilderImpl.java
index c5d0e48afe..e95c22953b 100644
---
a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/EntityPartBuilderImpl.java
+++
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/EntityPartBuilderImpl.java
@@ -33,10 +33,12 @@ import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.ext.MessageBodyWriter;
+import org.apache.cxf.BusFactory;
import org.apache.cxf.jaxrs.provider.ProviderFactory;
import org.apache.cxf.jaxrs.provider.ServerProviderFactory;
import org.apache.cxf.jaxrs.utils.JAXRSUtils;
import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageImpl;
public class EntityPartBuilderImpl implements EntityPart.Builder {
private final String name;
@@ -107,8 +109,16 @@ public class EntityPartBuilderImpl implements
EntityPart.Builder {
@Override
public EntityPart build() throws IllegalStateException, IOException,
WebApplicationException {
final MediaType mt = Objects.requireNonNullElse(mediaType,
MediaType.APPLICATION_OCTET_STREAM_TYPE);
- final Message message = JAXRSUtils.getCurrentMessage();
- final ProviderFactory factory =
ServerProviderFactory.getInstance(message);
+
+ Message message = JAXRSUtils.getCurrentMessage();
+ ProviderFactory factory = null;
+
+ if (message == null) {
+ message = new MessageImpl();
+ factory =
ServerProviderFactory.createInstance(BusFactory.getThreadDefaultBus());
+ } else {
+ factory = ProviderFactory.getInstance(message);
+ }
if (genericType != null) {
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
diff --git
a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/EntityPartImplTest.java
b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/EntityPartImplTest.java
new file mode 100644
index 0000000000..ea11efb00e
--- /dev/null
+++
b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/EntityPartImplTest.java
@@ -0,0 +1,59 @@
+/**
+ * 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.cxf.jaxrs.impl;
+
+import java.io.IOException;
+
+import jakarta.ws.rs.WebApplicationException;
+import jakarta.ws.rs.core.EntityPart;
+import jakarta.ws.rs.core.GenericType;
+import jakarta.ws.rs.core.MediaType;
+
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+public class EntityPartImplTest {
+ @Test
+ public void testCreateEntityPart() throws WebApplicationException,
IOException {
+ final EntityPart entityPart = EntityPart
+ .withName("greeting")
+ .content("hello")
+ .build();
+ assertThat(entityPart, is(not(nullValue())));
+ assertThat(entityPart.getContent(), is(not(nullValue())));
+ }
+
+ @Test
+ public void testCreateGenericEntityPart() throws WebApplicationException,
IOException {
+ final String content = "hello";
+ @SuppressWarnings("unchecked")
+ final EntityPart entityPart = EntityPart
+ .withName("greeting")
+ .mediaType(MediaType.APPLICATION_SVG_XML_TYPE)
+ .content(content, GenericType.forInstance(content))
+ .build();
+ assertThat(entityPart, is(not(nullValue())));
+ assertThat(entityPart.getContent(), is(not(nullValue())));
+ }
+}