Search in sources :

Example 1 with QueryParam

use of com.lonepulse.robozombie.annotation.QueryParam in project RoboZombie by sahan.

the class QueryParamProcessor method process.

/**
	 * <p>Accepts the {@link InvocationContext} along with an {@link HttpRequestBase} and creates a 
	 * <a href="http://en.wikipedia.org/wiki/Query_string">query string</a> using arguments annotated 
	 * with @{@link QueryParam} and @{@link QueryParams}; which is subsequently appended to the URI.</p>
	 * 
	 * <p>See {@link AbstractRequestProcessor#process(InvocationContext, HttpRequestBase)}.</p>
	 * 
	 * @param context
	 * 			the {@link InvocationContext} which is used to discover annotated query parameters
	 * <br><br>
	 * @param request
	 * 			prefers an instance of {@link HttpGet} so as to conform with HTTP 1.1; however, other 
	 * 			request types will be entertained to allow compliance with unusual endpoint definitions 
	 * <br><br>
 	 * @return the same instance of {@link HttpRequestBase} which was given for processing query parameters
	 * <br><br>
	 * @throws RequestProcessorException
	 * 			if the creation of a query string failed due to an unrecoverable errorS
	 * <br><br>
	 * @since 1.3.0
	 */
@Override
protected HttpRequestBase process(InvocationContext context, HttpRequestBase request) {
    try {
        URIBuilder uriBuilder = new URIBuilder(request.getURI());
        //add static name and value pairs
        List<Param> constantQueryParams = RequestUtils.findStaticQueryParams(context);
        for (Param param : constantQueryParams) {
            uriBuilder.setParameter(param.name(), param.value());
        }
        //add individual name and value pairs
        List<Entry<QueryParam, Object>> queryParams = Metadata.onParams(QueryParam.class, context);
        for (Entry<QueryParam, Object> entry : queryParams) {
            String name = entry.getKey().value();
            Object value = entry.getValue();
            if (!(value instanceof CharSequence)) {
                StringBuilder errorContext = new StringBuilder().append("Query parameters can only be of type ").append(CharSequence.class.getName()).append(". Please consider implementing CharSequence ").append("and providing a meaningful toString() representation for the ").append("<name> of the query parameter. ");
                throw new RequestProcessorException(new IllegalArgumentException(errorContext.toString()));
            }
            uriBuilder.setParameter(name, ((CharSequence) value).toString());
        }
        //add batch name and value pairs (along with any static params)
        List<Entry<QueryParams, Object>> queryParamMaps = Metadata.onParams(QueryParams.class, context);
        for (Entry<QueryParams, Object> entry : queryParamMaps) {
            Param[] constantParams = entry.getKey().value();
            if (constantParams != null && constantParams.length > 0) {
                for (Param param : constantParams) {
                    uriBuilder.setParameter(param.name(), param.value());
                }
            }
            Object map = entry.getValue();
            if (!(map instanceof Map)) {
                StringBuilder errorContext = new StringBuilder().append("@QueryParams can only be applied on <java.util.Map>s. ").append("Please refactor the method to provide a Map of name and value pairs. ");
                throw new RequestProcessorException(new IllegalArgumentException(errorContext.toString()));
            }
            Map<?, ?> nameAndValues = (Map<?, ?>) map;
            for (Entry<?, ?> nameAndValue : nameAndValues.entrySet()) {
                Object name = nameAndValue.getKey();
                Object value = nameAndValue.getValue();
                if (!(name instanceof CharSequence && (value instanceof CharSequence || value instanceof Collection))) {
                    StringBuilder errorContext = new StringBuilder().append("The <java.util.Map> identified by @QueryParams can only contain mappings of type ").append("<java.lang.CharSequence, java.lang.CharSequence> or ").append("<java.lang.CharSequence, java.util.Collection<? extends CharSequence>>");
                    throw new RequestProcessorException(new IllegalArgumentException(errorContext.toString()));
                }
                if (value instanceof CharSequence) {
                    uriBuilder.addParameter(((CharSequence) name).toString(), ((CharSequence) value).toString());
                } else {
                    //add multi-valued query params 
                    Collection<?> multivalues = (Collection<?>) value;
                    for (Object multivalue : multivalues) {
                        if (!(multivalue instanceof CharSequence)) {
                            StringBuilder errorContext = new StringBuilder().append("Values for the <java.util.Map> identified by @QueryParams can only contain collections ").append("of type java.util.Collection<? extends CharSequence>");
                            throw new RequestProcessorException(new IllegalArgumentException(errorContext.toString()));
                        }
                        uriBuilder.addParameter(((CharSequence) name).toString(), ((CharSequence) multivalue).toString());
                    }
                }
            }
        }
        request.setURI(uriBuilder.build());
        return request;
    } catch (Exception e) {
        throw (e instanceof RequestProcessorException) ? (RequestProcessorException) e : new RequestProcessorException(context, getClass(), e);
    }
}
Also used : URIBuilder(org.apache.http42.client.utils.URIBuilder) Entry(java.util.Map.Entry) QueryParam(com.lonepulse.robozombie.annotation.QueryParam) QueryParam(com.lonepulse.robozombie.annotation.QueryParam) Param(com.lonepulse.robozombie.annotation.Param) Collection(java.util.Collection) QueryParams(com.lonepulse.robozombie.annotation.QueryParams) Map(java.util.Map)

Aggregations

Param (com.lonepulse.robozombie.annotation.Param)1 QueryParam (com.lonepulse.robozombie.annotation.QueryParam)1 QueryParams (com.lonepulse.robozombie.annotation.QueryParams)1 Collection (java.util.Collection)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 URIBuilder (org.apache.http42.client.utils.URIBuilder)1