private String printTemplateToXML(ITemplate template) { StringBuilder xmlResponse = new StringBuilder(); xmlResponse.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); xmlResponse.append(template.getText()); return xmlResponse.toString(); }
private String printTemplatesToXML(List<ITemplate> templates) { StringBuilder xmlResponse = new StringBuilder(); xmlResponse.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); xmlResponse.append("<collection href=\"/templates\">\n"); xmlResponse.append("<items offset=\"0\" total=\"" + templates.size() + "\">\n"); for (ITemplate template : templates) { xmlResponse.append(template.getText()); } xmlResponse.append("</items>\n"); xmlResponse.append("</collection>\n"); return xmlResponse.toString(); }
public String createTemplate(HttpHeaders hh, String uriInfo, String payload) throws HelperException { logger.debug("StartOf createTemplate payload:" + payload); try { JAXBContext jaxbContext = JAXBContext.newInstance(eu.atos.sla.parser.data.wsag.Template.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); eu.atos.sla.parser.data.wsag.Template templateXML = (eu.atos.sla.parser.data.wsag.Template) jaxbUnmarshaller.unmarshal(new StringReader(payload)); ITemplate template = new Template(); String location = null; ITemplate templateStored = null; if (templateXML != null) { // remove xml header payload = payload.replaceAll("\\<\\?xml(.+?)\\?\\>", "").trim(); // add field TemplateId if it doesn't exist if (templateXML.getTemplateId() == null) { String templateId = UUID.randomUUID().toString(); payload = payload .replaceAll( "\\<wsag\\:Template xmlns\\:wsag=\"http\\:\\/\\/www.ggf.org\\/namespaces\\/ws-agreement\"\\>", "\\<wsag\\:Template xmlns\\:wsag=\"http\\:\\/\\/www.ggf.org\\/namespaces\\/ws-agreement\" TemplateId=\"" + templateId + "\"\\>") .trim(); templateXML.setTemplateId(templateId); } if (!doesTemplateIdExistInDatabase(templateXML.getTemplateId())) { String providerUUID = null; try { ServiceProvider ctxProvider = ServiceProvider.fromString(templateXML.getContext().getServiceProvider()); switch (ctxProvider) { case AGREEMENT_RESPONDER: providerUUID = templateXML.getContext().getAgreementResponder(); break; case AGREEMENT_INITIATOR: providerUUID = templateXML.getContext().getAgreementInitiator(); break; } } catch (IllegalArgumentException e) { throw new ModelConversionException( "The Context/ServiceProvider field must match with the word " + ServiceProvider.AGREEMENT_RESPONDER + " or " + ServiceProvider.AGREEMENT_INITIATOR); } IProvider provider = getProviderPerUUIDFromDatabase(providerUUID); if (provider == null) throw new HelperException( Code.DB_MISSING, "Provider with UUID " + providerUUID + " doesn't exist in the database"); template = modelConverter.getTemplateFromTemplateXML( (eu.atos.sla.parser.data.wsag.ITemplate) templateXML, payload); logger.info("Template uuid is" + template.getUuid()); logger.info("Template id is" + template.getId()); logger.info("Template text is" + template.getText()); templateStored = templateDAO.save(template); provider.addTemplate(templateStored); providerDAO.update(provider); } else { throw new HelperException( Code.DB_EXIST, "Element with id:" + templateXML.getTemplateId() + " already exists in the SLA Repository Database"); } } if (templateStored != null) { location = uriInfo + "/" + templateStored.getUuid(); logger.debug("EndOf createTemplate"); return location; } else throw new HelperException( Code.INTERNAL, "Error when creating the template in the SLA Repository Database"); } catch (JAXBException e) { logger.error("Error in createTemplate ", e); throw new HelperException( Code.PARSER, "Error when creating template parsing file:" + e.getMessage()); } catch (Throwable e) { logger.error("Error in createTemplate ", e); throw new HelperException(Code.INTERNAL, "Error when creating template:" + e.getMessage()); } }