001    /*
002     * Copyright 2011-2012 UnboundID Corp.
003     *
004     * This program is free software; you can redistribute it and/or modify
005     * it under the terms of the GNU General Public License (GPLv2 only)
006     * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
007     * as published by the Free Software Foundation.
008     *
009     * This program is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     * GNU General Public License for more details.
013     *
014     * You should have received a copy of the GNU General Public License
015     * along with this program; if not, see <http://www.gnu.org/licenses>.
016     */
017    
018    package com.unboundid.scim.wink;
019    
020    import javax.servlet.ServletContext;
021    import javax.ws.rs.core.HttpHeaders;
022    import javax.ws.rs.core.MediaType;
023    import javax.ws.rs.core.SecurityContext;
024    import javax.ws.rs.core.UriInfo;
025    import java.security.Principal;
026    import java.util.List;
027    
028    import static com.unboundid.scim.sdk.SCIMConstants.*;
029    
030    
031    
032    public class RequestContext
033    {
034      /**
035       * The servlet context of the request.
036       */
037      private final ServletContext servletContext;
038    
039      /**
040       * The security context of the request.
041       */
042      private final SecurityContext securityContext;
043    
044      /**
045       * The HTTP headers of the request.
046       */
047      private final HttpHeaders headers;
048    
049      /**
050       * The URI information of the request.
051       */
052      private final UriInfo uriInfo;
053    
054      /**
055       * The HTTP authenticated user ID. Not to be confused with a SCIM user ID.
056       */
057      private final String authID;
058    
059      /**
060       * The value of the HTTP Origin header.
061       */
062      private final String origin;
063    
064      /**
065       * The media type to be consumed, if any.
066       */
067      private final MediaType consumeMediaType;
068    
069      /**
070       * The media type to be produced, if any.
071       */
072      private final MediaType produceMediaType;
073    
074      /**
075       * The value of the HTTP Content-Length header.
076       */
077      private final long contentLength;
078    
079    
080    
081      /**
082       * Creates the resource implementation for a request.
083       *
084       * @param servletContext   The servlet context for the request.
085       * @param securityContext  The security context for the request.
086       * @param headers          The request headers.
087       * @param uriInfo          The URI info for the request.
088       * @param consumeMediaType The media type to be consumed, if any.
089       * @param produceMediaType The media type to be produced, if any.
090       */
091      public RequestContext(final ServletContext servletContext,
092                            final SecurityContext securityContext,
093                            final HttpHeaders headers,
094                            final UriInfo uriInfo,
095                            final MediaType consumeMediaType,
096                            final MediaType produceMediaType)
097      {
098        this.servletContext   = servletContext;
099        this.securityContext  = securityContext;
100        this.headers          = headers;
101        this.uriInfo          = uriInfo;
102        this.consumeMediaType = consumeMediaType;
103        this.produceMediaType = produceMediaType;
104    
105        // Determine the authenticated ID for the request.
106        final Principal userPrincipal = securityContext.getUserPrincipal();
107        if (userPrincipal != null)
108        {
109          authID = userPrincipal.getName();
110        }
111        else
112        {
113          authID = null;
114        }
115    
116        final List<String> originHeaders =
117            headers.getRequestHeader(HEADER_NAME_ORIGIN);
118        if (originHeaders != null)
119        {
120          origin = originHeaders.get(0);
121        }
122        else
123        {
124          origin = null;
125        }
126    
127        final List<String> contentLengthHeaders =
128            headers.getRequestHeader(HttpHeaders.CONTENT_LENGTH);
129        if (contentLengthHeaders != null)
130        {
131          contentLength = Long.parseLong(contentLengthHeaders.get(0));
132        }
133        else
134        {
135          contentLength = -1;
136        }
137      }
138    
139    
140    
141      /**
142       * Retrieve the servlet context of the request.
143       * @return The servlet context of the request.
144       */
145      public ServletContext getServletContext()
146      {
147        return servletContext;
148      }
149    
150    
151    
152      /**
153       * Retrieve the security context for the request.
154       * @return The security context for the request.
155       */
156      public SecurityContext getSecurityContext()
157      {
158        return securityContext;
159      }
160    
161    
162    
163      /**
164       * Retrieve the request headers.
165       * @return The request headers.
166       */
167      public HttpHeaders getHeaders()
168      {
169        return headers;
170      }
171    
172    
173    
174      /**
175       * Retrieve the URI info for the request.
176       * @return The URI info for the request.
177       */
178      public UriInfo getUriInfo()
179      {
180        return uriInfo;
181      }
182    
183    
184    
185      /**
186       * Retrieve the HTTP authenticated user ID.
187       * @return The the HTTP authenticated user ID.
188       */
189      public String getAuthID()
190      {
191        return authID;
192      }
193    
194    
195    
196      /**
197       * Retrieve the value of the HTTP Origin header.
198       * @return The value of the HTTP Origin header.
199       */
200      public String getOrigin()
201      {
202        return origin;
203      }
204    
205    
206    
207      /**
208       * Retrieve the media type to be consumed.
209       * @return  The media type to be consumed.
210       */
211      public MediaType getConsumeMediaType()
212      {
213        return consumeMediaType;
214      }
215    
216    
217    
218      /**
219       * Retrieve the media type to be produced.
220       * @return  The media type to be produced.
221       */
222      public MediaType getProduceMediaType()
223      {
224        return produceMediaType;
225      }
226    
227    
228    
229      /**
230       * Retrieve the value of the HTTP Content-Length header.
231       * @return The value of the HTTP Content-Length header, or -1 if it is
232       *         not present.
233       */
234      public long getContentLength()
235      {
236        return contentLength;
237      }
238    }