001/* 002 * Copyright 2015-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.scim2.server.providers; 019 020import com.unboundid.scim2.common.messages.SearchRequest; 021import com.unboundid.scim2.common.utils.JsonUtils; 022import com.unboundid.scim2.common.utils.StaticUtils; 023import com.unboundid.scim2.server.utils.ServerUtils; 024 025import javax.ws.rs.HttpMethod; 026import javax.ws.rs.container.ContainerRequestContext; 027import javax.ws.rs.container.ContainerRequestFilter; 028import javax.ws.rs.container.PreMatching; 029import javax.ws.rs.core.MediaType; 030import javax.ws.rs.core.PathSegment; 031import javax.ws.rs.core.UriBuilder; 032import javax.ws.rs.ext.Provider; 033import java.io.IOException; 034import java.util.List; 035 036import static com.unboundid.scim2.common.utils.ApiConstants.*; 037 038/** 039 * A ContainerRequestFilter implementation to convert a search request using 040 * HTTP POST combine with the ".search" path extension to a regular search 041 * using HTTP GET. 042 */ 043@Provider 044@PreMatching 045public class DotSearchFilter implements ContainerRequestFilter 046{ 047 /** 048 * {@inheritDoc} 049 */ 050 public void filter(final ContainerRequestContext requestContext) 051 throws IOException 052 { 053 if(requestContext.getMethod().equals(HttpMethod.POST) && 054 requestContext.getUriInfo().getPath().endsWith( 055 SEARCH_WITH_POST_PATH_EXTENSION) && 056 (requestContext.getMediaType().isCompatible( 057 ServerUtils.MEDIA_TYPE_SCIM_TYPE) || 058 requestContext.getMediaType().isCompatible( 059 MediaType.APPLICATION_JSON_TYPE))) 060 { 061 SearchRequest searchRequest = 062 JsonUtils.getObjectReader().forType(SearchRequest.class).readValue( 063 requestContext.getEntityStream()); 064 UriBuilder builder = requestContext.getUriInfo().getBaseUriBuilder(); 065 List<PathSegment> pathSegments = 066 requestContext.getUriInfo().getPathSegments(); 067 for(int i = 0; i < pathSegments.size() - 1; i ++) 068 { 069 builder.path(pathSegments.get(i).getPath()); 070 } 071 if(searchRequest.getAttributes() != null) 072 { 073 builder.queryParam(QUERY_PARAMETER_ATTRIBUTES, 074 StaticUtils.collectionToString(searchRequest.getAttributes(), ",")); 075 } 076 if(searchRequest.getExcludedAttributes() != null) 077 { 078 builder.queryParam(QUERY_PARAMETER_EXCLUDED_ATTRIBUTES, 079 StaticUtils.collectionToString( 080 searchRequest.getExcludedAttributes(), ",")); 081 } 082 if(searchRequest.getFilter() != null) 083 { 084 builder.queryParam(QUERY_PARAMETER_FILTER, 085 searchRequest.getFilter()); 086 } 087 if(searchRequest.getSortBy() != null) 088 { 089 builder.queryParam(QUERY_PARAMETER_SORT_BY, 090 searchRequest.getSortBy()); 091 } 092 if(searchRequest.getSortOrder() != null) 093 { 094 builder.queryParam(QUERY_PARAMETER_SORT_ORDER, 095 searchRequest.getSortOrder().getName()); 096 } 097 if(searchRequest.getStartIndex() != null) 098 { 099 builder.queryParam(QUERY_PARAMETER_PAGE_START_INDEX, 100 searchRequest.getStartIndex()); 101 } 102 if(searchRequest.getCount() != null) 103 { 104 builder.queryParam(QUERY_PARAMETER_PAGE_SIZE, 105 searchRequest.getCount()); 106 } 107 requestContext.setRequestUri(builder.build()); 108 requestContext.setMethod(HttpMethod.GET); 109 } 110 } 111}