Overriding Lyo Designer output¶
Sometimes, Lyo Designer generates code, which, in turn, produces REST responses that you would like to modify.
Initially, you may be tempted to edit the generated code directly. However, it is best not to make manual changes to the generated code. This helps prevent you changes from being overwritten by Lyo Designer the next time you re-generate the code.
Reminder about code generation
You may be familiar with a few code generators, such as in Ruby on Rails. Those are scaffolding generators, meant to help you get started. They generate code only once and encourage you modify it further. Some other generators, e.g. to generate OpenAPI clients, may be designed to be always regenerated from the definitions. Lyo Designer features an MBSE-style (model-based systems engineering) code generator that is designed to be used repeatedly and symbiotically when designing an OSLC Server.
Preferred options include (in the order of preference):
- Adjust the model definition so that Lyo Designer generates the code that suits your needs.
- Find a code segment separated by comments
Start of user code
andEnd of user code
and place your code there. Such code will be preserved upon re-generation. - File a bug on Lyo Designer and request a new user code block to be added where you need it.
- Consider changes to the dependency injection in the
ApplicationBinder
to use your implementations where necessary. - Consider using an interceptor below.
- Resort to making changes to the generated code.
Defining an interceptor¶
A sample interceptor below adds an extra jfs:oauthRealmName
property to all responses featuring an OSLC Service Provider Catalog.
import javax.ws.rs.ext.WriterInterceptor;
import javax.ws.rs.ext.WriterInterceptorContext;
import javax.xml.namespace.QName;
@Provider
public class ExtendedPropWriteInterceptor implements WriterInterceptor {
private static final QName OAUTH_REALM_NAME = new QName("http://jazz.net/xmlns/prod/jazz/jfs/1.0/", "oauthRealmName");
private final Logger log = LoggerFactory.getLogger(ExtendedPropWriteInterceptor.class);
@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
log.info("Interceptor called");
final Object entity = context.getEntity();
if (entity instanceof ServiceProviderCatalog) {
final ServiceProviderCatalog catalog = (ServiceProviderCatalog) entity;
catalog.getExtendedProperties().put(OAUTH_REALM_NAME, AuthenticationApplication.OAUTH_REALM);
context.setEntity(catalog);
}
context.proceed();
}
}