Retrieving IMxRuntimeRequest and IMxRuntimeResponse from java action

1
Is it possible to retrieve the IMxRuntimeRequest and IMxRuntimeResponse from a Mx standard Java action? I know this is possible in a custom servlet but I can't find a way to achieve this from a standard Java action.
asked
3 answers
1

You can get the current requestID from your IContext, if that's what you're after.

answered
0

Related

 

        com.mendix.systemwideinterfaces.core.IContext ctx=getContext();
        java.util.Optional<com.mendix.m2ee.api.IMxRuntimeRequest> orrq=ctx.getRuntimeRequest();
        com.mendix.m2ee.api.IMxRuntimeRequest rrq=orrq.orElse(null);
        if(rrq!=null){
            java.lang.String str_hdrK="User-Agent";
            java.lang.String str_hdrV=rrq.getHeader(str_hdrK);
            return str_hdrV;
        }else{
            return null;
        }        com.mendix.systemwideinterfaces.core.IContext ctx=getContext();
        java.util.Optional<com.mendix.m2ee.api.IMxRuntimeRequest> orrq=ctx.getRuntimeRequest();
        com.mendix.m2ee.api.IMxRuntimeRequest rrq=orrq.orElse(null);
        if(rrq!=null){
            java.lang.String str_hdrK="User-Agent";
            java.lang.String str_hdrV=rrq.getHeader(str_hdrK);
            return str_hdrV;
        }else{
            return null;
        }

 

answered
0

Dear all, 

Adding my bit – I wanted to be able to return a custom reponse code and reason phrase in published web services – this isn't possible using the tools available in Mx Studio Pro, like it is with Published Rest Services. To get the same effect, I have created a Java Action that takes two input parameters (int/long code and string reasonPhrase); then the action basically grabs the response from the MxRuntimeRequest, and sets the proper values. Hope this helps future users.

Best regards,

Wouter

public class JA_SetWebserviceHttpReponseCodeAndReason extends CustomJavaAction<java.lang.Boolean>
{
	private java.lang.String ReasonPhrase;
	private java.lang.Long Code;

	public JA_SetWebserviceHttpReponseCodeAndReason(IContext context, java.lang.String ReasonPhrase, java.lang.Long Code)
	{
		super(context);
		this.ReasonPhrase = ReasonPhrase;
		this.Code = Code;
	}

	@java.lang.Override
	public java.lang.Boolean executeAction() throws Exception
	{
		// BEGIN USER CODE
		try {
		Integer i = Code != null ? Code.intValue() : null;
		
		if (i != null) {
			//attempt to change http response
			IContext ctx = getContext();
			IMxRuntimeResponse response =  ctx.getRuntimeResponse().get();
			HttpServletResponse httpResponse = (HttpServletResponse) response.getHttpServletResponse();
			response.setStatus(i, ReasonPhrase);
			return true;
		}
		return false;
		
	} catch (Exception e) {
		throw new com.mendix.systemwideinterfaces.MendixRuntimeException("There was an error while trying to set the values");
	}
	
	
		
		
		
		
		// END USER CODE
	}

 

answered