Project

General

Profile

1
package eu.dnetlib.vocabularies.publisher.controller;
2

    
3
import java.util.List;
4
import javax.annotation.Resource;
5
import javax.servlet.http.HttpServletRequest;
6

    
7
import eu.dnetlib.common.rmi.DNetRestDocumentation;
8
import eu.dnetlib.vocabularies.publisher.VocabularyNotFoundException;
9
import eu.dnetlib.vocabularies.publisher.VocabularyRetriever;
10
import eu.dnetlib.vocabularies.publisher.model.Vocabulary;
11
import eu.dnetlib.vocabularies.publisher.model.VocabularyTerm;
12
import org.apache.commons.lang3.StringUtils;
13
import org.apache.commons.logging.Log;
14
import org.apache.commons.logging.LogFactory;
15
import org.dom4j.DocumentException;
16
import org.springframework.beans.factory.annotation.Value;
17
import org.springframework.stereotype.Controller;
18
import org.springframework.ui.ModelMap;
19
import org.springframework.util.AntPathMatcher;
20
import org.springframework.web.bind.annotation.*;
21
import org.springframework.web.servlet.HandlerMapping;
22

    
23
@Controller
24
@DNetRestDocumentation
25
@CrossOrigin(origins = { "*" })
26
public class VocabularyPublisherController {
27

    
28
	private static final Log log = LogFactory.getLog(VocabularyPublisherController.class);
29

    
30
	@Resource
31
	private VocabularyRetriever vocabularyRetriever;
32
	@Value("${vocabulary.publisher.title}")
33
	private String title;
34

    
35

    
36
	@RequestMapping(value = "/vocabularies", method = RequestMethod.GET, produces = { "application/json" })
37
	public @ResponseBody
38
	List<Vocabulary> listVocabularies() {
39
		log.debug("listVocabularies()");
40
		return vocabularyRetriever.listVocabularies();
41
	}
42

    
43
	@RequestMapping(value = "/vocabularies/{code}", method = RequestMethod.GET, produces = { "application/json" })
44
	public @ResponseBody
45
	Vocabulary getVocabulary(@PathVariable("code") final String code) throws VocabularyNotFoundException {
46
		log.debug("getVocabulary with code = " + code);
47
		return this.vocabularyRetriever.getVocabularyByCode(code);
48
	}
49

    
50
	@RequestMapping(value = "/vocabularies/{code}/**", method = RequestMethod.GET, produces = { "application/json" })
51
	public @ResponseBody
52
	VocabularyTerm getTermSynonyms(
53
			@PathVariable("code") final String code,
54
			final HttpServletRequest request) throws VocabularyNotFoundException {
55
		final String term = extractTerm(request);
56
		log.debug(String.format("getVocabulary with code = %s, term = %s", code, term));
57
		return this.vocabularyRetriever.getTermSynonyms(code, term);
58
	}
59

    
60

    
61
	private String extractTerm(HttpServletRequest request) {
62

    
63
		String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE); // /elements/CATEGORY1/CATEGORY1_1/ID
64
		String bestMatchPattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE); // /elements/**
65

    
66
		return StringUtils.substringBeforeLast(new AntPathMatcher().extractPathWithinPattern(bestMatchPattern, path), ".json"); // CATEGORY1/CATEGORY1_1/ID
67
	}
68

    
69
	// View-based methods for html responses
70
	@RequestMapping(value = "/vocabularies", method = RequestMethod.GET, produces = { "text/html" })
71
	public void listVocabularies(final ModelMap map) {
72
		map.addAttribute("vocabularies", listVocabularies());
73
		map.addAttribute("title", title);
74
	}
75

    
76
	@RequestMapping(value = "/vocabularies/{code}", method = RequestMethod.GET, produces = { "text/html" })
77
	public String getVocabulary(
78
			@PathVariable("code") final String code,
79
			final ModelMap map) throws VocabularyNotFoundException {
80

    
81
		map.addAttribute("vocabulary", getVocabulary(code));
82
		return "displayVocabulary";
83
	}
84

    
85
	@RequestMapping(value = "/vocabularies/{code}/**", method = RequestMethod.GET, produces = { "text/html" })
86
	public String getTermSynonyms(
87
			@PathVariable("code") final String code,
88
			final HttpServletRequest request,
89
			final ModelMap map) throws VocabularyNotFoundException {
90
		map.addAttribute("vocabulary", getVocabulary(code));
91
		map.addAttribute("term", getTermSynonyms(code, request));
92
		return "displaySynonyms";
93
	}
94

    
95
}
(2-2/2)