Ask is moving to Stack Overflow and Serverfault.com! Please use the "opendaylight" tag on either of these sites. This site is now in Read-Only mode

0

Adding CORS support for my Plugin

asked 2015-01-21 01:25:30 -0700

miclando gravatar image

hi, i have a plugin i developed using AD sal.
the plugin exposes a number of rest API's.
these api's are accessed from a web application deployed on another domain.
because of this my access are cross domain.
currently i am using jsonp for this sort of access.
what i am trying to do is to enable CORS support on my opendaylight hydrogen.
from what i managed to uncover. i need to to add my apis to cors-config.xml.
but that didn't work.
i also tried defining the filter in the plugin web xml but again with no success has anyone manged to get this to work?

edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted
0

answered 2015-02-03 03:21:40 -0700

miclando gravatar image

after a long search a found the answer. in case you want to enable cors support on Hydrogen release: enable the CORS on the ODL web container (Tomcat) : 1. Copy the following jar to your plugin dir in the ODL org.opendaylight.controller.filter-valve-1.4.2-SNAPSHOT.jar donload link: https://nexus.opendaylight.org/service/local/repositories/opendaylight.snapshot/content/org/opendaylight/controller/filter-valve/1.4.2-SNAPSHOT/filter-valve-1.4.2-20141001.225558-591.jar

  1. Go to ./configuration/tomcat-server.xml
  2. In the file add the following marked line:

    <Engine name="Catalina" defaultHost="localhost">
        <Host name="localhost" appBase="" unpackWARs="false" autoDeploy="false" deployOnStartup="false" createDirs="false">
        <Realm className="org.opendaylight.controller.security.ControllerCustomRealm" />
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                        prefix="web_access_log_" suffix=".txt" resolveHosts="false"
                        rotatable="true" fileDateFormat="yyyy-MM"
                        pattern="%{yyyy-MM-dd HH:mm:ss.SSS z}t - [%a] - %r"/>
    
        <!--add this line!-->
        <Valve className="org.opendaylight.controller.filtervalve.cors.FilterValve" configurationFile="configuration/cors-config.xml"/>
    
      </Host>
    </Engine>
    
  3. Create cors-config.xml file in configuration dir under ODL. The file contains filter definitions for tomcat. here you can define your path and add the CORS filters to it as was done for restconf api.
<Host>
  <!-- Filters are allowed here, only serving as a template -->
  <filter-template>
    <filter-name>CorsFilter</filter-name>
    <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
    <init-param>
      <param-name>cors.allowed.origins</param-name>
      <param-value>*</param-value>
    </init-param>
    <init-param>
      <param-name>cors.allowed.methods</param-name>
      <param-value>GET,POST,HEAD,OPTIONS,PUT,DELETE</param-value>
    </init-param>
    <init-param>
      <param-name>cors.allowed.headers</param-name>
      <param-value>Content-Type,X-Requested-With,accept,authorization,
        origin,Origin,Access-Control-Request-Method,Access-Control-Request-Headers
      </param-value>
    </init-param>
    <init-param>
      <param-name>cors.exposed.headers</param-name>
      <param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
    </init-param>
    <init-param>
      <param-name>cors.support.credentials</param-name>
      <param-value>true</param-value>
    </init-param>
    <init-param>
      <param-name>cors.preflight.maxage</param-name>
      <param-value>10</param-value>
    </init-param>
  </filter-template>

  <Context path="/restconf">
    <filter>
      <filter-name>CorsFilter</filter-name>
      <!-- init params can be added/overriden if template is used> -->
    </filter>
    <!-- references to templates without <filter> declaration are not allowed -->
    <filter-mapping>
      <filter-name>CorsFilter</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>
  </Context>




</Host>
edit flag offensive delete publish link more
0

answered 2016-11-03 02:09:56 -0700

icehong gravatar image

updated 2016-11-14 17:45:31 -0700

I am using Beryllium-SR4 version of odl package, and get filter-valve.jar from the URL : https://nexus.opendaylight.org/servic... . Then I copied to ./deploy/ directory.

But it seams doesn't work. I got such error from karaf web console Bundles page :

org.apache.catalina,version=[7.0,8) -- Cannot be resolved org.apache.catalina.connector,version=[7.0,8) -- Cannot be resolved org.apache.catalina.valves,version=[7.0,8) -- Cannot be resolved

Then I downloaded catalina jar and copy to deploy directory, then I had to import more jars.

edit flag offensive delete publish link more
0

answered 2016-11-04 05:21:39 -0700

Hello,

I've done that in the UniMgr project of ODL. I would suggest looking at this gerrit.

Hope this helps, Alexis

edit flag offensive delete publish link more

Comments

Hi Alexis , Thanks. It seems that this patch is merge into beryllium branch, but I still got CORS error when calling a restconf interface form another server . Could you give me a simple guide to open it ?ใ€€ใ€€ใ€€ I have tried miclando's answer but got error in the last comment. Thanks a lot .

icehong ( 2016-11-14 18:01:14 -0700 )edit

@icehong, the patch I've linked is just an example of how to add that functionality. You would have to do the same in your project's configuration. There is an existing wiki page but it's pretty much outdated so I'd rather not linking it here.

adetalhouet ( 2016-11-22 07:07:44 -0700 )edit
Login/Signup to Answer

Stats

Asked: 2015-01-21 01:25:30 -0700

Seen: 616 times

Last updated: Nov 14 '16