Custom RepositoryService and URLStreamHandler in Jaspersoft Studio

0

Hi everybody,

I am looking to add my custom RepositoryService to Jaspersoft Studio. I have implemented a custom protocol using a new URLStreamHandlerFactory. My code works just fine when use it in my application, although I would like to have the same functionality in the workbench at design time.

CustomRepositoryService.java

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.util.Arrays;
import net.sf.jasperreports.engine.DefaultJasperReportsContext;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperReportsContext;
import net.sf.jasperreports.engine.SimpleJasperReportsContext;
import net.sf.jasperreports.repo.DefaultRepositoryService;
import net.sf.jasperreports.repo.RepositoryService;
 
public class CustomRepositoryService extends DefaultRepositoryService {
 
    public CustomRepositoryService(JasperReportsContext parent) {
        super(parent);
 
        setURLStreamHandlerFactory(proto -> {
            return "myproto".equals(proto) ? new URLStreamHandler() {
                @Override
                protected URLConnection openConnection(URL u) throws IOException {
                    return new URLConnection(u) {
 
                        @Override
                        public void connect() throws IOException {
                            // noop
                        }
 
                        @Override
                        public InputStream getInputStream() throws IOException {
                            // do custom stuff to retrieve the data stream
                            return super.getInputStream();
                        }
                    };
                }
            } : null;
        });
    }
}

Usage example snippet

        SimpleJasperReportsContext ctx = new SimpleJasperReportsContext();
        CustomRepositoryService repositoryService = new CustomRepositoryService(DefaultJasperReportsContext.getInstance());
        ctx.setExtensions(RepositoryService.class, Arrays.asList(repositoryService));
        JasperFillManager fillManager = JasperFillManager.getInstance(ctx);
        JasperPrint jasperPrint = fillManager.fill(jasperReport, parameters);

JRXML example snippet

 
 
 
				<!--[CDATA["myproto:/images/logo.jpg"]]-->
 

Is it possible to attach this solution to the Jaspesoft Studio?
Is there a better way to achieve the same goal?

Any help would be much appreciated!

Best Regards,
Zsolt

zharsing's picture
Joined: Feb 15 2014 - 1:33am
Last seen: 21 hours 26 min ago

1 Answer:

1

Hi anybody,

I managed to make it work as an Extension based on this javadoc

http://jasperreports.sourceforge.net/api/net/sf/jasperreports/extensions/package-summary.html

Best Regards,
Zsolt

zharsing's picture
Joined: Feb 15 2014 - 1:33am
Last seen: 21 hours 26 min ago
Feedback
randomness