1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.web

File XWikiServletRequestStub.java

 

Coverage histogram

../../../../img/srcFileCovDistChart5.png
76% of files have more coverage

Code metrics

38
136
83
1
712
519
102
0.75
1.64
83
1.23

Classes

Class Line # Actions
XWikiServletRequestStub 58 136 0% 102 131
0.4902723749%
 

Contributing tests

This file is covered by 13 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package com.xpn.xwiki.web;
21   
22    import java.io.BufferedReader;
23    import java.io.IOException;
24    import java.io.UnsupportedEncodingException;
25    import java.net.URL;
26    import java.security.Principal;
27    import java.util.Arrays;
28    import java.util.Collection;
29    import java.util.Collections;
30    import java.util.Enumeration;
31    import java.util.LinkedHashMap;
32    import java.util.Locale;
33    import java.util.Map;
34    import java.util.Vector;
35   
36    import javax.servlet.AsyncContext;
37    import javax.servlet.DispatcherType;
38    import javax.servlet.RequestDispatcher;
39    import javax.servlet.ServletContext;
40    import javax.servlet.ServletException;
41    import javax.servlet.ServletInputStream;
42    import javax.servlet.ServletRequest;
43    import javax.servlet.ServletResponse;
44    import javax.servlet.http.Cookie;
45    import javax.servlet.http.HttpServletRequest;
46    import javax.servlet.http.HttpServletResponse;
47    import javax.servlet.http.HttpSession;
48    import javax.servlet.http.Part;
49   
50    import org.apache.commons.lang3.ArrayUtils;
51   
52    /**
53    * This stub is intended to simulate a servlet request in a daemon context, in order to be able to create a custom XWiki
54    * context. This trick is used in to give a daemon thread access to the XWiki api.
55    *
56    * @version $Id: ffa40e223b9c292cc75da66935b3d24f7ce1e119 $
57    */
 
58    public class XWikiServletRequestStub implements XWikiRequest
59    {
60    private boolean secure;
61   
62    private String scheme;
63   
64    private String protocol;
65   
66    private String queryString;
67   
68    private String contextPath;
69   
70    private String servletPath;
71   
72    private String serverName;
73   
74    private int serverPort;
75   
76    private Vector<String> headerNames;
77   
78    private Map<String, Vector<String>> headers;
79   
80    private Map<String, String[]> parameters;
81   
82    private String requestURI;
83   
84    private StringBuffer requestURL;
85   
86    private boolean daemon = true;
87   
 
88  41 toggle public XWikiServletRequestStub()
89    {
90    }
91   
92    /**
93    * @since 8.4RC1
94    */
 
95  0 toggle public XWikiServletRequestStub(URL requestURL, Map<String, String[]> requestParameters)
96    {
97  0 this(requestURL, null, requestParameters);
98    }
99   
100    /**
101    * @since 10.11.1
102    * @since 11.0
103    */
 
104  21272 toggle public XWikiServletRequestStub(URL requestURL, String contextPath, Map<String, String[]> requestParameters)
105    {
106  21272 if (requestURL != null) {
107  21271 this.protocol = requestURL.getProtocol();
108  21274 this.scheme = requestURL.getProtocol();
109   
110  21273 this.serverName = requestURL.getHost();
111  21275 this.serverPort = requestURL.getPort();
112   
113  21277 this.secure = this.protocol.equalsIgnoreCase("https");
114   
115  21273 this.requestURI = requestURL.getPath();
116  21275 this.requestURL = new StringBuffer(requestURL.toString());
117   
118  21279 setHost(requestURL.getHost());
119    }
120   
121  21276 this.contextPath = contextPath;
122   
123  21276 this.parameters = clone(requestParameters);
124    }
125   
126    /**
127    * @param request the request to copy
128    * @since 10.7RC1
129    */
 
130  138753 toggle public XWikiServletRequestStub(XWikiRequest request)
131    {
132  138753 this.secure = request.isSecure();
133  138753 this.protocol = request.getProtocol();
134  138753 this.scheme = request.getScheme();
135  138753 this.serverName = request.getServerName();
136  138753 this.serverPort = request.getServerPort();
137   
138  138753 this.contextPath = request.getContextPath();
139  138752 this.servletPath = request.getServletPath();
140   
141  138753 this.queryString = request.getQueryString();
142   
143  138749 this.requestURI = request.getRequestURI();
144  138748 this.requestURL = new StringBuffer(request.getRequestURL());
145   
146  138753 this.headerNames = new Vector<>();
147  138753 this.headers = new LinkedHashMap<>();
148  416055 for (Enumeration<String> enumeration = request.getHeaderNames(); enumeration.hasMoreElements();) {
149  277317 String headerName = enumeration.nextElement();
150   
151  277319 this.headerNames.addElement(headerName);
152  277323 Vector<String> values = new Vector<>();
153  554638 for (Enumeration<String> e2 = request.getHeaders(headerName); e2.hasMoreElements();) {
154  277310 values.addElement(e2.nextElement());
155    }
156  277327 this.headers.put(headerName.toLowerCase(), values);
157    }
158   
159  138747 this.parameters = clone(request.getParameterMap());
160    }
161   
 
162  319843 toggle private Map<String, String[]> clone(Map<String, String[]> map)
163    {
164  319899 Map<String, String[]> clone;
165  319888 if (this.parameters != null) {
166  0 clone = new LinkedHashMap<>(map.size());
167  0 for (Map.Entry<String, String[]> entry : map.entrySet()) {
168  0 clone.put(entry.getKey(), entry.getValue().clone());
169    }
170    } else {
171  319888 clone = null;
172    }
173   
174  319901 return clone;
175    }
176   
 
177  2 toggle public void setContextPath(String contextPath)
178    {
179  2 this.contextPath = contextPath;
180    }
181   
 
182  21279 toggle public void setHost(String host)
183    {
184  21278 if (this.headers == null) {
185  21278 this.headers = new LinkedHashMap<>();
186    }
187   
188  21277 this.headers.put("x-forwarded-host", new Vector<String>(Arrays.asList(host)));
189    }
190   
 
191  8 toggle public void setScheme(String scheme)
192    {
193  8 this.scheme = scheme;
194    }
195   
196    /**
197    * @since 7.1RC1
198    * @since 6.4.5
199    */
 
200  2 toggle public void setrequestURL(StringBuffer requestURL)
201    {
202  2 this.requestURL = requestURL;
203    }
204   
205    /**
206    * @since 7.2M2
207    */
 
208  2 toggle public void setRequestURI(String requestURI)
209    {
210  2 this.requestURI = requestURI;
211    }
212   
213    /**
214    * @since 7.1RC1
215    * @since 6.4.5
216    */
 
217  2 toggle public void setServerName(String serverName)
218    {
219  2 this.serverName = serverName;
220    }
221   
 
222  174457 toggle @Override
223    public String getHeader(String s)
224    {
225  174468 if (this.headers != null) {
226  174321 Vector<String> values = this.headers.get(s.toLowerCase());
227   
228  174320 if (values != null) {
229  21265 values.get(0);
230    }
231    }
232   
233  174466 return null;
234    }
235   
236    /**
237    * @since 7.3M1
238    */
 
239  0 toggle public void put(String key, String value)
240    {
241  0 if (this.parameters == null) {
242  0 this.parameters = new LinkedHashMap<>();
243    }
244   
245  0 String[] values = this.parameters.get(key);
246  0 if (values == null) {
247  0 values = new String[] { value };
248    } else {
249  0 values = ArrayUtils.add(values, value);
250    }
251  0 this.parameters.put(key, values);
252    }
253   
 
254  21184 toggle @Override
255    public String get(String name)
256    {
257  21187 return getParameter(name);
258    }
259   
 
260  42515 toggle @Override
261    public HttpServletRequest getHttpServletRequest()
262    {
263  42520 return this;
264    }
265   
 
266  0 toggle @Override
267    public Cookie getCookie(String cookieName)
268    {
269  0 return null;
270    }
271   
 
272  0 toggle @Override
273    public String getAuthType()
274    {
275  0 return null;
276    }
277   
 
278  2510 toggle @Override
279    public Cookie[] getCookies()
280    {
281  2510 return new Cookie[0];
282    }
283   
 
284  2 toggle @Override
285    public long getDateHeader(String s)
286    {
287  2 return 0;
288    }
289   
 
290  276995 toggle @Override
291    public Enumeration<String> getHeaders(String s)
292    {
293  277015 if (this.headers != null) {
294  277015 Vector<String> values = this.headers.get(s.toLowerCase());
295   
296  277014 if (values != null) {
297  277013 return values.elements();
298    }
299    }
300   
301  0 return Collections.emptyEnumeration();
302    }
303   
 
304  138679 toggle @Override
305    public Enumeration<String> getHeaderNames()
306    {
307  138685 return this.headerNames != null ? this.headerNames.elements() : Collections.emptyEnumeration();
308    }
309   
 
310  0 toggle @Override
311    public int getIntHeader(String s)
312    {
313  0 String header = getHeader(s);
314   
315  0 return header != null ? Integer.parseInt(header) : -1;
316    }
317   
 
318  0 toggle @Override
319    public String getMethod()
320    {
321  0 return null;
322    }
323   
 
324  0 toggle @Override
325    public String getPathInfo()
326    {
327  0 return null;
328    }
329   
 
330  0 toggle @Override
331    public String getPathTranslated()
332    {
333  0 return null;
334    }
335   
 
336  181665 toggle @Override
337    public String getContextPath()
338    {
339  181667 return this.contextPath;
340    }
341   
 
342  159905 toggle @Override
343    public String getQueryString()
344    {
345  159909 return this.queryString;
346    }
347   
 
348  0 toggle @Override
349    public String getRemoteUser()
350    {
351  0 return null;
352    }
353   
 
354  0 toggle @Override
355    public boolean isUserInRole(String s)
356    {
357  0 return false;
358    }
359   
 
360  0 toggle @Override
361    public Principal getUserPrincipal()
362    {
363  0 return null;
364    }
365   
 
366  0 toggle @Override
367    public String getRequestedSessionId()
368    {
369  0 return null;
370    }
371   
 
372  159903 toggle @Override
373    public String getRequestURI()
374    {
375  159911 return this.requestURI;
376    }
377   
 
378  138722 toggle @Override
379    public StringBuffer getRequestURL()
380    {
381  138725 return this.requestURL == null ? new StringBuffer() : this.requestURL;
382    }
383   
 
384  212946 toggle @Override
385    public String getServletPath()
386    {
387  212949 return this.servletPath;
388    }
389   
 
390  0 toggle @Override
391    public HttpSession getSession(boolean b)
392    {
393  0 return null;
394    }
395   
 
396  0 toggle @Override
397    public HttpSession getSession()
398    {
399  0 return null;
400    }
401   
 
402  0 toggle @Override
403    public boolean isRequestedSessionIdValid()
404    {
405  0 return false;
406    }
407   
 
408  0 toggle @Override
409    public boolean isRequestedSessionIdFromCookie()
410    {
411  0 return false;
412    }
413   
 
414  0 toggle @Override
415    public boolean isRequestedSessionIdFromURL()
416    {
417  0 return false;
418    }
419   
420    /**
421    * @deprecated
422    */
 
423  0 toggle @Override
424    @Deprecated
425    public boolean isRequestedSessionIdFromUrl()
426    {
427  0 return false;
428    }
429   
 
430  21189 toggle @Override
431    public Object getAttribute(String s)
432    {
433  21187 return null;
434    }
435   
 
436  2 toggle @Override
437    public Enumeration<String> getAttributeNames()
438    {
439  2 return Collections.emptyEnumeration();
440    }
441   
 
442  0 toggle @Override
443    public String getCharacterEncoding()
444    {
445  0 return null;
446    }
447   
 
448  0 toggle @Override
449    public void setCharacterEncoding(String s) throws UnsupportedEncodingException
450    {
451   
452    }
453   
 
454  0 toggle @Override
455    public int getContentLength()
456    {
457  0 return 0;
458    }
459   
 
460  0 toggle @Override
461    public String getContentType()
462    {
463  0 return null;
464    }
465   
 
466  0 toggle @Override
467    public ServletInputStream getInputStream() throws IOException
468    {
469  0 return null;
470    }
471   
 
472  491087 toggle @Override
473    public String getParameter(String s)
474    {
475  493932 if (this.parameters != null) {
476  0 String[] values = this.parameters.get(s);
477   
478  0 if (ArrayUtils.isNotEmpty(values)) {
479  0 return values[0];
480    }
481    }
482   
483  493935 return null;
484    }
485   
 
486  0 toggle @Override
487    public Enumeration<String> getParameterNames()
488    {
489  0 return this.parameters != null ? Collections.enumeration(this.parameters.keySet()) : null;
490    }
491   
 
492  0 toggle @Override
493    public String[] getParameterValues(String s)
494    {
495  0 if (this.parameters != null) {
496  0 String[] values = this.parameters.get(s);
497   
498  0 return values != null ? values.clone() : null;
499    }
500   
501  0 return ArrayUtils.EMPTY_STRING_ARRAY;
502    }
503   
 
504  159881 toggle @Override
505    public Map<String, String[]> getParameterMap()
506    {
507  159894 return clone(this.parameters);
508    }
509   
 
510  138685 toggle @Override
511    public String getProtocol()
512    {
513  138685 return this.protocol;
514    }
515   
 
516  181665 toggle @Override
517    public String getScheme()
518    {
519  181670 return this.scheme;
520    }
521   
 
522  181672 toggle @Override
523    public String getServerName()
524    {
525  181672 return this.serverName;
526    }
527   
 
528  181671 toggle @Override
529    public int getServerPort()
530    {
531  181672 return this.serverPort;
532    }
533   
 
534  0 toggle @Override
535    public BufferedReader getReader() throws IOException
536    {
537  0 return null;
538    }
539   
 
540  0 toggle @Override
541    public String getRemoteAddr()
542    {
543  0 return null;
544    }
545   
 
546  0 toggle @Override
547    public String getRemoteHost()
548    {
549  0 return null;
550    }
551   
 
552  2 toggle @Override
553    public void setAttribute(String s, Object o)
554    {
555   
556    }
557   
 
558  0 toggle @Override
559    public void removeAttribute(String s)
560    {
561   
562    }
563   
 
564  0 toggle @Override
565    public Locale getLocale()
566    {
567  0 return null;
568    }
569   
 
570  0 toggle @Override
571    public Enumeration<Locale> getLocales()
572    {
573  0 return null;
574    }
575   
 
576  181642 toggle @Override
577    public boolean isSecure()
578    {
579  181643 return this.secure;
580    }
581   
 
582  0 toggle @Override
583    public RequestDispatcher getRequestDispatcher(String s)
584    {
585  0 return null;
586    }
587   
588    /**
589    * @deprecated
590    */
 
591  0 toggle @Override
592    @Deprecated
593    public String getRealPath(String s)
594    {
595  0 return null;
596    }
597   
 
598  0 toggle @Override
599    public int getRemotePort()
600    {
601  0 return 0;
602    }
603   
 
604  0 toggle @Override
605    public String getLocalName()
606    {
607  0 return null;
608    }
609   
 
610  0 toggle @Override
611    public String getLocalAddr()
612    {
613  0 return null;
614    }
615   
 
616  0 toggle @Override
617    public int getLocalPort()
618    {
619  0 return 0;
620    }
621   
 
622  0 toggle @Override
623    public boolean authenticate(HttpServletResponse httpServletResponse) throws IOException, ServletException
624    {
625  0 return false;
626    }
627   
 
628  0 toggle @Override
629    public void login(String s, String s1) throws ServletException
630    {
631    }
632   
 
633  0 toggle @Override
634    public void logout() throws ServletException
635    {
636   
637    }
638   
 
639  0 toggle @Override
640    public Collection<Part> getParts() throws IOException, ServletException
641    {
642  0 return null;
643    }
644   
 
645  0 toggle @Override
646    public Part getPart(String s) throws IOException, ServletException
647    {
648  0 return null;
649    }
650   
 
651  0 toggle @Override
652    public ServletContext getServletContext()
653    {
654  0 return null;
655    }
656   
 
657  0 toggle @Override
658    public AsyncContext startAsync() throws IllegalStateException
659    {
660  0 return null;
661    }
662   
 
663  0 toggle @Override
664    public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse)
665    throws IllegalStateException
666    {
667  0 return null;
668    }
669   
 
670  0 toggle @Override
671    public boolean isAsyncStarted()
672    {
673  0 return false;
674    }
675   
 
676  0 toggle @Override
677    public boolean isAsyncSupported()
678    {
679  0 return false;
680    }
681   
 
682  0 toggle @Override
683    public AsyncContext getAsyncContext()
684    {
685  0 return null;
686    }
687   
 
688  0 toggle @Override
689    public DispatcherType getDispatcherType()
690    {
691  0 return null;
692    }
693   
694    /**
695    * @return true if the request is intended to be used in a long standing daemon thread (mails, etc.) and should not
696    * be taken into account when generating a URL
697    * @since 10.11RC1
698    */
 
699  21756 toggle public boolean isDaemon()
700    {
701  21761 return this.daemon;
702    }
703   
704    /**
705    * @param daemon the daemon to set
706    * @since 10.11RC1
707    */
 
708  21181 toggle public void setDaemon(boolean daemon)
709    {
710  21188 this.daemon = daemon;
711    }
712    }