Project

General

Profile

Revision e5846a95

IDe5846a95ffb2a598b4d4d4ef6568d691bac48da9
Parent 469f904a
Child 06ec599a

Added by Andreas Kohlbecker over 8 years ago

removing ContentNegociationHandlerInterceptor

View differences:

.gitattributes
1730 1730
cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/controller/dto/PolytomousKeyNodeDtoController.java -text
1731 1731
cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/controller/ext/ExternalBhleController.java -text
1732 1732
cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/controller/ext/ExternalGeoController.java -text
1733
cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/controller/interceptor/ContentNegociationHandlerInterceptor.java -text
1734 1733
cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/controller/interceptor/DatasourceContextHandlerInterceptor.java -text
1735 1734
cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/controller/interceptor/LocaleContextHandlerInterceptor.java -text
1736 1735
cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/controller/oaipmh/AbstractOaiPmhController.java -text
cdmlib-remote-webapp/src/main/webapp/WEB-INF/cdmlib-remote-servlet.xml
67 67
  <bean id="datasourceContextHandlerInterceptor"
68 68
    class="eu.etaxonomy.cdm.remote.controller.interceptor.DatasourceContextHandlerInterceptor" /> -->
69 69
  <bean id="localeContextHandlerInterceptor"
70
    class="eu.etaxonomy.cdm.remote.controller.interceptor.LocaleContextHandlerInterceptor" />
71
  <!--
72
     === no longer needed, replaced by ContentNegotiatingViewResolver ==
73
  <bean id="contentNegociationHandlerInterceptor"
74
    class="eu.etaxonomy.cdm.remote.controller.interceptor.ContentNegociationHandlerInterceptor">
75
    <property name="mapping">
76
      <map>
77
        <entry key="text/json" value="json" />
78
        <entry key="application/json" value="json" />
79
        <entry key="application/xml" value="xml" />
80
      </map>
81
    </property>
82
    <property name="defaultExtension" value="xml" />
83
  </bean>
84
   -->
70
    class="eu.etaxonomy.cdm.remote.controller.interceptor.LocaleContextHandlerInterceptor" />
85 71

  
86 72
  <!--
87 73
   ======= ViewControllers =======
cdmlib-remote/src/main/java/eu/etaxonomy/cdm/remote/controller/interceptor/ContentNegociationHandlerInterceptor.java
1
// $Id$
2
/**
3
* Copyright (C) 2007 EDIT
4
* European Distributed Institute of Taxonomy 
5
* http://www.e-taxonomy.eu
6
* 
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10

  
11
package eu.etaxonomy.cdm.remote.controller.interceptor;
12

  
13
import java.util.HashMap;
14
import java.util.Map;
15
import java.util.Set;
16
import java.util.regex.Matcher;
17
import java.util.regex.Pattern;
18

  
19
import javax.mail.internet.ContentType;
20
import javax.mail.internet.ParseException;
21
import javax.servlet.http.HttpServletRequest;
22
import javax.servlet.http.HttpServletResponse;
23

  
24
import org.springframework.web.servlet.ModelAndView;
25
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
26

  
27
/**
28
 * @author ben.clark
29
 * @author a.kohlbecker
30
 *
31
 */
32
public class ContentNegociationHandlerInterceptor extends HandlerInterceptorAdapter {
33
	
34
	private String defaultExtension;
35
	
36
	public static Pattern VIEW_WITH_SUFFIX_PATTERN;
37
	
38
	static {
39
		VIEW_WITH_SUFFIX_PATTERN = Pattern.compile("^.+\\.\\w+$");
40
	}
41
	
42
	private Map<ContentType,String> contentTypeToViewNameMapping;
43
	
44
	public void setMapping(Map<String,String> mapping) throws ParseException {
45
		this.contentTypeToViewNameMapping = new HashMap<ContentType,String>();
46
		
47
		for(String cType : mapping.keySet()) {
48
			ContentType key = new ContentType(cType);
49
			contentTypeToViewNameMapping.put(key, mapping.get(cType));
50
		}
51
	}
52
	
53
	public void setDefaultExtension(String defaultExtension)  {
54
		this.defaultExtension = defaultExtension;
55
	}
56
	
57
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
58
		if(modelAndView != null) {
59
		    Matcher matcher = VIEW_WITH_SUFFIX_PATTERN.matcher(request.getServletPath());
60
		    if(!matcher.matches()) {
61
    		    ContentType bestMatch = getBestContentType(request.getHeader("Accept"), contentTypeToViewNameMapping.keySet());
62
		        String viewName = modelAndView.getViewName();
63
		        
64
		        if(bestMatch == null) {
65
	    		    modelAndView.setViewName(viewName + "." + this.defaultExtension);
66
		        } else {
67
			        modelAndView.setViewName(viewName + "." + contentTypeToViewNameMapping.get(bestMatch));
68
		        }
69
	       } else {
70
	    	   // remove trailing slash
71
	    	   modelAndView.setViewName(request.getServletPath().substring(1));
72
	       }
73
		}
74
	}
75

  
76
	/**
77
	 * Determine the best content type from a list of supported content types 
78
	 * based upon their q values. Converted from the gdata api method to work with
79
	 * javax.mail.internet.ContentType instead
80
	 * @param acceptHeader
81
	 * @param supportedContentTypes
82
	 * @return
83
	 */
84
	public ContentType getBestContentType(String acceptHeader,  Set<ContentType> supportedContentTypes) {
85

  
86
		    if (acceptHeader == null) {
87
		      return null;
88
		    }
89

  
90
		    // iterate over all of the accepted content types to find the best match
91
		    float bestQ = 0;
92
		    ContentType bestContentType = null;
93
		    String[] acceptedTypes = acceptHeader.split(",");
94
		    for (String acceptedTypeString : acceptedTypes) {
95

  
96
		      // create the content type object
97
		      ContentType acceptedContentType;
98
		      try {
99
		        acceptedContentType = new ContentType(acceptedTypeString.trim());
100
		      } catch (ParseException e) {
101
		    	  continue;
102
			  }
103

  
104
		      // parse the "q" value (default of 1)
105
		      float curQ = 1;
106
		      try {
107
		        String qAttr = acceptedContentType.getParameter("q");
108
		        if (qAttr != null) {
109
		          float qValue = Float.valueOf(qAttr);
110
		          if (qValue <= 0 || qValue > 1) {
111
		            continue;
112
		          }
113
		          curQ = qValue;
114
		        }
115
		      } catch (NumberFormatException ex) {
116
		        // ignore exception
117
		        continue;
118
		      }
119

  
120
		      // only check it if it's at least as good ("q") as the best one so far
121
		      if (curQ < bestQ) {
122
		        continue;
123
		      }
124

  
125
		      /* iterate over the actual content types in order to find the best match
126
		      to the current accepted content type */
127
		      for (ContentType supportedContentType : supportedContentTypes) {
128

  
129
		        /* if the "q" value is the same as the current best, only check for
130
		        better content types */
131
		        if (curQ == bestQ && bestContentType == supportedContentType) {
132
		          break;
133
		        }
134

  
135
		        /* check if the accepted content type matches the current actual
136
		        content type */
137
		        if (supportedContentType.match(acceptedContentType)) {
138
		          bestContentType = supportedContentType;
139
		          bestQ = curQ;
140
		          break;
141
		        }
142
		      }
143
		    }
144

  
145
		    // if found an acceptable content type, return the best one
146
		    if (bestQ != 0) {
147
		      return bestContentType;
148
		    }
149

  
150
		    // Return null if no match
151
		    return null;
152
		  }
153

  
154
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)