package eu.dnetlib.data.mapping; import java.nio.charset.StandardCharsets; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpStatus; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import eu.dnetlib.data.mapping.cleaner.CleanerFactory; @RestController @RequestMapping("/ajax/mapping") public class MappingAjaxController { @Autowired private CleanerFactory cleanerFactory; @PostMapping(value = "/clean", consumes = { MediaType.TEXT_PLAIN_VALUE, MediaType.APPLICATION_XML_VALUE, }, produces = MediaType.APPLICATION_XML_VALUE) public void importResource(@RequestParam final String rule, final HttpServletRequest req, final HttpServletResponse res) throws Exception { final String xmlIn = IOUtils.toString(req.getInputStream(), StandardCharsets.UTF_8); final String xmlOut = cleanerFactory.obtainCleaningRule(rule).transform(xmlIn); res.setCharacterEncoding(StandardCharsets.UTF_8.name()); res.setContentType(MediaType.APPLICATION_XML_VALUE); if (StringUtils.isNotBlank(xmlOut)) { IOUtils.write(xmlOut, res.getOutputStream(), StandardCharsets.UTF_8.name()); } else { res.sendError(HttpStatus.SC_INTERNAL_SERVER_ERROR, "Invalid record"); } } }