Skip to content

Commit

Permalink
#188 improve content type handling of action results
Browse files Browse the repository at this point in the history
  • Loading branch information
p-a-s-c-a-l committed May 29, 2017
1 parent 8405c7e commit d196a5e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
/**
* DOCUMENT ME!
*
* @author jruiz
* @author jruiz, Pascal Dihé
* @version $Revision$, $Date$
*/
@org.openide.util.lookup.ServiceProvider(service = RestApiCidsServerAction.class)
Expand Down Expand Up @@ -104,7 +104,8 @@ public String getTaskName() {
}

@Override
public GenericResourceWithContentType<byte[]> execute(final Object body,
public GenericResourceWithContentType<byte[]> execute(
final Object body,
final ServerActionParameter... params) {
if (LOG.isDebugEnabled()) {
LOG.debug("executing '" + this.getTaskName() + "' with "
Expand All @@ -131,9 +132,26 @@ public GenericResourceWithContentType<byte[]> execute(final Object body,
}
}

if (path == null) {
final String message =
"Could not download file, client did not provide a valid path (parameter FILEPATH)";
LOG.error(message);
throw new RuntimeException(message);
} else if (path.toFile().canRead()) {
final String message = "Cannot read file at specified path '" + path + "'";
LOG.error(message);
throw new RuntimeException(message);
}

String contentType = Files.probeContentType(path);
if (contentType == null) {
LOG.warn("Content type of file at specified path '" + path + "' could not be determined!");
}
contentType = (contentType != null) ? contentType : MediaType.APPLICATION_OCTET_STREAM;

// TODO: check requested content type in ActionInfo
LOG.info("reading file at specified path '" + path + "' with content type '" + contentType + "'");

// FIXME: not very efficient for large files -> out of memory!
// return output stream instead?!
final byte[] fileContent = Files.readAllBytes(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import de.cismet.cidsx.server.api.types.ActionTask;
import de.cismet.cidsx.server.api.types.CidsClass;
import de.cismet.cidsx.server.api.types.CidsNode;
import de.cismet.cidsx.server.api.types.ParameterInfo;

import static de.cismet.cidsx.server.api.types.legacy.ServerSearchFactory.toBase64String;

Expand Down Expand Up @@ -291,13 +290,14 @@ public Class<? extends ServerAction> getServerActionClass(final String actionKey
}

/**
* Populates an instance of a ServerAction with parameters from the actionParameters object.
* Populates an instance of a ServerAction with parameters from the actionParameters object. Note: Parameters have
* been processed by Jackson ObjectMapper!
*
* @param actionTask DOCUMENT ME!
*
* @return ServerAction with parameters
*/
public ServerActionParameter[] ServerActionParametersFromActionTask(
public ServerActionParameter[] serverActionParametersFromActionTask(
final ActionTask actionTask) {
if (LOG.isDebugEnabled()) {
LOG.debug("getting parameters from cids server action '" + actionTask.getActionKey() + "'");
Expand Down Expand Up @@ -348,7 +348,8 @@ public ServerActionParameter[] ServerActionParametersFromActionTask(
*
* @throws Exception DOCUMENT ME!
*/
public Object bodyObjectFromFileAttachment(final InputStream fileAttachement,
public Object bodyObjectFromFileAttachment(
final InputStream fileAttachement,
final ActionParameterInfo bodyDescription) throws Exception {
final Object body;

Expand Down Expand Up @@ -430,6 +431,7 @@ public ActionParameterInfo getDefaultReturnDescription() {
* {@link CidsBeanFactory#cidsBeanFromLightweightMetaObject(Sirius.server.middleware.types.LightweightMetaObject, Sirius.server.middleware.types.MetaClass) )}
*
* @param actionResult DOCUMENT ME!
* @param contentType DOCUMENT ME!
* @param resultType resultParameterInfo DOCUMENT ME!
* @param classNameCache DOCUMENT ME!
*
Expand All @@ -439,13 +441,16 @@ public ActionParameterInfo getDefaultReturnDescription() {
*/
public Object transformLegacyActionResult(
final Object actionResult,
final String contentType,
final Type resultType,
final ClassNameCache classNameCache) throws Exception {
final boolean isMetaClass = resultType == Type.ENTITY_INFO;
final boolean isMetaObject = resultType == Type.ENTITY;
final boolean isLightWightMetaObject = resultType == Type.ENTITY_REFERENCE;
final boolean isMetaNode = resultType == Type.NODE;
final boolean isBinaryObject = resultType == Type.JAVA_SERIALIZABLE;
final boolean isJsonObject = contentType.toLowerCase().equalsIgnoreCase(MediaType.APPLICATION_JSON);
final boolean isPlainTextObject = contentType.toLowerCase().contains("text");

// FIXME: Merge with ServerSearchFactory.jsonNodesFromResultCollection()
if (isMetaClass) {
Expand Down Expand Up @@ -487,6 +492,10 @@ public Object transformLegacyActionResult(
} else if (isBinaryObject) {
final String stringRepresentation = toBase64String(actionResult);
return stringRepresentation;
} else if (isPlainTextObject) {
return String.valueOf(actionResult);
} else if (isJsonObject) {
return MAPPER.writeValueAsString(actionResult);
} else {
return actionResult;
}
Expand Down

0 comments on commit d196a5e

Please sign in to comment.