|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0, which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * This Source Code may also be made available under the following Secondary |
| 9 | + * Licenses when the conditions for such availability set forth in the |
| 10 | + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, |
| 11 | + * version 2 with the GNU Classpath Exception, which is available at |
| 12 | + * https://www.gnu.org/software/classpath/license.html. |
| 13 | + * |
| 14 | + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| 15 | + */ |
| 16 | + |
| 17 | +package org.glassfish.jersey.media.multipart.internal; |
| 18 | + |
| 19 | +import jakarta.inject.Inject; |
| 20 | +import jakarta.ws.rs.WebApplicationException; |
| 21 | +import jakarta.ws.rs.core.Context; |
| 22 | +import jakarta.ws.rs.core.EntityPart; |
| 23 | +import jakarta.ws.rs.core.MediaType; |
| 24 | +import jakarta.ws.rs.core.MultivaluedMap; |
| 25 | +import jakarta.ws.rs.ext.MessageBodyReader; |
| 26 | +import jakarta.ws.rs.ext.Providers; |
| 27 | +import org.glassfish.jersey.media.multipart.BodyPart; |
| 28 | +import org.glassfish.jersey.media.multipart.FormDataBodyPart; |
| 29 | +import org.glassfish.jersey.media.multipart.JerseyEntityPartBuilderProvider; |
| 30 | +import org.glassfish.jersey.media.multipart.MultiPart; |
| 31 | + |
| 32 | +import java.io.IOException; |
| 33 | +import java.io.InputStream; |
| 34 | +import java.lang.annotation.Annotation; |
| 35 | +import java.lang.reflect.Type; |
| 36 | +import java.util.LinkedList; |
| 37 | +import java.util.List; |
| 38 | + |
| 39 | +public class SingleEntityPartReader implements MessageBodyReader<EntityPart> { |
| 40 | + |
| 41 | + private MultiPartReaderClientSide multiPartReaderClientSide; |
| 42 | + |
| 43 | + private final Providers providers; |
| 44 | + |
| 45 | + @Inject |
| 46 | + public SingleEntityPartReader(@Context Providers providers) { |
| 47 | + this.providers = providers; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public boolean isReadable(Class<?> type, Type generic, Annotation[] annotations, MediaType mediaType) { |
| 52 | + return EntityPart.class.isAssignableFrom(type); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public EntityPart readFrom(Class<EntityPart> type, Type genericType, Annotation[] annotations, |
| 57 | + MediaType mediaType, MultivaluedMap<String, String> httpHeaders, |
| 58 | + InputStream entityStream) throws IOException, WebApplicationException { |
| 59 | + |
| 60 | + if (multiPartReaderClientSide == null) { |
| 61 | + multiPartReaderClientSide = (MultiPartReaderClientSide) providers.getMessageBodyReader( |
| 62 | + MultiPart.class, MultiPart.class, new Annotation[0], MediaType.MULTIPART_FORM_DATA_TYPE); |
| 63 | + } |
| 64 | + |
| 65 | + final MultiPart multiPart = multiPartReaderClientSide.readFrom( |
| 66 | + MultiPart.class, MultiPart.class, annotations, mediaType, httpHeaders, entityStream); |
| 67 | + final List<BodyPart> bodyParts = multiPart.getBodyParts(); |
| 68 | + final List<EntityPart> entityParts = new LinkedList<>(); |
| 69 | + |
| 70 | + for (BodyPart bp : bodyParts) { |
| 71 | + if (FormDataBodyPart.class.isInstance(bp)) { |
| 72 | + entityParts.add((EntityPart) bp); |
| 73 | + } else { |
| 74 | + final EntityPart ep = new JerseyEntityPartBuilderProvider().withName("") |
| 75 | + .mediaType(bp.getMediaType()).content(bp.getEntity()).headers(bp.getHeaders()).build(); |
| 76 | + entityParts.add(ep); |
| 77 | + } |
| 78 | + // consume all bodyParts |
| 79 | + } |
| 80 | + |
| 81 | + return entityParts.get(0); |
| 82 | + } |
| 83 | +} |
0 commit comments