org.glassfish.admin.rest.resources.TemplateListOfResource 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 for IBM JDK
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2009-2016 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 [2016] [Payara Foundation]
package org.glassfish.admin.rest.resources;
import java.lang.reflect.Method;
import org.glassfish.config.support.Create;
import java.net.HttpURLConnection;
import com.sun.enterprise.util.LocalStringManagerImpl;
import org.glassfish.admin.rest.utils.ResourceUtil;
import org.glassfish.admin.rest.utils.Util;
import org.glassfish.admin.rest.provider.MethodMetaData;
import org.glassfish.admin.rest.results.ActionReportResult;
import org.glassfish.admin.rest.results.OptionsResult;
import org.glassfish.admin.rest.utils.xml.RestActionReporter;
import org.glassfish.api.ActionReport;
import org.glassfish.api.admin.RestRedirect;
import org.jvnet.hk2.config.ConfigBeanProxy;
import org.jvnet.hk2.config.ConfigModel;
import org.jvnet.hk2.config.Dom;
import org.jvnet.hk2.config.DomDocument;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.OPTIONS;
import javax.ws.rs.POST;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.glassfish.hk2.api.MultiException;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import static org.glassfish.admin.rest.utils.Util.decode;
import static org.glassfish.admin.rest.utils.Util.getName;
/**
* @author Ludovic Champenois [email protected]
* @author Rajeshwar Patil
*/
public abstract class TemplateListOfResource extends AbstractResource {
@Context
protected ServiceLocator injector;
protected List entity;
protected Dom parent;
protected String tagName;
public final static LocalStringManagerImpl localStrings = new LocalStringManagerImpl(TemplateListOfResource.class);
@GET
@Produces({MediaType.TEXT_HTML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response get(@QueryParam("expandLevel") @DefaultValue("1") int expandLevel) {
return Response.ok().entity(buildActionReportResult()).build();
}
@POST
//create
@Produces({MediaType.TEXT_HTML,
MediaType.APPLICATION_JSON,
MediaType.APPLICATION_XML})
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML,
MediaType.APPLICATION_FORM_URLENCODED})
public Response createResource(HashMap data) {
if (data == null) {
data = new HashMap();
}
try {
if (data.containsKey("error")) {
String errorMessage = localStrings.getLocalString("rest.request.parsing.error",
"Unable to parse the input entity. Please check the syntax.");
ActionReportResult arr = ResourceUtil.getActionReportResult(ActionReport.ExitCode.FAILURE, errorMessage, requestHeaders, uriInfo);
return Response.status(400).entity(arr).build();
}
ResourceUtil.purgeEmptyEntries(data);
//Command to execute
String commandName = getPostCommand();
String resourceToCreate = uriInfo.getAbsolutePath() + "/";
if (null != commandName) {
ResourceUtil.adjustParameters(data); //adjusting for DEFAULT is required only while executing a CLI command
if (data.containsKey("name")) {
resourceToCreate += data.get("name");
} else {
resourceToCreate += data.get("DEFAULT");
}
RestActionReporter actionReport = ResourceUtil.runCommand(commandName, data, getSubject());
ActionReport.ExitCode exitCode = actionReport.getActionExitCode();
if (exitCode != ActionReport.ExitCode.FAILURE) {
String successMessage =
localStrings.getLocalString("rest.resource.create.message",
"\"{0}\" created successfully.", resourceToCreate);
ActionReportResult arr = ResourceUtil.getActionReportResult(actionReport, successMessage, requestHeaders, uriInfo);
return Response.ok(arr).build();
}
String errorMessage = getErrorMessage(data, actionReport);
ActionReportResult arr = ResourceUtil.getActionReportResult(actionReport, errorMessage, requestHeaders, uriInfo);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(arr).build();
} else {
ActionReportResult arr = ResourceUtil.getActionReportResult(ActionReport.ExitCode.FAILURE, "No CRUD Create possible.", requestHeaders, uriInfo);
return Response.status(HttpURLConnection.HTTP_INTERNAL_ERROR).entity(arr).build();
}
} catch (Exception e) {
throw new WebApplicationException(e, Response.Status.INTERNAL_SERVER_ERROR);
}
}
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response post(FormDataMultiPart formData) {
/* data passed to the generic command running
*
* */
HashMap data = TemplateRestResource.createDataBasedOnForm(formData);
return createResource(data, data.get("name")); //execute the deploy command with a copy of the file locally
}
@OPTIONS
@Produces({MediaType.APPLICATION_JSON, MediaType.TEXT_HTML, MediaType.APPLICATION_XML})
public Response options() {
return Response.ok().entity(buildActionReportResult()).build();
}
public void setEntity(List p) {
entity = p;
}
public List getEntity() {
return entity;
}
public void setParentAndTagName(Dom parent, String tagName) {
this.parent = parent;
this.tagName = tagName;
if (parent != null) {
entity = parent.nodeElements(tagName);
}
}
/**
* allows for remote files to be put in a tmp area and we pass the
* local location of this file to the corresponding command instead of the content of the file
* * Yu need to add enctype="multipart/form-data" in the form
* for ex:
© 2015 - 2024 Weber Informatics LLC | Privacy Policy