| /* |
| * Copyright (c) 2011, 2020 Oracle and/or its affiliates. All rights reserved. |
| * |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License v. 2.0 which is available at |
| * http://www.eclipse.org/legal/epl-2.0, |
| * or the Eclipse Distribution License v. 1.0 which is available at |
| * http://www.eclipse.org/org/documents/edl-v10.php. |
| * |
| * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause |
| */ |
| |
| // Contributors: |
| // Blaise Doughan - 2.3 - initial implementation |
| package org.eclipse.persistence.testing.jaxrs.model; |
| |
| import org.eclipse.persistence.jaxb.JAXBContextFactory; |
| |
| import jakarta.ws.rs.Consumes; |
| import jakarta.ws.rs.WebApplicationException; |
| import jakarta.ws.rs.core.MediaType; |
| import jakarta.ws.rs.core.MultivaluedMap; |
| import jakarta.ws.rs.ext.MessageBodyReader; |
| import jakarta.ws.rs.ext.Provider; |
| import jakarta.xml.bind.JAXBContext; |
| import jakarta.xml.bind.JAXBException; |
| import java.io.IOException; |
| import java.io.InputStream; |
| import java.lang.annotation.Annotation; |
| import java.lang.reflect.Type; |
| import java.util.HashMap; |
| import java.util.Map; |
| |
| @Provider |
| @Consumes(MediaType.APPLICATION_XML) |
| public class PhoneNumberReader implements MessageBodyReader<PhoneNumber> { |
| |
| private JAXBContext jc; |
| |
| public PhoneNumberReader() { |
| try { |
| Map<String, Object> properties = new HashMap<String, Object>(1); |
| properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, |
| "META-INF/binding-phonenumber.xml"); |
| jc = JAXBContext.newInstance(new Class[] { PhoneNumber.class }, |
| properties); |
| } catch (JAXBException e) { |
| throw new RuntimeException(e); |
| } |
| } |
| |
| public boolean isReadable(Class<?> type, Type genericType, |
| Annotation[] annotations, MediaType mediaType) { |
| return type == PhoneNumber.class; |
| } |
| |
| public PhoneNumber readFrom(Class<PhoneNumber> type, Type genericType, |
| Annotation[] annotations, MediaType mediaType, |
| MultivaluedMap<String, String> httpHeaders, InputStream entityStream) |
| throws IOException, WebApplicationException { |
| try { |
| return (PhoneNumber) jc.createUnmarshaller() |
| .unmarshal(entityStream); |
| } catch (JAXBException e) { |
| throw new WebApplicationException(e); |
| } |
| } |
| |
| } |