I’ve done something similar to add images into emails without having to embed them as base64.
You can use a request handler to build the same function as “url/file?guid” but without the need for a user to be authenticated.
Here is my java action code:
instead of “url/file?guid” im using “url/image?guid”.
The IMXRuntimeResponse has an output stream that you can use to return the image. I used apache commons to easily copy an input stream to an output stream but that can be replaced if you dont want a dependency.
import com.mendix.core.Core;
import com.mendix.externalinterface.connector.RequestHandler;
import com.mendix.m2ee.api.IMxRuntimeRequest;
import com.mendix.m2ee.api.IMxRuntimeResponse;
import com.mendix.systemwideinterfaces.core.IContext;
import com.mendix.systemwideinterfaces.core.IMendixIdentifier;
import com.mendix.systemwideinterfaces.core.IMendixObject;
import com.mendix.webui.CustomJavaAction;
import org.apache.commons.io.IOUtils;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import static com.mendix.core.Core.retrieveId;
import static com.mendix.core.Core.retrieveXPathQuery;
public class HostImages extends CustomJavaAction<java.lang.Boolean>
{
public HostImages(IContext context)
{
super(context);
}
@java.lang.Override
public java.lang.Boolean executeAction() throws Exception
{
// BEGIN USER CODE
//throw new com.mendix.systemwideinterfaces.MendixRuntimeException("Java action was not implemented");
try {
Core.addRequestHandler("image", new handler());
Core.getLogger("RequestHandlers").info("Image hosting initialized");
return true;
} catch(Exception e) {
Core.getLogger("RequestHandlers").error("Image Hosting failed to initialize");
return false;
}
// END USER CODE
}
/**
* Returns a string representation of this action
*/
@java.lang.Override
public java.lang.String toString()
{
return "HostImages";
}
// BEGIN EXTRA CODE
class handler extends RequestHandler {
@Override
public void processRequest(IMxRuntimeRequest request,
IMxRuntimeResponse response, String arg2) throws Exception
{
String guid = request.getParameter("guid");
try {
Core.getLogger("RequestHandlers").debug("guid found: " + guid);
java.util.List<IMendixObject> result = retrieveXPathQuery(getContext(), "//MiCorr.Image[id='" + guid + "']");
IMendixObject obj = result.get(0);
OutputStream os = response.getOutputStream();
InputStream is = Core.getFileDocumentContent(getContext(), obj);
IOUtils.copy(is, os);
IOUtils.closeQuietly(os);
response.setStatus(IMxRuntimeResponse.OK);
response.setContentType("image/png");
} catch (Exception e) {
response.setStatus(IMxRuntimeResponse.OK);
}
}
}
// END EXTRA CODE
}