ref #6964 base implementation of the RegistrationController
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / controller / AbstractController.java
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.util.Arrays;
12 import java.util.List;
13
14 import javax.servlet.http.HttpServletRequest;
15
16 import org.apache.log4j.Logger;
17
18 import eu.etaxonomy.cdm.api.service.IService;
19 import eu.etaxonomy.cdm.model.common.CdmBase;
20 import eu.etaxonomy.cdm.remote.controller.util.PagerParameters;
21
22 /**
23 * Abstract controller class for CDM Rest service controllers which return entities or DTOs
24 * from the underlying data base. Implementations of this class are primarily bound to a
25 * specific cdm service class and thus are operating primarily on a specific cdm base type.
26 *
27 * This class guarantees consistent use of init-strategies and harmonizes the logging of full
28 * request urls with query parameters.
29 *
30 * @author a.kohlbecker
31 * @date 23.06.2009
32 *
33 * @param <T>
34 * @param <SERVICE>
35 */
36 public abstract class AbstractController<T extends CdmBase, SERVICE extends IService<T>> {
37
38 public static final Logger logger = Logger.getLogger(AbstractController.class);
39
40 protected static final List<String> DEFAULT_INIT_STRATEGY = Arrays.asList(new String []{
41 "$"
42 });
43 protected static final Integer DEFAULT_PAGE_SIZE = PagerParameters.DEFAULT_PAGESIZE;
44
45 /**
46 * Default thread priority for long term processes which are running in
47 * separate threads. These batch processes are usually monitored with the
48 * {@link ProgressMonitorController}. This value must be lower than
49 * {@link Thread#NORM_PRIORITY}
50 */
51 public static final int DEFAULT_BATCH_THREAD_PRIORITY = 3;
52
53 protected SERVICE service;
54
55 public abstract void setService(SERVICE service);
56
57 protected List<String> initializationStrategy = DEFAULT_INIT_STRATEGY;
58
59 /**
60 * Set the default initialization strategy for this controller.
61 *
62 * @param initializationStrategy
63 */
64 public final void setInitializationStrategy(List<String> initializationStrategy) {
65 this.initializationStrategy = initializationStrategy;
66 }
67
68 /**
69 * Provides access to the default initialization strategy.
70 * The default initialization strategy is predefined for all controllers in
71 * {@link #DEFAULT_INIT_STRATEGY} but can be altered by
72 * concrete implementations by utilizing {@link #setInitializationStrategy(List)}
73 * in the constructor of the specific controller.
74 *
75 * @return the default initialization strategy
76 */
77 public final List<String> getInitializationStrategy() {
78 return this.initializationStrategy;
79 }
80
81 /**
82 * Returns the HTTP request path and query parameters as string
83 *
84 * @param request
85 * @return request path and query parameters as string.
86 */
87 protected static String requestPathAndQuery(HttpServletRequest request) {
88 if(request == null) {
89 return "";
90 }
91 StringBuilder b = new StringBuilder();
92 b.append(request.getMethod()).append(": ");
93 b.append(request.getRequestURI());
94 String query = request.getQueryString();
95 if(query != null) {
96 b.append("?").append(query);
97 }
98
99 return b.toString();
100 }
101
102 }