@Override public String prepareResponse(String className, Object o) throws ParseException { try { Class clazz = Class.forName(className); JAXBContext jaxbContext = JAXBContext.newInstance(clazz.getPackage().getName()); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); marshaller.marshal(clazz.cast(o), byteArrayOutputStream); return byteArrayOutputStream.toString(); } catch (JAXBException e) { throw new ParseException( "Error occured during the parsing of request's XML body. The details of the exception " + e.toString(), 0); } catch (ClassNotFoundException e) { throw new ParseException( "Error occured during the parsing of request's XML body. The details of the exception " + e.toString(), 0); } }
public static JAXBElement createJAXBelement(Object content) { Class clazz = (Class) content.getClass(); // Remove the 'Type' suffix from the xml type name and use it as XML element! XmlType t = (XmlType) clazz.getAnnotation(XmlType.class); String element = t.name().substring(0, t.name().length() - 4); return new JAXBElement(new QName(JossoConstants.JOSSO_PROTOCOL_NS, element), clazz, content); }
@Override public Object parseRequest(String className) throws ParseException { try { Class clazz = Class.forName(className); JAXBContext jaxbContext = JAXBContext.newInstance(clazz.getPackage().getName()); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); Source source = new StreamSource(new ByteArrayInputStream(body.getBytes())); JAXBElement<Object> root = jaxbUnmarshaller.unmarshal(source, clazz); return clazz.cast(root.getValue()); } catch (JAXBException e) { throw new ParseException( "Error occured during the parsing of request's XML body. The details of the exception " + e.toString(), 0); } catch (ClassNotFoundException e) { throw new ParseException( "Error occured during the parsing of request's XML body. The details of the exception " + e.toString(), 0); } }