
com.formkiq.server.api.AbstractRestController Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of formkiq-server Show documentation
Show all versions of formkiq-server Show documentation
Server-side integration for the FormKiQ ios application
/*
* Copyright (C) 2016 FormKiQ Inc.
*
* 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.formkiq.server.api;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import com.formkiq.server.service.ServerRequestNotSupported;
import javassist.bytecode.stackmap.TypeData.ClassName;
/**
* Abstract Rest Services controller.
*
*/
public abstract class AbstractRestController {
/** Valid Accept Header. */
public static final MediaType ACCEPT_HEADER_V1
= MediaType.valueOf("application/vnd.formkiq.v1+json");
/** Admin Accept Header, skpis OAuth. */
public static final MediaType ACCEPT_HEADER_ADMIN
= MediaType.valueOf("application/vnd.formkiq.admin");
/** Logger. */
protected static final Logger LOG = Logger.getLogger(ClassName.class
.getName());
/**
* @param request HttpServletRequest
* @return int
*/
protected int getApiVersion(final HttpServletRequest request) {
String accept = request.getHeader("Accept");
if (accept.contains(ACCEPT_HEADER_V1.toString())) {
return 1;
}
throw new InvalidMediaTypeException(
"invalid accept header, must be " + ACCEPT_HEADER_V1);
}
/**
* Get First Value in Map.
* @param map {@link Map}
* @param key {@link String}
* @return {@link String}
*/
protected String getFirstValue(final Map map,
final String key) {
String[] values = map.get(key);
if (values != null && values.length > 0) {
return values[0];
}
return null;
}
/**
* Get Parameter from Request.
* @param map {@link Map}
* @param param {@link String}
* @param required boolean
* @return {@link String}
*/
protected String getParameter(final Map map,
final String param, final boolean required) {
String value = getFirstValue(map, param);
if (StringUtils.isEmpty(value) && required) {
throw new ServerRequestNotSupported();
}
return value;
}
/**
* Get Parameter from Request.
* @param Enum type parameter
* @param map {@link Map}
* @param param {@link String}
* @param defaultValue {@link Enum}
* @param e {@link Enum}
* @return {@link Enum} Enum
*/
protected > List getParameters(
final Map map, final String param,
final List defaultValue, final Class e) {
List list = new ArrayList();
String[] values = map.get(param);
if (values != null && values.length > 0) {
for (String value : values) {
T t = getParameter(param, value, null, e);
if (t != null) {
list.add(t);
}
}
}
return list.isEmpty() ? defaultValue : list;
}
/**
* Get Parameter from Request.
* @param Enum type parameter
* @param map {@link Map}
* @param param {@link String}
* @param defaultValue {@link Enum}
* @param e {@link Enum}
* @return {@link Enum} Enum
*/
protected > T getParameter(
final Map map, final String param,
final T defaultValue, final Class e) {
String value = getFirstValue(map, param);
return getParameter(param, value, defaultValue, e);
}
/**
* Get Parameter from Request.
* @param Enum type parameter
* @param param {@link String}
* @param value {@link String}
* @param defaultValue {@link Enum}
* @param e {@link Enum}
* @return {@link Enum} Enum
*/
protected > T getParameter(final String param,
final String value, final T defaultValue, final Class e) {
if (StringUtils.isEmpty(value) && defaultValue != null) {
return defaultValue;
}
try {
return Enum.valueOf(e, value.toUpperCase());
} catch (Exception ex) {
throw new ServerRequestNotSupported();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy