[ 
https://issues.apache.org/jira/browse/CXF-5404?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13827429#comment-13827429
 ] 

Dale Peakall commented on CXF-5404:
-----------------------------------

Sample code:

{code}
import org.apache.cxf.jaxrs.provider.atom.AtomPojoProvider;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.nio.charset.Charset;

import javax.ws.rs.ClientErrorException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * Example that demonstrates problem with AtomPojoProvider when using JAXB and 
response is missing content.
 */
public final class NoContentApp {
    /** A sample entry with content. */
    private static final String SAMPLE_ENTRY_WITH_CONTENT =
        "<?xml version='1.0' encoding='UTF-8'?>\n" +
            "<entry xmlns=\"http://www.w3.org/2005/Atom\";>\n" +
            "  <id>84297856</id>\n" +
            "  <content type=\"application/xml\">\n" +
            "    <jaxbDataType xmlns=\"\">\n" +
            "    </jaxbDataType>\n" +
            "  </content>\n" +
            "</entry>";


    /** A sample entry without content. */
    private static final String SAMPLE_ENTRY_WITHOUT_CONTENT =
        "<?xml version='1.0' encoding='UTF-8'?>\n" +
        "<entry xmlns=\"http://www.w3.org/2005/Atom\";>\n" +
        "  <id>84297856</id>\n" +
        "</entry>";

    /**
     * A sample JAXB data-type to read data into.
     */
    @XmlRootElement
    public static class JaxbDataType {
        // no data
    }

    /**
     * This class cannot be instantiated.
     */
    private NoContentApp() {
        // do nothing
    }

    /**
     * Main entry point.
     * @param args the command-line arguments: none expected.
     */
    public static void main(String[] args) throws IOException {
        AtomPojoProvider atomPojoProvider = new AtomPojoProvider();

        atomPojoProvider.readFrom((Class)JaxbDataType.class,
            JaxbDataType.class,
            new Annotation[0],
            MediaType.valueOf("application/atom+xml;type=entry"),
            new MultivaluedHashMap<String, String>(),
            new 
ByteArrayInputStream(SAMPLE_ENTRY_WITH_CONTENT.getBytes(Charset.forName("UTF-8"))));

        try {
            atomPojoProvider.readFrom((Class)JaxbDataType.class,
                JaxbDataType.class,
                new Annotation[0],
                MediaType.valueOf("application/atom+xml;type=entry"),
                new MultivaluedHashMap<String, String>(),
                new 
ByteArrayInputStream(SAMPLE_ENTRY_WITHOUT_CONTENT.getBytes(Charset.forName("UTF-8"))));
        } catch (ClientErrorException e) {
            if (e.getCause() instanceof NullPointerException) {
                System.out.println("This isn't very helpful!");
            }
        }
    }
}
{code}

> NPE in AtomPojoProvider when no content (or content is empty)
> -------------------------------------------------------------
>
>                 Key: CXF-5404
>                 URL: https://issues.apache.org/jira/browse/CXF-5404
>             Project: CXF
>          Issue Type: Bug
>          Components: JAX-RS
>    Affects Versions: 2.7.7
>            Reporter: Dale Peakall
>            Priority: Minor
>
> When using the AtomPojoProvider with JAX-RS - the library bombs with an NPE 
> (embedded in a ClientErrorException) if using JAXB annotated content objects 
> - but no content is provided in the entry.  It also NPE's if the content 
> element is present but contains no XML (although this is a slightly dubious 
> Atom document at this point).
> Returning "null" would be a much better result in this case.



--
This message was sent by Atlassian JIRA
(v6.1#6144)

Reply via email to