use of com.sun.enterprise.admin.remote.ParamsWithPayload in project Payara by payara.
the class CommandResource method executeCommand.
private Response executeCommand(CommandName commandName, Payload.Inbound inbound, ParameterMap params, boolean supportsMultiparResult, String xIndentHeader, String modelETag, Cookie jSessionId) throws WebApplicationException {
// Scope support
if (RestLogging.restLogger.isLoggable(Level.FINEST)) {
RestLogging.restLogger.log(Level.FINEST, "executeCommand(): ", commandName);
}
// Check command model
CommandModel model = getCommandModel(commandName);
checkCommandModelETag(model, modelETag);
// Execute it
boolean notifyOption = false;
if (params != null) {
notifyOption = params.containsKey("notify");
}
// new RestActionReporter(); //Must use PropsFileActionReporter because some commands react diferently on it :-(
ActionReporter ar = new PropsFileActionReporter();
final RestPayloadImpl.Outbound outbound = new RestPayloadImpl.Outbound(false);
final CommandRunner.CommandInvocation commandInvocation = getCommandRunner().getCommandInvocation(commandName.getScope(), commandName.getName(), ar, getSubject(), notifyOption);
if (inbound != null) {
commandInvocation.inbound(inbound);
}
commandInvocation.outbound(outbound).parameters(params).execute();
ar = (ActionReporter) commandInvocation.report();
fixActionReporterSpecialCases(ar);
ActionReport.ExitCode exitCode = ar.getActionExitCode();
int status = HttpURLConnection.HTTP_OK;
/*200 - ok*/
if (exitCode == ActionReport.ExitCode.FAILURE) {
status = HttpURLConnection.HTTP_INTERNAL_ERROR;
}
ResponseBuilder rb = Response.status(status);
if (xIndentHeader != null) {
rb.header("X-Indent", xIndentHeader);
}
if (supportsMultiparResult && outbound.size() > 0) {
ParamsWithPayload pwp = new ParamsWithPayload(outbound, ar);
rb.entity(pwp);
} else {
rb.type(MediaType.APPLICATION_JSON_TYPE);
rb.entity(ar);
}
if (isSingleInstanceCommand(model)) {
rb.cookie(getJSessionCookie(jSessionId));
}
return rb.build();
}
use of com.sun.enterprise.admin.remote.ParamsWithPayload in project Payara by payara.
the class MultipartProprietaryReader method readFrom.
@Override
public ParamsWithPayload readFrom(final InputStream is, final String contentType) throws IOException {
RestPayloadImpl.Inbound payload = null;
ActionReport actionReport = null;
ParameterMap parameters = null;
Properties mtProps = parseHeaderParams(contentType);
final String boundary = mtProps.getProperty("boundary");
if (!StringUtils.ok(boundary)) {
throw new IOException("ContentType does not define boundary");
}
final MIMEMessage mimeMessage = new MIMEMessage(is, boundary, new MIMEConfig());
// Parse
for (MIMEPart mimePart : mimeMessage.getAttachments()) {
String cd = getFirst(mimePart.getHeader("Content-Disposition"));
if (!StringUtils.ok(cd)) {
cd = "file";
}
cd = cd.trim();
Properties cdParams = parseHeaderParams(cd);
// 3 types of content disposition
if (cd.startsWith("form-data")) {
// COMMAND PARAMETER
if (!StringUtils.ok(cdParams.getProperty("name"))) {
throw new IOException("Form-data Content-Disposition does not contains name parameter.");
}
if (parameters == null) {
parameters = new ParameterMap();
}
parameters.add(cdParams.getProperty("name"), stream2String(mimePart.readOnce()));
} else if (mimePart.getContentType() != null && mimePart.getContentType().startsWith("application/json")) {
// ACTION REPORT
actionReport = actionReportReader.readFrom(mimePart.readOnce(), "application/json");
} else {
// PAYLOAD
String name = "noname";
if (cdParams.containsKey("name")) {
name = cdParams.getProperty("name");
} else if (cdParams.containsKey("filename")) {
name = cdParams.getProperty("filename");
}
if (payload == null) {
payload = new RestPayloadImpl.Inbound();
}
String ct = mimePart.getContentType();
if (!StringUtils.ok(ct) || ct.trim().startsWith("text/plain")) {
payload.add(name, stream2String(mimePart.readOnce()), mimePart.getAllHeaders());
} else {
payload.add(name, mimePart.read(), ct, mimePart.getAllHeaders());
}
}
}
// Result
return new ParamsWithPayload(payload, parameters, actionReport);
}
use of com.sun.enterprise.admin.remote.ParamsWithPayload in project Payara by payara.
the class MultipartProprietaryWriter method writeTo.
@Override
public void writeTo(final Object entity, final HttpURLConnection urlConnection) throws IOException {
Payload.Outbound payload = null;
ParameterMap parameters = null;
ActionReport ar = null;
if (entity instanceof ParamsWithPayload) {
ParamsWithPayload pwp = (ParamsWithPayload) entity;
payload = pwp.getPayloadOutbound();
parameters = pwp.getParameters();
ar = pwp.getActionReport();
} else if (entity instanceof Payload.Outbound) {
payload = (Payload.Outbound) entity;
}
writeTo(payload, parameters, ar, new OutputStream() {
private OutputStream delegate;
private OutputStream getDelegate() throws IOException {
if (delegate == null) {
delegate = urlConnection.getOutputStream();
}
return delegate;
}
@Override
public void write(int b) throws IOException {
getDelegate().write(b);
}
@Override
public void write(byte[] b) throws IOException {
getDelegate().write(b);
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
getDelegate().write(b, off, len);
}
@Override
public void flush() throws IOException {
getDelegate().flush();
}
@Override
public void close() throws IOException {
getDelegate().close();
}
}, new ContentTypeWriter() {
@Override
public void writeContentType(String firstPart, String secondPart, String boundary) {
StringBuilder ct = new StringBuilder();
ct.append(firstPart).append('/').append(secondPart);
if (boundary != null) {
ct.append("; boundary=").append(boundary);
}
urlConnection.addRequestProperty("Content-type", ct.toString());
urlConnection.setRequestProperty("MIME-Version", "1.0");
}
});
}
use of com.sun.enterprise.admin.remote.ParamsWithPayload in project Payara by payara.
the class ParameterMapFormProprietaryWriter method writeTo.
@Override
public void writeTo(Object entity, HttpURLConnection urlConnection) throws IOException {
ParameterMap pm;
if (entity instanceof ParameterMap) {
pm = (ParameterMap) entity;
} else if (entity instanceof ParamsWithPayload) {
pm = ((ParamsWithPayload) entity).getParameters();
} else {
pm = new ParameterMap();
}
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
final StringBuilder sb = new StringBuilder();
for (Map.Entry<String, List<String>> entry : pm.entrySet()) {
for (String value : entry.getValue()) {
if (sb.length() > 0) {
sb.append('&');
}
sb.append(URLEncoder.encode(entry.getKey(), UTF8));
if (value != null) {
sb.append('=');
sb.append(URLEncoder.encode(value, UTF8));
}
}
}
urlConnection.getOutputStream().write(sb.toString().getBytes(UTF8));
}
Aggregations