Search in sources :

Example 1 with ResourceProvider

use of org.apache.cxf.jaxrs.lifecycle.ResourceProvider in project tika by apache.

the class StackTraceOffTest method setUpResources.

@Override
protected void setUpResources(JAXRSServerFactoryBean sf) {
    List<ResourceProvider> rCoreProviders = new ArrayList<ResourceProvider>();
    rCoreProviders.add(new SingletonResourceProvider(new MetadataResource()));
    rCoreProviders.add(new SingletonResourceProvider(new RecursiveMetadataResource()));
    rCoreProviders.add(new SingletonResourceProvider(new DetectorResource()));
    rCoreProviders.add(new SingletonResourceProvider(new TikaResource()));
    rCoreProviders.add(new SingletonResourceProvider(new UnpackerResource()));
    sf.setResourceProviders(rCoreProviders);
}
Also used : RecursiveMetadataResource(org.apache.tika.server.resource.RecursiveMetadataResource) MetadataResource(org.apache.tika.server.resource.MetadataResource) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider) ResourceProvider(org.apache.cxf.jaxrs.lifecycle.ResourceProvider) TikaResource(org.apache.tika.server.resource.TikaResource) ArrayList(java.util.ArrayList) DetectorResource(org.apache.tika.server.resource.DetectorResource) RecursiveMetadataResource(org.apache.tika.server.resource.RecursiveMetadataResource) UnpackerResource(org.apache.tika.server.resource.UnpackerResource) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider)

Example 2 with ResourceProvider

use of org.apache.cxf.jaxrs.lifecycle.ResourceProvider in project tika by apache.

the class TikaWelcomeTest method setUpResources.

@Override
protected void setUpResources(JAXRSServerFactoryBean sf) {
    List<ResourceProvider> rpsCore = new ArrayList<ResourceProvider>();
    rpsCore.add(new SingletonResourceProvider(new TikaVersion()));
    rpsCore.add(new SingletonResourceProvider(new DetectorResource()));
    rpsCore.add(new SingletonResourceProvider(new MetadataResource()));
    List<ResourceProvider> all = new ArrayList<ResourceProvider>(rpsCore);
    all.add(new SingletonResourceProvider(new TikaWelcome(rpsCore)));
    sf.setResourceProviders(all);
}
Also used : MetadataResource(org.apache.tika.server.resource.MetadataResource) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider) ResourceProvider(org.apache.cxf.jaxrs.lifecycle.ResourceProvider) ArrayList(java.util.ArrayList) DetectorResource(org.apache.tika.server.resource.DetectorResource) TikaVersion(org.apache.tika.server.resource.TikaVersion) TikaWelcome(org.apache.tika.server.resource.TikaWelcome) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider)

Example 3 with ResourceProvider

use of org.apache.cxf.jaxrs.lifecycle.ResourceProvider in project tika by apache.

the class TikaServerCli method main.

public static void main(String[] args) {
    LOG.info("Starting {} server", new Tika());
    try {
        Options options = getOptions();
        CommandLineParser cliParser = new GnuParser();
        CommandLine line = cliParser.parse(options, args);
        if (line.hasOption("help")) {
            HelpFormatter helpFormatter = new HelpFormatter();
            helpFormatter.printHelp("tikaserver", options);
            System.exit(-1);
        }
        String host = DEFAULT_HOST;
        if (line.hasOption("host")) {
            host = line.getOptionValue("host");
            if ("*".equals(host)) {
                host = "0.0.0.0";
            }
        }
        int port = DEFAULT_PORT;
        if (line.hasOption("port")) {
            port = Integer.valueOf(line.getOptionValue("port"));
        }
        boolean returnStackTrace = false;
        if (line.hasOption("includeStack")) {
            returnStackTrace = true;
        }
        TikaLoggingFilter logFilter = null;
        if (line.hasOption("log")) {
            String logLevel = line.getOptionValue("log");
            if (LOG_LEVELS.contains(logLevel)) {
                boolean isInfoLevel = "info".equals(logLevel);
                logFilter = new TikaLoggingFilter(isInfoLevel);
            } else {
                LOG.info("Unsupported request URI log level: {}", logLevel);
            }
        }
        CrossOriginResourceSharingFilter corsFilter = null;
        if (line.hasOption("cors")) {
            corsFilter = new CrossOriginResourceSharingFilter();
            String url = line.getOptionValue("cors");
            List<String> origins = new ArrayList<String>();
            // Empty list allows all origins.
            if (!url.equals("*"))
                origins.add(url);
            corsFilter.setAllowOrigins(origins);
        }
        // The Tika Configuration to use throughout            
        TikaConfig tika;
        if (line.hasOption("config")) {
            String configFilePath = line.getOptionValue("config");
            LOG.info("Using custom config: {}", configFilePath);
            tika = new TikaConfig(configFilePath);
        } else {
            tika = TikaConfig.getDefaultConfig();
        }
        DigestingParser.Digester digester = null;
        if (line.hasOption("digest")) {
            int digestMarkLimit = DEFAULT_DIGEST_MARK_LIMIT;
            if (line.hasOption("dml")) {
                String dmlS = line.getOptionValue("dml");
                try {
                    digestMarkLimit = Integer.parseInt(dmlS);
                } catch (NumberFormatException e) {
                    throw new RuntimeException("Must have parseable int after digestMarkLimit(dml): " + dmlS);
                }
            }
            digester = new CommonsDigester(digestMarkLimit, CommonsDigester.parse(line.getOptionValue("digest")));
        }
        if (line.hasOption("enableFileUrl") && !line.hasOption("enableUnsecureFeatures")) {
            System.err.println("If you want to enable fileUrl, you must also acknowledge the security risks\n" + "by including --enableUnsecureFeatures.  See CVE-2015-3271.");
            System.exit(-1);
        }
        InputStreamFactory inputStreamFactory = null;
        if (line.hasOption("enableFileUrl") && line.hasOption("enableUnsecureFeatures")) {
            inputStreamFactory = new URLEnabledInputStreamFactory();
            System.out.println(FILE_URL_WARNING);
        } else {
            inputStreamFactory = new DefaultInputStreamFactory();
        }
        TikaResource.init(tika, digester, inputStreamFactory);
        JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
        List<ResourceProvider> rCoreProviders = new ArrayList<>();
        rCoreProviders.add(new SingletonResourceProvider(new MetadataResource()));
        rCoreProviders.add(new SingletonResourceProvider(new RecursiveMetadataResource()));
        rCoreProviders.add(new SingletonResourceProvider(new DetectorResource()));
        rCoreProviders.add(new SingletonResourceProvider(new LanguageResource()));
        rCoreProviders.add(new SingletonResourceProvider(new TranslateResource()));
        rCoreProviders.add(new SingletonResourceProvider(new TikaResource()));
        rCoreProviders.add(new SingletonResourceProvider(new UnpackerResource()));
        rCoreProviders.add(new SingletonResourceProvider(new TikaMimeTypes()));
        rCoreProviders.add(new SingletonResourceProvider(new TikaDetectors()));
        rCoreProviders.add(new SingletonResourceProvider(new TikaParsers()));
        rCoreProviders.add(new SingletonResourceProvider(new TikaVersion()));
        List<ResourceProvider> rAllProviders = new ArrayList<>(rCoreProviders);
        rAllProviders.add(new SingletonResourceProvider(new TikaWelcome(rCoreProviders)));
        sf.setResourceProviders(rAllProviders);
        List<Object> providers = new ArrayList<>();
        providers.add(new TarWriter());
        providers.add(new ZipWriter());
        providers.add(new CSVMessageBodyWriter());
        providers.add(new MetadataListMessageBodyWriter());
        providers.add(new JSONMessageBodyWriter());
        providers.add(new XMPMessageBodyWriter());
        providers.add(new TextMessageBodyWriter());
        providers.add(new TikaServerParseExceptionMapper(returnStackTrace));
        if (logFilter != null) {
            providers.add(logFilter);
        }
        if (corsFilter != null) {
            providers.add(corsFilter);
        }
        sf.setProviders(providers);
        String url = "http://" + host + ":" + port + "/";
        sf.setAddress(url);
        BindingFactoryManager manager = sf.getBus().getExtension(BindingFactoryManager.class);
        JAXRSBindingFactory factory = new JAXRSBindingFactory();
        factory.setBus(sf.getBus());
        manager.registerBindingFactory(JAXRSBindingFactory.JAXRS_BINDING_ID, factory);
        sf.create();
        LOG.info("Started Apache Tika server at {}", url);
    } catch (Exception ex) {
        LOG.error("Can't start", ex);
        System.exit(-1);
    }
}
Also used : TikaParsers(org.apache.tika.server.resource.TikaParsers) JAXRSBindingFactory(org.apache.cxf.jaxrs.JAXRSBindingFactory) GnuParser(org.apache.commons.cli.GnuParser) ArrayList(java.util.ArrayList) UnpackerResource(org.apache.tika.server.resource.UnpackerResource) BindingFactoryManager(org.apache.cxf.binding.BindingFactoryManager) Tika(org.apache.tika.Tika) HelpFormatter(org.apache.commons.cli.HelpFormatter) MetadataListMessageBodyWriter(org.apache.tika.server.writer.MetadataListMessageBodyWriter) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider) ResourceProvider(org.apache.cxf.jaxrs.lifecycle.ResourceProvider) CommonsDigester(org.apache.tika.parser.utils.CommonsDigester) XMPMessageBodyWriter(org.apache.tika.server.writer.XMPMessageBodyWriter) TikaDetectors(org.apache.tika.server.resource.TikaDetectors) MetadataResource(org.apache.tika.server.resource.MetadataResource) RecursiveMetadataResource(org.apache.tika.server.resource.RecursiveMetadataResource) JSONMessageBodyWriter(org.apache.tika.server.writer.JSONMessageBodyWriter) DigestingParser(org.apache.tika.parser.DigestingParser) CrossOriginResourceSharingFilter(org.apache.cxf.rs.security.cors.CrossOriginResourceSharingFilter) DetectorResource(org.apache.tika.server.resource.DetectorResource) TranslateResource(org.apache.tika.server.resource.TranslateResource) RecursiveMetadataResource(org.apache.tika.server.resource.RecursiveMetadataResource) Options(org.apache.commons.cli.Options) ZipWriter(org.apache.tika.server.writer.ZipWriter) LanguageResource(org.apache.tika.server.resource.LanguageResource) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider) CSVMessageBodyWriter(org.apache.tika.server.writer.CSVMessageBodyWriter) TextMessageBodyWriter(org.apache.tika.server.writer.TextMessageBodyWriter) TikaMimeTypes(org.apache.tika.server.resource.TikaMimeTypes) CommandLineParser(org.apache.commons.cli.CommandLineParser) TarWriter(org.apache.tika.server.writer.TarWriter) TikaConfig(org.apache.tika.config.TikaConfig) TikaResource(org.apache.tika.server.resource.TikaResource) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) TikaVersion(org.apache.tika.server.resource.TikaVersion) CommandLine(org.apache.commons.cli.CommandLine) TikaWelcome(org.apache.tika.server.resource.TikaWelcome)

Example 4 with ResourceProvider

use of org.apache.cxf.jaxrs.lifecycle.ResourceProvider in project tomee by apache.

the class CXFNonSpringJaxrsServlet method init.

@Override
public void init(ServletConfig servletConfig) throws ServletException {
    super.init(servletConfig);
    if (getApplication() != null) {
        createServerFromApplication(servletConfig);
        return;
    }
    String applicationClass = servletConfig.getInitParameter(JAXRS_APPLICATION_PARAM);
    if (applicationClass != null) {
        createServerFromApplication(applicationClass, servletConfig);
        return;
    }
    String splitChar = getParameterSplitChar(servletConfig);
    JAXRSServerFactoryBean bean = new JAXRSServerFactoryBean();
    bean.setBus(getBus());
    String address = servletConfig.getInitParameter(SERVICE_ADDRESS_PARAM);
    if (address == null) {
        address = "/";
    }
    bean.setAddress(address);
    bean.setStaticSubresourceResolution(getStaticSubResolutionValue(servletConfig));
    String modelRef = servletConfig.getInitParameter(USER_MODEL_PARAM);
    if (modelRef != null) {
        bean.setModelRef(modelRef.trim());
    }
    setDocLocation(bean, servletConfig);
    setSchemasLocations(bean, servletConfig);
    setAllInterceptors(bean, servletConfig, splitChar);
    setInvoker(bean, servletConfig);
    Map<Class<?>, Map<String, List<String>>> resourceClasses = getServiceClasses(servletConfig, modelRef != null, splitChar);
    Map<Class<?>, ResourceProvider> resourceProviders = getResourceProviders(servletConfig, resourceClasses);
    List<?> providers = getProviders(servletConfig, splitChar);
    bean.setResourceClasses(new ArrayList<Class<?>>(resourceClasses.keySet()));
    bean.setProviders(providers);
    for (Map.Entry<Class<?>, ResourceProvider> entry : resourceProviders.entrySet()) {
        bean.setResourceProvider(entry.getKey(), entry.getValue());
    }
    setExtensions(bean, servletConfig);
    List<? extends Feature> features = getFeatures(servletConfig, splitChar);
    bean.getFeatures().addAll(features);
    bean.create();
}
Also used : PerRequestResourceProvider(org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider) ResourceProvider(org.apache.cxf.jaxrs.lifecycle.ResourceProvider) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with ResourceProvider

use of org.apache.cxf.jaxrs.lifecycle.ResourceProvider in project tomee by apache.

the class ResourceUtils method createApplication.

@SuppressWarnings("unchecked")
public static JAXRSServerFactoryBean createApplication(Application app, boolean ignoreAppPath, boolean staticSubresourceResolution, boolean useSingletonResourceProvider, Bus bus) {
    Set<Object> singletons = app.getSingletons();
    verifySingletons(singletons);
    List<Class<?>> resourceClasses = new ArrayList<>();
    List<Object> providers = new ArrayList<>();
    List<Feature> features = new ArrayList<>();
    Map<Class<?>, ResourceProvider> map = new HashMap<>();
    // or singleton provider classes
    for (Class<?> cls : app.getClasses()) {
        if (isValidApplicationClass(cls, singletons)) {
            if (isValidProvider(cls)) {
                providers.add(createProviderInstance(cls));
            } else if (Feature.class.isAssignableFrom(cls)) {
                features.add(createFeatureInstance((Class<? extends Feature>) cls));
            } else {
                resourceClasses.add(cls);
                if (useSingletonResourceProvider) {
                    map.put(cls, new SingletonResourceProvider(createProviderInstance(cls)));
                } else {
                    map.put(cls, new PerRequestResourceProvider(cls));
                }
            }
        }
    }
    // we can get either a provider or resource class here
    for (Object o : singletons) {
        if (isValidProvider(o.getClass())) {
            providers.add(o);
        } else if (o instanceof Feature) {
            features.add((Feature) o);
        } else {
            resourceClasses.add(o.getClass());
            map.put(o.getClass(), new SingletonResourceProvider(o));
        }
    }
    JAXRSServerFactoryBean bean = new JAXRSServerFactoryBean();
    if (bus != null) {
        bean.setBus(bus);
    }
    String address = "/";
    if (!ignoreAppPath) {
        ApplicationPath appPath = locateApplicationPath(app.getClass());
        if (appPath != null) {
            address = appPath.value();
        }
    }
    if (!address.startsWith("/")) {
        address = "/" + address;
    }
    bean.setAddress(address);
    bean.setStaticSubresourceResolution(staticSubresourceResolution);
    bean.setResourceClasses(resourceClasses);
    bean.setProviders(providers);
    bean.getFeatures().addAll(features);
    for (Map.Entry<Class<?>, ResourceProvider> entry : map.entrySet()) {
        bean.setResourceProvider(entry.getKey(), entry.getValue());
    }
    Map<String, Object> appProps = app.getProperties();
    if (appProps != null) {
        bean.getProperties(true).putAll(appProps);
    }
    bean.setApplication(app);
    return bean;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JAXRSServerFactoryBean(org.apache.cxf.jaxrs.JAXRSServerFactoryBean) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider) ApplicationPath(javax.ws.rs.ApplicationPath) Feature(org.apache.cxf.feature.Feature) PerRequestResourceProvider(org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider) ResourceProvider(org.apache.cxf.jaxrs.lifecycle.ResourceProvider) SingletonResourceProvider(org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider) ElementClass(org.apache.cxf.jaxrs.ext.xml.ElementClass) PerRequestResourceProvider(org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider) Map(java.util.Map) HashMap(java.util.HashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Aggregations

ResourceProvider (org.apache.cxf.jaxrs.lifecycle.ResourceProvider)25 SingletonResourceProvider (org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider)14 PerRequestResourceProvider (org.apache.cxf.jaxrs.lifecycle.PerRequestResourceProvider)8 ArrayList (java.util.ArrayList)7 JAXRSServerFactoryBean (org.apache.cxf.jaxrs.JAXRSServerFactoryBean)6 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)5 OperationResourceInfo (org.apache.cxf.jaxrs.model.OperationResourceInfo)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 DetectorResource (org.apache.tika.server.resource.DetectorResource)4 MetadataResource (org.apache.tika.server.resource.MetadataResource)4 RecursiveMetadataResource (org.apache.tika.server.resource.RecursiveMetadataResource)3 TikaResource (org.apache.tika.server.resource.TikaResource)3 UnpackerResource (org.apache.tika.server.resource.UnpackerResource)3 ApplicationPath (javax.ws.rs.ApplicationPath)2 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)2 Feature (org.apache.cxf.feature.Feature)2 ResourceContextProvider (org.apache.cxf.jaxrs.ext.ResourceContextProvider)2 ElementClass (org.apache.cxf.jaxrs.ext.xml.ElementClass)2 MessageImpl (org.apache.cxf.message.MessageImpl)2