All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.apitrary.api.client.util.RequestUtil Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
/*
 * Copyright 2012 Denis Neuling 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package com.apitrary.api.client.util;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.apache.cxf.jaxrs.client.WebClient;

import com.apitrary.api.annotation.Body;
import com.apitrary.api.annotation.Query;
import com.apitrary.api.annotation.Required;
import com.apitrary.api.client.exception.ValidationConstraintViolationException;
import com.apitrary.api.client.exception.ValidationConstraintViolationException.ConstraintViolation;
import com.apitrary.api.request.Request;
import com.apitrary.api.response.Response;

/**
 * 

* RequestUtil class. *

* * @author Denis Neuling ([email protected]) * */ public class RequestUtil { private static final String EMPTY = "{}"; private static final String preMessage = "Request breaks constraints."; /** *

* getInstanceOfParameterizedType. *

* * @param request * a {@link com.apitrary.api.request.Request} object. * @param * a T object. * @return a {@link com.apitrary.api.response.Response} object. */ @SuppressWarnings("unchecked") public static Response getInstanceOfParameterizedType(Request request) { Type superclazz = request.getClass().getGenericSuperclass(); try { Type parameterizedTypeClazz = ((ParameterizedType) superclazz).getActualTypeArguments()[0]; return (Response) ((Class) parameterizedTypeClazz).newInstance(); } catch (Exception e) { throw new RuntimeException(e); } } /** *

* getRequestPayload. *

* * @param request * a {@link com.apitrary.api.request.Request} object. * @param * a T object. * @return a {@link java.lang.String} object. */ @SuppressWarnings("unused") public static String getRequestPayload(Request request) { Class referenceClazz = request.getClass(); List fields = ClassUtil.getAnnotatedFields(referenceClazz, Body.class); List objects = new LinkedList(); for (Field field : fields) { Body body = field.getAnnotation(Body.class); String asString = ClassUtil.getValueOf(field, request, referenceClazz, String.class); return (asString != null ? asString : ""); // what if there are more bodies?! } return EMPTY; } /** *

* resolveAndSetQueryPart. *

* * @param request * a {@link com.apitrary.api.request.Request} object. * @param webClient * a {@link org.apache.cxf.jaxrs.client.WebClient} object. * @param * a T object. * @return a {@link org.apache.cxf.jaxrs.client.WebClient} object. */ public static WebClient resolveAndSetQueryPart(Request request, WebClient webClient) { HashMap queryParts = resolveQueryPart(request); Iterator iterator = queryParts.keySet().iterator(); if (!iterator.hasNext()) { return webClient; } else { while (iterator.hasNext()) { String key = iterator.next(); String value = queryParts.get(key); webClient = webClient.replaceQueryParam(key, value); } } return webClient; } /** *

* resolveQueryPart. *

* * @param request * a {@link com.apitrary.api.request.Request} object. * @param * a T object. * @return a {@link java.util.HashMap} object. */ public static HashMap resolveQueryPart(Request request) { HashMap queryParts = new HashMap(); Class referenceClazz = request.getClass(); List fields = ClassUtil.getAnnotatedFields(referenceClazz, Query.class); for (Field field : fields) { Query query = field.getAnnotation(Query.class); String key = query.value(); // in case the value() is null or empty: continue if (key == null || (key != null && key.isEmpty())) { continue; } String value = ClassUtil.getValueOf(field, request, referenceClazz, String.class); if (value != null) { queryParts.put(key, value); } } return queryParts; } /** *

* validate. *

* * @param request * a {@link com.apitrary.api.request.Request} object. * @param * a T object. * @throws com.apitrary.api.client.exception.ValidationConstraintViolationException * if any. */ public static void validate(Request request) throws ValidationConstraintViolationException { if (request != null) { Class clazz = request.getClass(); List leafs = new LinkedList(); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { if (null != field.getAnnotation(Required.class)) { field.setAccessible(true); Object value = null; try { value = field.get(request); } catch (Exception e) { // not cool... throw new ValidationConstraintViolationException(e); } if (value == null || value instanceof String && ((String) value).isEmpty()) { ConstraintViolation violation = ConstraintViolation.newConstraintViolation("@" + Required.class.getSimpleName(), field); leafs.add(violation); } } } if (!leafs.isEmpty()) { throw new ValidationConstraintViolationException(preMessage, leafs); } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy