Commit 57987661 authored by Noel Alonso's avatar Noel Alonso
Browse files

Cambia la creación del sitemap

Ahora el sitemap se crea en el arranque de la api y es la propia api la
que sirve el fichero xml. De esta forma, es independiente de donde se
crea.
parent 10f4ac94
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -103,7 +103,7 @@ public class Oauth2SecurityConfiguration {
					"#oauth2.hasScope('read') or #oauth2.hasScope('write') and "
					+ "hasAnyRole('ROLE_ADMINISTRATOR', 'ROLE_OAG', 'ROLE_COLLABORATOR')");
			
			http.authorizeRequests().antMatchers(HttpMethod.GET, "/generate-sitemap").access(
			http.authorizeRequests().antMatchers(HttpMethod.GET, "/sitemap.xml").access(
					"#oauth2.hasScope('read') or #oauth2.hasScope('write') and "
					+ "hasAnyRole('ROLE_ADMINISTRATOR')");
			
+38 −7
Original line number Diff line number Diff line
package es.redmic.api.utils.sitemap.controller;

import java.io.File;
import java.io.FileInputStream;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import es.redmic.api.utils.sitemap.service.GenerateSitemapService;
import es.redmic.exception.dto.SuperDTO;
import es.redmic.exception.mediastorage.MSFileNotFoundException;
import es.redmic.mediastorage.service.MediaStorageServiceItfc;

@Controller
public class GenerateSitemapController {

	private GenerateSitemapService service;

	@Autowired
	MediaStorageServiceItfc mediaStorageService;

	@Value("${property.SITEMAP_DESTINATION_DIR}")
	private String PATH;

	@Autowired
	public GenerateSitemapController(GenerateSitemapService service) {
		this.service = service;
	}

	@RequestMapping(value = "/generate-sitemap", method = RequestMethod.GET)
	@ResponseBody
	public SuperDTO getMapping() {

	@PostConstruct
	public void GenerateSitemapControllerPostConstruct() {
		service.createSitemap();
	}

		return new SuperDTO(true);
	@RequestMapping(value = "/sitemap.xml", method = RequestMethod.GET, produces = { "text/xml", "application/xml" })
	public void download(HttpServletResponse response) {

		String name = "sitemap.xml";

		// TODO: Esta funcionalidad es provisional. En caso de mantener el servir el xml
		// desde api, añadir esta funcionalidad al servicio.
		File file = mediaStorageService.openTempFile(PATH, name);

		response.reset();
		response.setContentType("application/xml");
		response.setHeader("Content-Disposition", "inline;filename=\"" + name + "\"");

		try {

			IOUtils.copy(new FileInputStream(file), response.getOutputStream());
		} catch (Exception e) {
			throw new MSFileNotFoundException(file.getName(), PATH, e);
		}
	}
}
+1 −1
Original line number Diff line number Diff line
@@ -18,7 +18,7 @@ oauth.server=http://oauth:8081

#Sitemap properties
property.USER_API=http://user:8082
property.SITEMAP_BASE_URL=https://appdev.redmic.net
property.SITEMAP_BASE_URL=https://redmic.net

#S3

+1 −1
Original line number Diff line number Diff line
@@ -68,7 +68,7 @@ spring.servlet.multipart.max-request-size=250MB
redmic.elasticsearch.MAX_QUERY_SIZE=3000

#Sitemap properties
property.SITEMAP_DESTINATION_DIR=/home/REDMIC/
property.SITEMAP_DESTINATION_DIR=${property.path.media_storage.TEMP_PUBLIC}
property.SITEMAP_BASE_URL=${property.SITEMAP_BASE_URL}
property.USER_API=${property.USER_API}
property.URL_OPEN_MODULES=${property.USER_API}/api/user/modules/openmodules/
+3 −6
Original line number Diff line number Diff line
package es.redmic.test.integration.utils.sitemap;

import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@@ -22,7 +21,6 @@ import es.redmic.ApiApplication;
import es.redmic.api.utils.sitemap.controller.GenerateSitemapController;
import es.redmic.api.utils.sitemap.dto.OpenModules;
import es.redmic.api.utils.sitemap.service.GenerateSitemapService;
import es.redmic.exception.dto.SuperDTO;
import es.redmic.test.integration.ApiApplicationTest;
import es.redmic.test.integration.common.IntegrationTestBase;
import es.redmic.test.integration.utils.JsonToBeanTestUtil;
@@ -41,7 +39,8 @@ public class GenerateSitemapTest extends IntegrationTestBase {

	// @formatter:off

	private final String GENERATE_SITEMAP_URL = "/generate-sitemap",
	private final String FILE_NAME = "sitemap.xml",
			GENERATE_SITEMAP_URL = "/" + FILE_NAME,
			OPEN_MODULES_RESOURCE = "/sitemap/openModules.json";

	// @formatter:on
@@ -52,8 +51,6 @@ public class GenerateSitemapTest extends IntegrationTestBase {
	@Test
	public void generateSitemapAsAdministrator_IsSuccessful_IfIsAuthorized() throws Exception {

		when(controller.getMapping()).thenReturn(new SuperDTO(true));

		mockMvc.perform(get(GENERATE_SITEMAP_URL).header("Authorization", "Bearer " + getTokenAdministratorUser()))
				.andExpect(status().isOk());
	}
@@ -67,7 +64,7 @@ public class GenerateSitemapTest extends IntegrationTestBase {
	@Test
	public void generateSitemap_CreateFile_IfThereAreOpenModules() throws Exception {

		File f = new File(DESTINATION_DIR + "sitemap.xml");
		File f = new File(DESTINATION_DIR + "/" + FILE_NAME);
		f.delete();

		OpenModules openModules = (OpenModules) JsonToBeanTestUtil.getBean(OPEN_MODULES_RESOURCE, OpenModules.class);