001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.rest.converters; 014 015import org.apache.juneau.rest.*; 016import org.apache.juneau.utils.*; 017 018/** 019 * Converter for enabling of {@link PojoQuery} support on response objects returned by a <code>@RestMethod</code> method. 020 * 021 * <p> 022 * When enabled, objects in a POJO tree can be filtered using the functionality described in the {@link PojoQuery} 023 * class. 024 * 025 * <p> 026 * The following HTTP request parameters are available for tabular data (e.g. {@code Collections} of {@code Maps}, 027 * arrays of beans, etc...): 028 * <ul class='spaced-list'> 029 * <li> 030 * <code>&s=</code> Search arguments. 031 * <br>Comma-delimited list of key/value pairs representing column names and search tokens. 032 * <br>Example: 033 * <p class='bcode'> 034 * &s=name=Bill*,birthDate>2000 035 * </p> 036 * <li> 037 * <code>&v=</code> Visible columns. 038 * <br>Comma-delimited list of column names to display. 039 * <br>Example: 040 * <p class='bcode'> 041 * &v=name,birthDate 042 * </p> 043 * <li> 044 * <code>&o=</code> Sort commands. 045 * <br>Comma-delimited list of columns to sort by. 046 * <br>Column names can be suffixed with <js>'+'</js> or <js>'-'</js> to indicate ascending or descending order. 047 * <br>The default is ascending order. 048 * <br>Example: 049 * <p class='bcode'> 050 * &o=name,birthDate- 051 * </p> 052 * <li> 053 * <code>&i=</code> Case-insensitive parameter. 054 * <br>Boolean flag for case-insensitive matching on the search parameters. 055 * <li> 056 * <code>&p=</code> - Position parameter. 057 * <br>Only return rows starting at the specified index position (zero-indexed). 058 * <br>Default is {@code 0}. 059 * <li> 060 * <code>&l=</code> Limit parameter. 061 * <br>Only return the specified number of rows. 062 * <br>Default is {@code 0} (meaning return all rows). 063 * </ul> 064 * 065 * <h5 class='section'>See Also:</h5> 066 * <ul> 067 * <li class='jc'>{@link PojoQuery} - Additional information on filtering POJO models. 068 * <li class='jf'>{@link RestContext#REST_converters} - Registering converters with REST resources. 069 * <li class='link'><a class="doclink" href="../../../../../overview-summary.html#juneau-rest-server.Converters">Overview > juneau-rest-server > Converters</a> 070 * </ul> 071 */ 072public final class Queryable implements RestConverter { 073 074 @Override /* RestConverter */ 075 public Object convert(RestRequest req, Object o) { 076 if (o == null) 077 return null; 078 SearchArgs searchArgs = req.getQuery().getSearchArgs(); 079 if (searchArgs == null) 080 return o; 081 return new PojoQuery(o, req.getBeanSession()).filter(searchArgs); 082 } 083}