Struts 2 JUnit Plugin v2.2.3: Test Class Extending StrutsTestCase; 'request' is null - plugins

I am attempting to use the Struts2 JUnit Plugin (v2.2.3 w/ Struts2 v2.2.3) and have run into several issues.
I attempted to use the Struts2 JUnit Plugin Tutorial as a guide. The first change I needed to make (not in the guide) is to annotate my test class with:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations={"classpath*:applicationContext-test.xml"})
because I was getting the error when trying to run my unit test:
SEVERE: [56:51.239] ********** FATAL ERROR STARTING UP STRUTS-SPRING INTEGRATION **********
Looks like the Spring listener was not configured for your web app!
Nothing will work until WebApplicationContextUtils returns a valid ApplicationContext.
You might need to add the following to web.xml:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
SEVERE: [56:51.254] Dispatcher initialization failed
This is different than the tutorial- does anyone know why I needed to do this?
I also had to add the following Spring Jars (I have the necessary Struts2 jars included in my classpath):
spring-beans-2.5.6.jar
spring-context-2.5.6.jar
spring-core-2.5.6.jar
spring-test-2.5.6.jar
spring-web-2.5.6.jar
I do not use Spring w/in my struts app, but I assume these jars are needed to use the mock request object and such in the StrutsTestCase.
My Test class:
package com.actions;
import java.io.UnsupportedEncodingException;
import java.util.List;
import javax.servlet.ServletException;
import org.apache.log4j.Logger;
import org.apache.struts2.StrutsTestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.beans.LabelValueBean;
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations={"classpath*:applicationContext-test.xml"})
public class DocumentCategoryTest extends StrutsTestCase {
private static Logger log = Logger.getLogger(DocumentCategoryTest .class);
/**
* Testing RetrieveDocumentTypesForDocumentCategory
* Expecting the List<LabelValueBean> is not null
* #throws ServletException
* #throws UnsupportedEncodingException
*/
#Test
public void testRetrieveDocumentTypesForDocumentCategory() throws UnsupportedEncodingException, ServletException {
final String docCategory = "Employment";
//set parameters
request.setParameter("documentCategoryDescription", docCategory);
//execute the action
executeAction("/recipient/RetrieveDocumentTypesForDocumentCategory.action");
//retrieve the document types
#SuppressWarnings("unchecked")
List<LabelValueBean> testDocTypeList = (List<LabelValueBean>) findValueAfterExecute("documentTypes");
//make sure the document type list is not null and has at least one document type
assertNotNull(testDocTypeList);
assertTrue("At least one document type should exist for category 'Employment'", testDocTypeList.size() > 0);
//print types
log.debug("Document types for category '" + docCategory + "'");
log.debug(testDocTypeList);
}
}
When I execute the action, I get a NullPointerException on the request.setParameter piece of code:
java.lang.NullPointerException
at com.actions.DocumentCategoryTest.testRetrieveDocumentTypesForDocumentCategory(DocumentCategoryTest.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.test.context.junit4.SpringTestMethod.invoke(SpringTestMethod.java:160)
at org.springframework.test.context.junit4.SpringMethodRoadie.runTestMethod(SpringMethodRoadie.java:233)
at org.springframework.test.context.junit4.SpringMethodRoadie$RunBeforesThenTestThenAfters.run(SpringMethodRoadie.java:333)
at org.springframework.test.context.junit4.SpringMethodRoadie.runWithRepetitions(SpringMethodRoadie.java:217)
at org.springframework.test.context.junit4.SpringMethodRoadie.runTest(SpringMethodRoadie.java:197)
at org.springframework.test.context.junit4.SpringMethodRoadie.run(SpringMethodRoadie.java:143)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:160)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:97)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
This is because, for some reason, request is null in StrutsTestCase. Why is this? I'm doing exactly as the tutorial dictates!
My action class extends another class (BaseAction) that extends ActionSupport and implements SessionAware.
package com.actions;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import com.actions.BaseAction;
/**
* Retrieves the document types for the passed in document category
*/
public class DocumentIndexingData extends BaseAction {
private static final long serialVersionUID = 1L;
private static Logger log = Logger.getLogger(DocumentIndexingData.class);
/*=========================================================================
* FIELDS
*=========================================================================*/
/**
* The description to use to look up the document types for that category
*/
private String documentCategoryDescription;
/**
* List of document types for a category
*/
private List<LabelValueBean> documentTypes;
/*=========================================================================
* PUBLIC METHODS
*=========================================================================*/
/**
* Retrieves & sets list of document types for document category with passed in ID
*
* #return If there is an error, document type will be set to ERROR; SUCCESS is always returned by this method
*/
public String retrieveDocumentTypesForDocumentCategory() {
List<LabelValueBean> docTypes = new ArrayList<LabelValueBean>();
//retrieve document types for passed in category ID
log.debug("looking up document types for category: " + getDocumentCategoryDescription());
////////////////
//retrieve the document types for the document category here...
////////////////
this.setDocumentTypes(docTypes);
log.debug("document types found: " + getDocumentTypes());
return SUCCESS;
}
/*=========================================================================
* GETTERS/SETTERS
*=========================================================================*/
/**
* Retrieves the list of document types
*
* #return list of document types
*/
public List<LabelValueBean> getDocumentTypes() {
return documentTypes;
}
/**
* Sets the list of document types
*
* #param documentTypes
*/
public void setDocumentTypes(List<LabelValueBean> documentTypes) {
this.documentTypes = documentTypes;
}
/**
* Retrieves the document category to retrieve document types for
*
* #return the document category
*/
public String getDocumentCategoryDescription() {
return documentCategoryDescription;
}
/**
* Sets the document category to retrieve document types for
*
* #param documentCategoryDescription the document category to retrieve document types for
*/
public void setDocumentCategoryDescription(String documentCategoryDescription) {
this.documentCategoryDescription = documentCategoryDescription;
}
}
struts.xml entry:
<!-- indexing attributes action -->
<action name="RetrieveDocumentTypesForDocumentCategory" class="com.actions.DocumentIndexingData" method="retrieveDocumentTypesForDocumentCategory">
<result type="json">
<param name="root">documentTypes</param>
</result>
</action>
I'm following the tutorial pretty closely, why is request null? Thank you so much for any help!
UPDATE (My Solution! via #Alex):
I now only have the following Struts JARs included on my classpath:
struts2-junit-plugin-2.2.3.jar
spring-beans-2.5.6.jar
spring-context-2.5.6.jar
spring-core-2.5.6.jar
spring-test-2.5.6.jar
spring-web-2.5.6.jar
Sample test:
public class MyTest extends StrutsTestCase {
#Test
public void testMyAction() throws Exception {
request.setParameter("aParameter", "aValue");
//create action proxy
ActionProxy proxy = getActionProxy("/test/MyAction");
assertNotNull(proxy);
MyAction action = (MyAction) proxy.getAction();
assertNotNull(action);
//execute the action
String result = proxy.execute();
//make assertions, expecting success and no error messags
assertTrue("There should be no field errors: " + action.getFieldErrors(), action.getFieldErrors().size() == 0);
assertTrue("There should be no action errors: " + action.getActionErrors(), action.getActionErrors().size() == 0);
assertEquals("Result did not match expected value; ", Action.SUCCESS, result);
}
}

I do not use Spring w/in my struts app, but I assume these jars are needed to use the mock request object and such in the StrutsTestCase.
Given the exception messages you're seeing above your application believes that it uses Spring. Do you have the Struts2-Spring-Plugin on your classpath? If you're not using Spring for IoC then you shouldn't include it on your classpath.
You should be able to test your actions by following guide you've referenced without any spring jars if you don't use Spring. Try removing all the spring jars and the struts2-spring-plugin jar. You also certainly don't need:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations={"classpath*:applicationContext-test.xml"})
in your test class if you don't use spring.
Also check that your struts.xml doesn't contain this line:
<constant name="struts.objectFactory"
value="org.apache.struts2.spring.StrutsSpringObjectFactory"/>

Related

unit test failing if Log Member object (#Slf4j isn't found groovy.lang.MissingPropertyException: No such property: log for class:

I'm helping my development team with some logging code in our framework.
using spring AOP I've created a groovy class called LoggingAspect. Its main purpose is to log method execution times for classes in com.zions.comon.services.logging directories and
annotated with #Loggable.
Since some classes already have #sl4j logging I need to detect if hat log member objects exists and use the built in #slf4j logging for that class. If it doesn't I need to execute the #sl4j annotation in aspect logging code.
The first statement in the try block will check if log member exists on object. If it does, then iLog will get set to incoming object's logger. However I'm not sure how to complete the rest of the code Once I detect the log member object. I don't expect anyone to write this code for me but would appreciate any suggestions/areas of researcoh on how to do this - such as using "if"
The logic should go something like:
Intercept and calculate method logging times in select classes
Check for existing log member object that indicates #slf4j is already present in class
If log member object exits use #sl4j logging features already built into that class
If log member object doesnt exist use #slf4j logging in logging Aspect code.
any help would be appreciated
"logging flow diagram"
Reverted code to original version - My LoggingAspect code looks like this at the moment
package com.zions.common.services.logging
import groovy.util.logging.Slf4j
import org.slf4j.Logger
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.EnableAspectJAutoProxy
#Aspect
#Configuration
#Slf4j
#EnableAspectJAutoProxy(proxyTargetClass=true)
public class LoggingAspect {
*
* This is a Logging Aspect for the Loggable annotation that calculates method runtimes
* for all methods under classes annotated with #Loggable*/
/**
* Logs execution time of method under aspect.
*
* #param joinPoint - method under join
* #return actual return of method under join point.
* #throws Throwable
*/
#Around('execution (* *(..)) && !execution(* *.getMetaClass()) && #within(com.zions.common.services.logging.Loggable)')
public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
def obj = joinPoint.this
Logger iLog = log
long start = System.currentTimeMillis();
Object proceed = joinPoint.proceed();
long executionTime = System.currentTimeMillis() - start;
try {
/*First statement of try block attempts to test if log members exist on object.
If it does, then iLog will get set to incoming object's logger*/
obj.log.isInfoEnabled()
iLog = obj.log
} catch (e) {}
iLog.info("${joinPoint.getSignature()} executed in ${executionTime}ms");
return proceed;
}
}
If its helpful my logging Annotation is
package com.zions.common.services.logging
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/* Logging annotation to be used at class level
* Loggable annotation for all methods of a class annotated with the #Loggable annotation*/
#Target(ElementType.TYPE)
#Retention(RetentionPolicy.RUNTIME)
public #interface Loggable {}
I've added a junit test class that validates when log member is found - The line 'iLog = obj.log' get's called from the LoggingAspect code and the test is PASSING.
LoggingAspectSpecification.groovy
package com.zions.common.services.logging
import static org.junit.Assert.*
import groovy.util.logging.Slf4j
import org.junit.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.TestConfiguration
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Profile
import org.springframework.context.annotation.PropertySource
import org.springframework.test.context.ContextConfiguration
import spock.lang.Specification
#ContextConfiguration(classes=[LoggingAspectSpecificationConfig])
class LoggingAspectSpecification extends Specification {
#Autowired
SomeClass someClass
def 'Main flow for timing log'() {
setup: 'class to be logged'
when: 'execute something with class testing log'
someClass.methodOne()
someClass.methodTwo()
then: 'validate something logged'
true
}
}
#TestConfiguration
#Profile("test")
#ComponentScan(["com.zions.common.services.logging"])
#PropertySource("classpath:test.properties")
class LoggingAspectSpecificationConfig {
#Bean
SomeClass someClass() {
return new SomeClass()
}
}
#Loggable
#Slf4j
class SomeClass {
def methodOne() {
log.info('run methodOne')
}
def methodTwo() {
log.info('run methodTwo')
}
}
However my unit test is failing with classes that do not have #Slf4j meaning it will execute with the logger of the aspect instead of the pointcut object. The full error trace is:
groovy.lang.MissingPropertyException: No such property: log for class: com.zions.common.services.logging.SomeClass2
at com.zions.common.services.logging.SomeClass2.methodOne(LoggingAspectSpecification2.groovy:55)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:747)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:89)
at com.zions.common.services.logging.LoggingAspect.logExecutionTime(LoggingAspect.groovy:42)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:643)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:632)
at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:174)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689)
at com.zions.common.services.logging.LoggingAspectSpecification2.Main flow for timing log(LoggingAspectSpecification2.groovy:27)
The second unit test code is below - (the only difference is that #Slf4j) is not present in the classes.
LoggingAspectSpecification2.groovy
package com.zions.common.services.logging
import static org.junit.Assert.*
import groovy.util.logging.Log
import groovy.util.logging.Slf4j
import org.junit.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.TestConfiguration
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Profile
import org.springframework.context.annotation.PropertySource
import org.springframework.test.context.ContextConfiguration
import spock.lang.Specification
#ContextConfiguration(classes=[LoggingAspectSpecificationConfig2])
class LoggingAspectSpecification2 extends Specification {
#Autowired
SomeClass2 someClass2
def 'Main flow for timing log'() {
setup: 'class to be logged'
when: 'execute something with class testing log'
someClass2.methodOne()
someClass2.methodTwo()
then: 'validate something logged'
true
}
}
<!-- language: lang-groovy -->
#TestConfiguration
#Profile("test")
#ComponentScan(["com.zions.common.services.logging"])
#PropertySource("classpath:test.properties")
class LoggingAspectSpecificationConfig2 {
#Bean
SomeClass2 someClass2() {
return new SomeClass2()
}
}
<!-- language: lang-groovy -->
#Loggable
class SomeClass2 {
def methodOne() {
int x=10, y=20;
System.out.println(x+y+" testing the aspect logging code");
}
def methodTwo() {
int x=10, y=20;
System.out.println(x+y+" testing the aspect logging code");
}
}
I'm guessing something's wrong in my LoggingAspect code in the Try Catch block?
To resolve the error and get my unit test to pass without #Slf4j or #Log - I had to add a println statement to the SomeClass2 code as in,
int x=10, y=20;
System.out.println(x+y+" testing the apsect logging code");
adding #Log just gave it another built in log member similar to #Slf4j - adding the println statement and removing the #Log annotation force the LoggingAspect code to execute. Unit test is passing.

MyBatis Error: Invalid bound statement (not found)

Here is the stack trace when I try to execute a simple query using MyBatis:
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.ppcredit.gypsophila.mapper.GypsophilaVarsStatisticsMapper.selectBySql
at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:225)
at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:48)
at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:65)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:58)
at com.sun.proxy.$Proxy110.selectBySql(Unknown Source)
Here is a method from my class:
/**
* use the sql to query result
*
* #param sql
* #return
*/
List<LinkedHashMap<String, Object>> selectBySql(String sql, Map<String, Object> parms);
In this case, need want to execute SQL statement directly, the sql as below:
SELECT VELOCITY_VARS FROM PPC_GYPSOPHILA_VARS_STATISTICS WHERE SCENARIO_ID=#{scenarioId} and TACTIC_ID=#{flowId} and CREATE_USER=#{userId}
all parameters of sql can be found in Map<String, Object> parms
I've done some research, but none of the solutions have worked for me. My Mapper class seems have some problems, but i dont know what's wrong.
Does anyone have an idea what's wrong here?
Thanks in advance
I am going to suppose that public interface GypsophilaVarsStatisticsMapper implements selectBySQL, in a correct way. I assumed you are import annotations if you are using it by:
import com.ppcredit.gypsophila.mapper.entity.GypsophilaVarsStatisti‌​cs;
import org.apache.ibatis.annotations.*;
public interface GypsophilaVarsStatisticsMapper {
List<GypsophilaVarsStatistics> selectBySQL(String sql,Map parameters);
/**your code**/
I am going to suppose likewise, that you have a class (let's call it test) whose scope allows to call GypsophilaVarsStatisticsMapper Interface (in other case import). That would i do is the folloging:
import com.ppcredit.gypsophila.mapper.entity.GypsophilaVarsStatisti‌​cs;
public class Test {
public static void main(String args[]) throws IOException{
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sqlSessionFactory.openSession();
session.getConfiguration().addMapper(GypsophilaVarsStatisticsMapper.class);
GypsophilaVarsStatisticsMapper mapper = session.getMapper(GypsophilaVarsStatisticsMapper.class);
sql_string = 'SELECT VELOCITY_VARS FROM PPC_GYPSOPHILA_VARS_STATISTICS WHERE SCENARIO_ID=#{scenarioId} and TACTIC_ID=#{flowId} and CREATE_USER=#{userId}'
Map<String,Object) params = getParams() /* Your params */
List<GypsophilaVarsStatistics> result = mapper.selectBySQL(sql_string, params)
I suppose than you not are using MyBatis with a dependency injection framework like Spring. In this case SqlSessionFactory is supplied by this framework.

Why is Querydsl looking for incorrect generated entities?

I am having an issue where I will get a ClassNotFoundException error when I try to run Junit tests. The query classes that are generated are QSomeTableEntity_Q but it keeps looking for QSomeTableEntity in the SomeTableRepository for the entity even though my Predicate class imports the QSomeTableEntity_Q class.
I have in my maven pom
< querydsl.suffix >_Q< /querydsl.suffix >
It seems like spring jpa framework will look for the q-entity in the domain class located package.Here is the code:
/**
* Returns the name of the query class for the given domain class.
*
* #param domainClass
* #return
*/
private String getQueryClassName(Class<?> domainClass) {
String simpleClassName = ClassUtils.getShortName(domainClass);
return String.format("%s.Q%s%s", domainClass.getPackage().getName(), getClassBase(simpleClassName),
domainClass.getSimpleName());
}
So just move the q-entity will solve the problem.

How to add structural links to Jersey/Moxy/JAXB XML without altering the model

I mean "structural links" in the HATEOAS/hypermedia API sense. The more general question is how to augment the generated XML with data that depends on both the entity being marshalled, and also on the environment (in this case, at least the absolute URL).
I'm using Jersey 2.9 with Moxy 2.5 as the JAXB provider.
From this model:
package testing;
import java.util.ArrayList;
import java.util.List;
public class Planet {
private int id = 1;
private String name = "test";
private double radius = 3.0;
private String href;
private List<Moon> moons = new ArrayList<Moon>(0);
public void addMoon(Moon moon) {
moons.add(moon);
}
}
...plus Moon class
I want to get something like this XML (and the equivalent JSON):
<planet href="http://mytestserver/rest/planets/test">
<name>test</name>
<radius>3.0</radius>
<moons>
<moon href="http://mytestserver/rest/moons/moon1">
<name>moon1</name>
</moon>
<moon href="http://mytestserver/rest/moons/moon2">
<name>moon2</name>
</moon>
</moons>
</planet>
The model has no "href" field, nor can one be added. Ideally I could use UriBuilder to grab these paths straight from the resource classes.
So far I've come up with several possiblities. Can I ask you to consider which (if any) has the most legs, and then how you would work around the shortcomings of that method?
1. Augment the model with AspectJ (or Javassist).
And then use the existing declarative linking mechanisms in Jersey, all of which rely on there being a field in the model to receive the generated links. This obviously won't work if you don't have AspectJ in your build process and/or balk at exotic techniques like byte code manipulation.
2. Post-process the generated XML and JSON
For example, in a MessageBodyWriter:
ContextResolver<JAXBContext> resolver = providers.getContextResolver(JAXBContext.class, mediaType);
JAXBContext context = resolver.getContext(type);
Marshaller m = context.createMarshaller();
<--- here, marshall to e.g. a DOM then transform that
<--- then manipulate the JSON structures
I have absolutely no idea how to do any of that, hence the lack of code. There may be other ways to hook into the XML generation process, but as far as I can see none of Jersey's or JAXB's event handlers or interceptors actually allow you to manipulate the generated XML/JSON.
3. Use a Moxy XMLTransformationMapping
For example:
XML binding:
<java-type name="Planet" xml-customizer="testing.HrefCustomizer">
Customizer:
public class HrefCustomizer implements DescriptorCustomizer {
#Override
public void customize(ClassDescriptor descriptor) throws Exception {
XMLTransformationMapping xtm = new XMLTransformationMapping();
xtm.addFieldTransformer("#href", new HrefWriter());
descriptor.addMapping(xtm);
}
}
Transformer:
public class HrefWriter implements FieldTransformer {
#Override
public Object buildFieldValue(Object instance, String fieldName,
Session session) {
return "href"; // constant value just for proof-of-concept
}
#Override
public void initialize(AbstractTransformationMapping mapping) {
// TODO Auto-generated method stub
}
}
I have two problems with this approach:
It was so hard to find any documentation on it that I wonder if it is in fact unsupported usage.
I can't see how the transformer is going to get a UriBuilder to work with. At minimum it would need the root URL of the rest service.
4. Slightly different Moxy xml-transform approach
If we decide we can't provide the transformer with any meaningful context at instantiation time, the customizer is adding no value and we can simplify the above to just this:
<java-type name="Planet">
<xml-root-element/>
<java-attributes>
<xml-transformation java-attribute="name">
<xml-write-transformer transformer-class="testing.HrefWriter" xml-path="#href"/>
</xml-transformation>
<xml-element java-attribute="name"/>
With the slight oddity that we are hanging the transformer off another field ("name", in this example).
5. ?????
Or, I'm completely barking up the wrong tree. Help!!
AspectJ approach
Synopsis
Use AspectJ to add a field to the model classes (called "href" in this example)
Add the Jersey #InjectLink annotation to that field
Jersey will then populate the field with the right URL as defined by the resource class
Specify the marshaling of the href field using an external mapping file.
You could also specify the marshaling of href by adding JAXB annotations to it via the same AspectJ intertype declaration mechanism.
Example code
These are the most informative bits. See http://lagod.id.au/blog/?p=494 for the full example.
The aspect
package testing;
import org.glassfish.jersey.linking.InjectLink;
import org.glassfish.jersey.linking.Binding;
public aspect HrefInjector {
private String Planet.href;
declare #field : * Planet.href : #InjectLink(
resource=Services.class,
style=InjectLink.Style.ABSOLUTE
) ;
private String Moon.href;
declare #field : * Moon.href : #InjectLink(
resource=Services.class,
method="moon",
bindings={#Binding(
name="moonid", value="${instance.name}"
)},
style=InjectLink.Style.ABSOLUTE
) ;
}
Model classes
POJOs with no REST-specific cruft. See Jersey + Moxy + JAXB - how to marshal XML without annotations.
package testing;
import java.util.ArrayList;
import java.util.List;
public class Planet {
private int id = 1;
private String name = "test";
private double radius = 3.0;
private List<Moon> moons = new ArrayList<Moon>(0);
public void addMoon(Moon moon) {
moons.add(moon);
}
}
package testing;
public class Moon {
private String name;
// No-arg constructor is a requirement of JAXB
public Moon() {
}
public Moon(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Resource class
This is a standard JAX-RS resource class. For demo purposes, we're just returning freshly instantiated model instances.
package testing;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("/services")
#Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
public class Services {
private Planet initPlanet() {
Planet p = new Planet();
p.addMoon(new Moon("moon1"));
p.addMoon(new Moon("moon2"));
return p;
}
#GET
public Planet planet () {
return initPlanet();
}
#GET #Path("/moons/{moonid}")
public Moon moon (#PathParam("moonid") String name) {
return new Moon(name);
}
}
Moxy mapping file
Note that you can choose for any given type whether or not you want to actually marshal the href field. In fact, by using multiple mapping files, you can include the href field in some representations and not in others.
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="testing"
xml-mapping-metadata-complete="true"
xml-accessor-type="NONE">
<java-types>
<java-type name="Planet">
<xml-root-element/>
<java-attributes>
<xml-attribute java-attribute="href"/>
<xml-element java-attribute="name"/>
<xml-element java-attribute="radius"/>
<xml-element java-attribute="moons" name="moon">
<xml-element-wrapper name="moons"/>
</xml-element>
</java-attributes>
</java-type>
<java-type name="Moon">
<xml-root-element/>
<java-attributes>
<xml-attribute java-attribute="href"/>
<xml-element java-attribute="name"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
Sample output
Ta-dah! Structural links derived automatically from the JAX-RS resource class without altering model source code. Because we're using Moxy, we also get JSON for free.
<planet href="http://localhost:8080/reststructlinks/rest/services">
<name>test</name>
<radius>3.0</radius>
<moons>
<moon href="http://localhost:8080/reststructlinks/rest/services/moons/moon1">
<name>moon1</name>
</moon>
<moon href="http://localhost:8080/reststructlinks/rest/services/moons/moon2">
<name>moon2</name>
</moon>
</moons>
</planet>

Filter request URL before any processing in CQ5.6

In my CQ5.6 application,. as soon as the user hits a URL, I need to edit it using a certain parameters. All this must happen before Sling starts processing the URL.
I basically need to convert the URL like:
www.mysite.fr --> converts to --> /content/mysite/fr/
and so on....
I understand I'll need to create an OSGi bundle for this, but which API should I use to ensure that the URL is filtered by my class first and then catered by
Sling. ?
if you want a code-based solution for multiple websites (and you don't want to manage /etc/map) you can setup your own Filter:
package your.package;
import org.apache.felix.scr.annotations.*;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.osgi.service.component.ComponentContext;
#Component(immediate=true, enabled=true)
#Service(value=Filter.class)
#Properties({
#Property(name="sling.filter.scope", value="REQUEST", propertyPrivate=true),
#Property(name="service.ranking", intValue=-10000, propertyPrivate=true)
})
public class YourFilter implements Filter {
private static final Logger log = LoggerFactory.getLogger(ProductSEOFilter.class);
#Activate
protected void activate(ComponentContext ctx) throws Exception {
}
#Deactivate
protected void deactivate() throws Exception {
}
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws java.io.IOException, ServletException {
String lang = "en";
// 1. get domain and path
// 2. check if your conditions are met
// 3. extract language from domain
// 4. internal redirect
RequestDispatcher dispatch = request.getRequestDispatcher("/content/mysite/" + lang);
dispatch.forward(request, response);
}
public void destroy() {
}
}
you don't need to bother checking for and passing querystrings--those are carried on in the dispatcher. it only needs a new url to forward to.
You can do this via Sling URL Mapping without the need for a filter. The simpliest way to achieve this is to create a node under the /etc/map directory with a resource type of sling:Mapping & called www.mysite.fr.
This then takes a property of sling:internalRedirect — if an incoming request matches the node name, this property is appended to the path to continue with internal resource resolution.
<map>
<http jcr:primaryType="sling:OrderedFolder">
<www.mysite.fr
jcr:primaryType="sling:Mapping"
sling:internalRedirect="/content/mysite/fr"/>
</http>
</map>
The above will ensure any request coming to www.mysite.fr is resolved to www.mysite.fr/content/mysite/fr.
You can also pattern matching based on regex properties rather than names & include port numbers or schemes too. The full documentation is available on the Sling website.