@@ -69,8 +69,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
6969 } else {
7070 options = objectMapper .readValue (URLDecoder .decode (request .getQueryString (), StandardCharsets .UTF_8 ), LinkedHashMap .class );
7171 }
72- } else if (request .getQueryString () != null ) {
73- options .putAll (objectMapper .readValue (request .getQueryString (), Map .class ));
72+ if (log .isDebugEnabled ()) log .debug ("Options from query: " + options );
7473 }
7574 } else {
7675 identifier = path ;
@@ -109,6 +108,104 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
109108
110109 if (log .isDebugEnabled ()) log .debug ("Using options: " + options );
111110
111+ // execute
112+
113+ this .execute (response , identifier , options , httpAcceptMediaTypes , isResolve );
114+ }
115+
116+ @ Override
117+ protected void doPost (HttpServletRequest request , HttpServletResponse response ) throws IOException {
118+
119+ // read request
120+
121+ request .setCharacterEncoding ("UTF-8" );
122+ response .setCharacterEncoding ("UTF-8" );
123+
124+ String contextPath = request .getContextPath ();
125+ String servletPath = request .getServletPath ();
126+ String requestPath = request .getRequestURI ();
127+
128+ if (log .isDebugEnabled ()) log .debug ("Incoming request: " + requestPath );
129+
130+ String path = requestPath .substring (contextPath .length () + servletPath .length ());
131+ if (path .startsWith ("/" )) path = path .substring (1 );
132+
133+ if (path .isEmpty ()) {
134+ ServletUtil .sendResponse (response , HttpServletResponse .SC_BAD_REQUEST , "No identifier found in request." );
135+ return ;
136+ }
137+
138+ // parse request
139+
140+ String identifier ;
141+ Map <String , Object > options = new LinkedHashMap <>();
142+ boolean isResolve ;
143+
144+ if (path .startsWith ("did%3A" )) {
145+ identifier = URLDecoder .decode (path , StandardCharsets .UTF_8 );
146+ if (request .getQueryString () != null ) {
147+ if (request .getQueryString ().contains ("=" )) {
148+ for (Enumeration <String > e = request .getParameterNames (); e .hasMoreElements (); ) {
149+ String parameterName = e .nextElement ();
150+ String parameterValue = request .getParameter (parameterName );
151+ options .put (parameterName , parameterValue );
152+ }
153+ } else {
154+ options = objectMapper .readValue (URLDecoder .decode (request .getQueryString (), StandardCharsets .UTF_8 ), LinkedHashMap .class );
155+ }
156+ if (log .isDebugEnabled ()) log .debug ("Options from query: " + options );
157+ }
158+ } else {
159+ identifier = path ;
160+ if (request .getQueryString () != null ) identifier += "?" + request .getQueryString ();
161+ }
162+ isResolve = (! identifier .contains ("/" )) && (! identifier .contains ("?" )) && (! identifier .contains ("#" ));
163+
164+ Map <String , Object > bodyOptions = objectMapper .readValue (request .getReader (), Map .class );
165+ if (log .isDebugEnabled ()) log .debug ("Options from body: " + bodyOptions );
166+ if (bodyOptions != null ) {
167+ options .keySet ().forEach (bodyOptions ::remove );
168+ options .putAll (bodyOptions );
169+ }
170+
171+ if (log .isInfoEnabled ()) log .info ("Incoming identifier: " + identifier + " (isResolve=" + isResolve + "), incoming options: " + options );
172+
173+ // prepare options
174+
175+ String httpXConfigHeader = request .getHeader ("X-Config" );
176+ if (log .isInfoEnabled ()) log .info ("Incoming X-Config: header string: " + httpXConfigHeader );
177+ Map <String , Object > httpXConfigHeaderMap = httpXConfigHeader == null ? null : (Map <String , Object >) objectMapper .readValue (httpXConfigHeader , Map .class );
178+ if (httpXConfigHeaderMap != null ) {
179+ options .keySet ().forEach (httpXConfigHeaderMap ::remove );
180+ options .putAll (httpXConfigHeaderMap );
181+ }
182+
183+ String httpAcceptHeader = request .getHeader ("Accept" );
184+ if (log .isInfoEnabled ()) log .info ("Incoming Accept: header string: " + httpAcceptHeader );
185+
186+ List <MediaType > httpAcceptMediaTypes = httpAcceptHeader == null ? null : MediaType .parseMediaTypes (httpAcceptHeader );
187+ if (httpAcceptMediaTypes != null ) MimeTypeUtils .sortBySpecificity (httpAcceptMediaTypes );
188+ if (httpAcceptHeader != null ) options .put ("_http_accept" , httpAcceptMediaTypes );
189+
190+ if (! options .containsKey ("accept" )) {
191+ if (isResolve ) {
192+ if (httpAcceptMediaTypes == null ) httpAcceptMediaTypes = Collections .singletonList (MediaType .parseMediaType (ResolveResult .MEDIA_TYPE ));
193+ options .put ("accept" , HttpBindingServerUtil .resolveAcceptForHttpAccepts (httpAcceptMediaTypes ));
194+ } else {
195+ if (httpAcceptMediaTypes == null ) httpAcceptMediaTypes = Collections .singletonList (MediaType .parseMediaType (DereferenceResult .MEDIA_TYPE ));
196+ options .put ("accept" , HttpBindingServerUtil .dereferenceAcceptForHttpAccepts (httpAcceptMediaTypes ));
197+ }
198+ }
199+
200+ if (log .isDebugEnabled ()) log .debug ("Using options: " + options );
201+
202+ // execute
203+
204+ this .execute (response , identifier , options , httpAcceptMediaTypes , isResolve );
205+ }
206+
207+ private void execute (HttpServletResponse response , String identifier , Map <String , Object > options , List <MediaType > httpAcceptMediaTypes , boolean isResolve ) throws IOException {
208+
112209 // execute the request
113210
114211 Result result ;
@@ -233,6 +330,6 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
233330 ServletUtil .sendResponse (
234331 response ,
235332 HttpServletResponse .SC_NOT_ACCEPTABLE ,
236- "Not acceptable media types " + httpAcceptHeader );
333+ "Not acceptable media types " + httpAcceptMediaTypes );
237334 }
238335}
0 commit comments