1
|
/**
|
2
|
* Copyright (C) 2009 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.remote.controller;
|
10
|
|
11
|
import java.io.UnsupportedEncodingException;
|
12
|
import java.util.Arrays;
|
13
|
import java.util.List;
|
14
|
|
15
|
import javax.servlet.http.HttpServletRequest;
|
16
|
|
17
|
import org.springframework.beans.factory.annotation.Autowired;
|
18
|
|
19
|
import eu.etaxonomy.cdm.api.service.IService;
|
20
|
import eu.etaxonomy.cdm.api.utility.UserHelper;
|
21
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
22
|
import eu.etaxonomy.cdm.persistence.dao.hibernate.common.DaoBase;
|
23
|
import eu.etaxonomy.cdm.remote.controller.util.PagerParameters;
|
24
|
|
25
|
/**
|
26
|
* Abstract controller class for CDM Rest service controllers which return entities or DTOs
|
27
|
* from the underlying data base. Implementations of this class are primarily bound to a
|
28
|
* specific cdm service class and thus are operating primarily on a specific cdm base type.
|
29
|
*
|
30
|
* This class guarantees consistent use of init-strategies and harmonizes the logging of full
|
31
|
* request urls with query parameters.
|
32
|
*
|
33
|
* @author a.kohlbecker
|
34
|
* @since 23.06.2009
|
35
|
*
|
36
|
* @param <T>
|
37
|
* @param <SERVICE>
|
38
|
*/
|
39
|
public abstract class AbstractController<T extends CdmBase, SERVICE extends IService<T>> {
|
40
|
|
41
|
protected static final List<String> DEFAULT_INIT_STRATEGY = Arrays.asList(new String []{
|
42
|
"$"
|
43
|
});
|
44
|
protected static final Integer DEFAULT_PAGE_SIZE = PagerParameters.DEFAULT_PAGESIZE;
|
45
|
|
46
|
/**
|
47
|
* Default thread priority for long term processes which are running in
|
48
|
* separate threads. These batch processes are usually monitored with the
|
49
|
* {@link ProgressMonitorController}. This value must be lower than
|
50
|
* {@link Thread#NORM_PRIORITY}
|
51
|
*/
|
52
|
public static final int DEFAULT_BATCH_THREAD_PRIORITY = 3;
|
53
|
|
54
|
protected static final boolean NO_UNPUBLISHED = DaoBase.NO_UNPUBLISHED;
|
55
|
protected static final boolean INCLUDE_UNPUBLISHED = DaoBase.INCLUDE_UNPUBLISHED;
|
56
|
|
57
|
protected SERVICE service;
|
58
|
|
59
|
@Autowired
|
60
|
protected UserHelper userHelper;
|
61
|
|
62
|
public abstract void setService(SERVICE service);
|
63
|
|
64
|
protected List<String> initializationStrategy = DEFAULT_INIT_STRATEGY;
|
65
|
|
66
|
/**
|
67
|
* Set the default initialization strategy for this controller.
|
68
|
*
|
69
|
* @param initializationStrategy
|
70
|
*/
|
71
|
public final void setInitializationStrategy(List<String> initializationStrategy) {
|
72
|
this.initializationStrategy = initializationStrategy;
|
73
|
}
|
74
|
|
75
|
/**
|
76
|
* Provides access to the default initialization strategy.
|
77
|
* The default initialization strategy is predefined for all controllers in
|
78
|
* {@link #DEFAULT_INIT_STRATEGY} but can be altered by
|
79
|
* concrete implementations by utilizing {@link #setInitializationStrategy(List)}
|
80
|
* in the constructor of the specific controller.
|
81
|
*
|
82
|
* @return the default initialization strategy
|
83
|
*/
|
84
|
public final List<String> getInitializationStrategy() {
|
85
|
return this.initializationStrategy;
|
86
|
}
|
87
|
|
88
|
/**
|
89
|
* Returns the HTTP request path and query parameters as string
|
90
|
*
|
91
|
* @param request
|
92
|
* @return request path and query parameters as string.
|
93
|
*/
|
94
|
public static String requestPathAndQuery(HttpServletRequest request) {
|
95
|
if(request == null) {
|
96
|
return "";
|
97
|
}
|
98
|
StringBuilder b = new StringBuilder();
|
99
|
b.append(request.getMethod()).append(": ");
|
100
|
b.append(request.getRequestURI());
|
101
|
String query = request.getQueryString();
|
102
|
if(query != null) {
|
103
|
b.append("?").append(query);
|
104
|
}
|
105
|
|
106
|
return b.toString();
|
107
|
}
|
108
|
|
109
|
/**
|
110
|
* This method is useful to read path parameters from request urls in methods where the method has been annotated with a
|
111
|
* {@link RequestMapping} having wildcards as trailing characters like in <code>@RequestMapping("identifier/**")</code>.
|
112
|
* <p>
|
113
|
* Reads the path part following pattern passed as <code>basePath</code> and returns it is urldecoded String.
|
114
|
* The <code>basepath</code> usually is the combination of the class level and method level RequestMappings e.g.:
|
115
|
* <code>"/registration/identifier/"</code>
|
116
|
*
|
117
|
* @param basePath
|
118
|
* The base path of the controller method.
|
119
|
* @param request
|
120
|
* @return
|
121
|
*/
|
122
|
protected String readPathParameter(HttpServletRequest request, String basePath) {
|
123
|
String pathParameter = request.getRequestURI().replaceFirst("^(?:.*)" + basePath , "");
|
124
|
if(pathParameter != null){
|
125
|
try {
|
126
|
pathParameter = java.net.URLDecoder.decode(pathParameter, "UTF-8");
|
127
|
pathParameter = pathParameter.replaceAll("\\.json$|\\.xml$", "");
|
128
|
} catch (UnsupportedEncodingException e) {
|
129
|
// should never happen
|
130
|
throw new RuntimeException(e);
|
131
|
}
|
132
|
}
|
133
|
return pathParameter;
|
134
|
}
|
135
|
|
136
|
}
|