com.wavemaker.commons.util.WMUtils Maven / Gradle / Ivy
/**
* Copyright (C) 2020 WaveMaker, 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.wavemaker.commons.util;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import com.wavemaker.commons.CommonConstants;
import com.wavemaker.commons.MessageResource;
import com.wavemaker.commons.WMRuntimeException;
import com.wavemaker.commons.wrapper.BooleanWrapper;
import com.wavemaker.commons.wrapper.IntegerWrapper;
import com.wavemaker.commons.wrapper.StringWrapper;
/**
* @author Uday Shankar
*/
public class WMUtils {
private static Boolean isSharedLibSetup;
public static final String SUCCESS = "success";
public static final StringWrapper SUCCESS_RESPONSE = new StringWrapper(SUCCESS);
private static final Logger logger = LoggerFactory.getLogger(WMUtils.class);
private WMUtils() {
}
public static String getFileExtensionFromFileName(String fileName) {
int indexOfDot = fileName.lastIndexOf('.');
return (indexOfDot == -1) ? "":fileName.substring(indexOfDot + 1);
}
public static String decodeRequestURI(String requestURI) {
try {
return URLDecoder.decode(requestURI, CommonConstants.UTF8);
} catch (UnsupportedEncodingException e) {
throw new WMRuntimeException(MessageResource.create("com.wavemaker.commons.failed.to.decode.request.uri"), e);
}
}
public static boolean isXmlMediaType(MediaType mediaType) {
return MediaType.APPLICATION_XML.equals(mediaType) || MediaType.TEXT_XML.equals(mediaType) || MediaType.APPLICATION_ATOM_XML.equals(mediaType);
}
public static boolean isJsonMediaType(MediaType mediaType) {
return MediaType.APPLICATION_JSON.equals(mediaType);
}
public static String[] getStringList(Object obj) {
if (obj instanceof String) {
return new String[]{(String) obj};
}
if (obj instanceof String[]) {
return (String[]) obj;
}
if (obj instanceof List) {
List o = (List) obj;
return (String[]) o.toArray(new String[]{});
}
throw new WMRuntimeException(MessageResource.create("com.wavemaker.commons.unsupported.object.type"), obj.getClass());
}
public static boolean areObjectsEqual(Object o1, Object o2) {
if(o1 == o2) {
return true;
}
if(o1 == null || o2 == null) {
return false;
}
return o1.equals(o2);
}
public static StringWrapper wrapString(String response) {
return new StringWrapper(response);
}
public static IntegerWrapper wrapInteger(Integer response) {
return new IntegerWrapper(response);
}
public static BooleanWrapper wrapBoolean(Boolean response) {
return new BooleanWrapper(response);
}
public static boolean isSharedLibSetup() {
if (isSharedLibSetup == null) {
Class klass = Assert.class;
if (klass.getClassLoader() == WMUtils.class.getClassLoader()) {
isSharedLibSetup = false;
logger.info("Using classes from the webapp class loader {}", WMUtils.class.getClassLoader());
} else {
isSharedLibSetup = true;
logger.info("Using classes from the jars of the shared library");
}
}
return isSharedLibSetup;
}
}