001/*
002 * Copyright 2011-2016 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
018package com.unboundid.scim.wink;
019
020import com.unboundid.scim.sdk.OAuthTokenHandler;
021
022import javax.servlet.http.HttpServletRequest;
023import javax.ws.rs.Consumes;
024import javax.ws.rs.GET;
025import javax.ws.rs.POST;
026import javax.ws.rs.Path;
027import javax.ws.rs.PathParam;
028import javax.ws.rs.Produces;
029import javax.ws.rs.QueryParam;
030import javax.ws.rs.core.Context;
031import javax.ws.rs.core.HttpHeaders;
032import javax.ws.rs.core.MediaType;
033import javax.ws.rs.core.Response;
034import javax.ws.rs.core.SecurityContext;
035import javax.ws.rs.core.UriInfo;
036
037import java.io.InputStream;
038
039import static com.unboundid.scim.sdk.SCIMConstants.QUERY_PARAMETER_BASE_ID;
040import static com.unboundid.scim.sdk.SCIMConstants.QUERY_PARAMETER_FILTER;
041import static com.unboundid.scim.sdk.SCIMConstants.QUERY_PARAMETER_PAGE_SIZE;
042import static com.unboundid.scim.sdk.SCIMConstants.
043    QUERY_PARAMETER_PAGE_START_INDEX;
044import static com.unboundid.scim.sdk.SCIMConstants.QUERY_PARAMETER_SCOPE;
045import static com.unboundid.scim.sdk.SCIMConstants.QUERY_PARAMETER_SORT_BY;
046import static com.unboundid.scim.sdk.SCIMConstants.QUERY_PARAMETER_SORT_ORDER;
047
048
049
050/**
051 * This class is a Wink resource implementation for query operations
052 * on a SCIM resource where the client requests JSON response format in the URL
053 * by appending ".json" on to the endpoint.
054 */
055@Path("{endpoint}.json")
056public class JSONQueryResource extends AbstractSCIMResource
057{
058  /**
059   * Create a new SCIM wink resource for XML query operations on a SCIM
060   * endpoint.
061   *
062   * @param application         The SCIM JAX-RS application associated with this
063   *                            resource.
064   * @param tokenHandler        The token handler to use for OAuth
065   *                            authentication.
066   */
067  public JSONQueryResource(final SCIMApplication application,
068                           final OAuthTokenHandler tokenHandler)
069  {
070    super(application, tokenHandler);
071  }
072
073
074
075  /**
076   * Implement the GET operation producing JSON format.
077   *
078   * @param endpoint         The resource endpoint.
079   * @param request          The current HTTP servlet request.
080   * @param securityContext  The security context of the current request.
081   * @param headers          The request headers.
082   * @param uriInfo          The URI info for the request.
083   * @param filterString     The filter query parameter, or {@code null}.
084   * @param baseID           The SCIM resource ID of the search base entry,
085   *                         or {@code null}.
086   * @param searchScope      The LDAP search scope to use, or {@code null}.
087   * @param sortBy           The sortBy query parameter, or {@code null}.
088   * @param sortOrder        The sortOrder query parameter, or {@code null}.
089   * @param pageStartIndex   The startIndex query parameter, or {@code null}.
090   * @param pageSize         The count query parameter, or {@code null}.
091   *
092   * @return  The response to the request.
093   */
094  @GET
095  @Produces(MediaType.APPLICATION_JSON)
096  public Response doJsonGet(@PathParam("endpoint") final String endpoint,
097                            @Context final HttpServletRequest request,
098                            @Context final SecurityContext securityContext,
099                            @Context final HttpHeaders headers,
100                            @Context final UriInfo uriInfo,
101                            @QueryParam(QUERY_PARAMETER_FILTER)
102                            final String filterString,
103                            @QueryParam(QUERY_PARAMETER_BASE_ID)
104                            final String baseID,
105                            @QueryParam(QUERY_PARAMETER_SCOPE)
106                            final String searchScope,
107                            @QueryParam(QUERY_PARAMETER_SORT_BY)
108                            final String sortBy,
109                            @QueryParam(QUERY_PARAMETER_SORT_ORDER)
110                            final String sortOrder,
111                            @QueryParam(QUERY_PARAMETER_PAGE_START_INDEX)
112                            final String pageStartIndex,
113                            @QueryParam(QUERY_PARAMETER_PAGE_SIZE)
114                            final String pageSize)
115  {
116    final RequestContext requestContext =
117        new RequestContext(request, securityContext, headers, uriInfo,
118                           MediaType.APPLICATION_JSON_TYPE,
119                           MediaType.APPLICATION_JSON_TYPE);
120
121    return getUsers(requestContext, endpoint, filterString, baseID, searchScope,
122                    sortBy, sortOrder, pageStartIndex, pageSize);
123  }
124
125
126
127  /**
128   * Implement the POST operation consuming and producing JSON format.
129   *
130   * @param inputStream      The content to be consumed.
131   * @param endpoint         The resource endpoint.
132   * @param request          The current HTTP servlet request.
133   * @param securityContext  The security context for the request.
134   * @param headers          The request headers.
135   * @param uriInfo          The URI info for the request.
136   *
137   * @return  The response to the request.
138   */
139  @POST
140  @Consumes(MediaType.APPLICATION_JSON)
141  @Produces(MediaType.APPLICATION_JSON)
142  public Response doJsonDotJsonPost(final InputStream inputStream,
143                                    @PathParam("endpoint") final String
144                                        endpoint,
145                                    @Context final HttpServletRequest request,
146                                    @Context final SecurityContext
147                                        securityContext,
148                                    @Context final HttpHeaders headers,
149                                    @Context final UriInfo uriInfo)
150  {
151    final RequestContext requestContext =
152        new RequestContext(request, securityContext, headers, uriInfo,
153                           MediaType.APPLICATION_JSON_TYPE,
154                           MediaType.APPLICATION_JSON_TYPE);
155    return postUser(requestContext, endpoint, inputStream);
156  }
157
158
159
160  /**
161   * Implement the POST operation consuming XML and producing JSON format.
162   *
163   * @param inputStream      The content to be consumed.
164   * @param endpoint         The resource endpoint.
165   * @param request          The current HTTP servlet request.
166   * @param securityContext  The security context for the request.
167   * @param headers          The request headers.
168   * @param uriInfo          The URI info for the request.
169   *
170   * @return  The response to the request.
171   */
172  @POST
173  @Consumes(MediaType.APPLICATION_XML)
174  @Produces(MediaType.APPLICATION_JSON)
175  public Response doXmlDotJsonPost(final InputStream inputStream,
176                                    @PathParam("endpoint") final String
177                                        endpoint,
178                                    @Context final HttpServletRequest request,
179                                    @Context final SecurityContext
180                                        securityContext,
181                                    @Context final HttpHeaders headers,
182                                    @Context final UriInfo uriInfo)
183  {
184    final RequestContext requestContext =
185        new RequestContext(request, securityContext, headers, uriInfo,
186                           MediaType.APPLICATION_XML_TYPE,
187                           MediaType.APPLICATION_JSON_TYPE);
188    return postUser(requestContext, endpoint, inputStream);
189  }
190}