Archive | October, 2011

Calling pentaho report from Grails

14 Oct

You can take a look at this pentaho site

def reportPentahoServer = "http://localhost:9090/pentaho/content/reporting" // modify it based on your pentaho server location
def username = "user" // modify it based on your pentaho username
def password = "password" // modify it based on your pentaho password
def pentahoQuery = buildQuery(reportParam)

withHttp(uri: reportPentahoServer) {
   // this will ensure that the authentication happens before the server asks for credentials
	client.addRequestInterceptor(new org.apache.http.HttpRequestInterceptor() {
		void process(org.apache.http.HttpRequest httpRequest, org.apache.http.protocol.HttpContext httpContext) {
			String auth = username + ":" + password
			httpRequest.addHeader('Authorization', 'Basic ' + auth.bytes.encodeBase64().toString())
		}
	})

	post( query : pentahoQuery ) { resp, rd ->
		// have your own implementation here
	}
}

private Map buildQuery(ReportParam reportParam){
	Map query = new HashMap()
     // this 5 parameter is a must
	query.put("output-target", reportParam.getOutputType())
	query.put("renderMode", reportParam.getRenderMode())
	query.put("solution", reportParam.getSolution())
	query.put("path", reportParam.getPath())
	query.put("name", reportParam.getName())

    // this is additional parameter if needed in the report
	if(reportParam.filterCriteria.size() > 0){
		query.putAll(reportParam.filterCriteria)
	}

	return query
}

Note: – need to install plugins.rest=0.6
– ReportParam is my own class that i used to keep report parameter

Grails get controller by name

13 Oct

These several days i’ve been searching on how to map url request to the controller, and finally found it. First i code it in java, but i face some problems and move it into groovy. Here is the code

ApplicationContext ctx = (ApplicationContext) org.codehaus.groovy.grails.web.context.ServletContextHolder.getServletContext().getAttribute(org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes.APPLICATION_CONTEXT);
def grailsApp = ctx.getBean("grailsApplication")
def controllerName = ctx.getBean("grailsUrlMappingsHolder").match(request.getServletPath()).getParameters().controller
def controllerInstance = grailsApp.getArtefactByLogicalPropertyName("Controller", ctrlName)

Grails redirection when session expired

11 Oct

In my current application because i have 2 login page (1 for user and 1 for admin), then i need to handle this 2 login page based on user role. I’ve manage the logout redirection in this post, however at that time i haven’t figured out how to handle session expiration, because we only can specified 1 url in DefaultSecurityConfig (eq: auth.loginFormUrl = ‘/login/auth’). So what happen is, when session expired, user will be redirected to login auth page.

Thanks God, finally i found how to manage it.
– First we need to extends LoginUrlAuthenticationEntryPoint

package com.test
/* Copyright 2006-2010 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.groovy.grails.web.mapping.UrlMappingsHolder;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;

/**
 * @author <a href='mailto:burt@burtbeckwith.com'>Burt Beckwith</a>
 */
public class AjaxAwareAuthenticationEntryPoint extends LoginUrlAuthenticationEntryPoint {

	private UrlMappingsHolder proxyBean;

	@Override
	protected String determineUrlToUseForThisRequest(final HttpServletRequest request,
			final HttpServletResponse response, final AuthenticationException e) {

        String controllerName = (String)proxyBean.match(request.getServletPath()).getParameters().get("controller");
		if(controllerName.equals("/admin") {
			return "/admin/login";
		}
		else{
			return "/user/login";
		}
	}

	public void setGrailsUrlMappingsHolder(UrlMappingsHolder proxyBean) {
		this.proxyBean = proxyBean;
	}
}

– second, register it to resources.groovy

authenticationEntryPoint(netbank.AjaxAwareAuthenticationEntryPoint) {
		loginFormUrl = '/login/auth' // has to be specified even though it's ignored
		grailsUrlMappingsHolder = ref('grailsUrlMappingsHolder')
		portMapper = ref('portMapper')
		portResolver = ref('portResolver')
	 }