org.glassfish.admin.rest.utils.Util Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of payara-micro Show documentation
Show all versions of payara-micro Show documentation
Micro Distribution of the Payara Project
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2009-2013 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2019] Payara Foundation and/or affiliates
package org.glassfish.admin.rest.utils;
import com.sun.enterprise.util.LocalStringManagerImpl;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import com.sun.enterprise.util.io.FileUtils;
import org.glassfish.admin.rest.provider.ProviderUtil;
import javax.ws.rs.client.Client;
import javax.ws.rs.core.UriInfo;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Level;
import javax.security.auth.Subject;
import javax.ws.rs.core.PathSegment;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.HttpHeaders;
import org.glassfish.admin.rest.Constants;
import org.glassfish.admin.rest.RestLogging;
import org.glassfish.admin.rest.utils.xml.RestActionReporter;
import org.glassfish.admin.restconnector.RestConfig;
import org.glassfish.api.ActionReport.MessagePart;
import org.glassfish.api.Param;
import org.glassfish.api.admin.CommandModel;
import org.glassfish.api.admin.ParameterMap;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.internal.api.Globals;
import org.jvnet.hk2.config.ConfigModel;
/**
* Utilities class. Extended by ResourceUtil and ProviderUtil utilities. Used by
* resource and providers.
*
* @author Rajeshwar Patil
*/
public class Util {
private static final String JAVA_IO_TMPDIR = "java.io.tmpdir";
public static final LocalStringManagerImpl localStrings = new LocalStringManagerImpl(Util.class);
private static Client client;
private Util() {
}
public static void logTimingMessage(String msg) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS");
RestLogging.restLogger.log(Level.INFO, RestLogging.TIMESTAMP_MESSAGE, new Object[]{sdf.format(new Date()), msg});
}
/**
* Returns name of the resource from UriInfo.
*/
public static String getResourceName(UriInfo uriInfo) {
return upperCaseFirstLetter(eleminateHypen(getName(uriInfo.getPath(), '/')));
}
/**
* Returns name of the resource parent from UriInfo.
*/
public static String getParentName(UriInfo uriInfo) {
if (uriInfo == null) {
return null;
}
return getParentName(uriInfo.getPath());
}
public static String getGrandparentName(UriInfo uriInfo) {
if (uriInfo == null) {
return null;
}
return getGrandparentName(uriInfo.getPath());
}
/**
* Returns just the name of the given fully qualified name.
*/
public static String getName(String typeName) {
return getName(typeName, '.');
}
/**
* Returns just the name of the given fully qualified name.
*/
public static String getName(String typeName, char delimiter) {
if ((typeName == null) || ("".equals(typeName))) {
return typeName;
}
//elimiate last char from typeName if its a delimiter
if (typeName.length() - 1 == typeName.lastIndexOf(delimiter)) {
typeName = typeName.substring(0, typeName.length() - 1);
}
if ((typeName != null) && (typeName.length() > 0)) {
int index = typeName.lastIndexOf(delimiter);
if (index != -1) {
return typeName.substring(index + 1);
}
}
return typeName;
}
/**
* returns just the parent name of the resource from the resource url.
*/
public static String getParentName(String url) {
if ((url == null) || ("".equals(url))) {
return url;
}
String name = getName(url, '/');
// Find the : to skip past the protocal part of the URL, as that is causing
// problems with resources named 'http'.
int nameIndex = url.indexOf(name, url.indexOf(':') + 1);
return getName(url.substring(0, nameIndex - 1), '/');
}
public static String getGrandparentName(String url) {
if ((url == null) || ("".equals(url))) {
return url;
}
String name = getParentName(url);
// Find the : to skip past the protocal part of the URL, as that is causing
// problems with resources named 'http'.
int nameIndex = url.indexOf(name, url.indexOf(':') + 1);
return getName(url.substring(0, nameIndex - 1), '/');
}
/**
* Removes any hypens ( - ) from the given string.
* When it removes a hypen, it converts next immediate
* character, if any, to an Uppercase.(schema2beans convention)
* @param string the input string
* @return a String
resulted after removing the hypens
*/
public static String eleminateHypen(String string) {
if (!(string == null || string.length() <= 0)) {
int index = string.indexOf('-');
while (index != -1) {
if (index == 0) {
string = string.substring(1);
} else {
if (index == (string.length() - 1)) {
string = string.substring(0, string.length() - 1);
} else {
string = string.substring(0, index)
+ upperCaseFirstLetter(string.substring(index + 1));
}
}
index = string.indexOf('-');
}
}
return string;
}
/**
* Decodes an encoded URL using UTF-8
* @param string string to decode
* @return decoded string, or the original string if decoding failed
*/
public static String decode(String string) {
String ret = string;
try {
ret = URLDecoder.decode(string, "UTF-8");
} catch (UnsupportedEncodingException e) {
//UTF-8 *Should* be supported by every system
}
return ret;
}
/**
* Converts the first letter of the given string to Uppercase.
*
* @param string the input string
* @return the string with the Uppercase first letter
*/
public static String upperCaseFirstLetter(String string) {
if (string == null || string.length() <= 0) {
return string;
}
return string.substring(0, 1).toUpperCase(Locale.US) + string.substring(1);
}
/**
* Converts the first letter of the given string to lower case.
*
* @param string the input string
* @return the string with the lower case first letter
*/
public static String lowerCaseFirstLetter(String string) {
if (string == null || string.length() <= 0) {
return string;
}
return string.substring(0, 1).toLowerCase(Locale.US) + string.substring(1);
}
/**
* Returns the html for the given message.
*
* @param uriInfo the uriInfo context of the request
* @return String the html representation of the given message
*/
protected static String getHtml(String message, UriInfo uriInfo, boolean delete) {
String result = ProviderUtil.getHtmlHeader(uriInfo.getBaseUri().toASCIIString());
String uri = uriInfo.getAbsolutePath().toString();
if (delete) {
uri = uri + "/..";
}
String name = upperCaseFirstLetter(eleminateHypen(getName(uri, '/')));
result = result + "" + name + "
";
result = result + message;
result = result + "Back";
result = result + "