Opera Unite JavaScript API

Jul1509Jul 15, 09

I've been taking a close look at what's happening in the early stages of Opera Unite's adoption. People seem to like it, but nobody I have spoken with has committed to maintaining their Unite server presence. That said, I think the opportunity is on us for some really cool web apps powered by users' computers instead of our own servers and bandwidth. I've been extremely busy working on amoebaOS recently as things are being prepared for the initial public release, so for the sake of brevity I thought it would be useful to post the documentation for Opera Unite's JavaScript API here. This was pulled from the JavaScript example file as posted on opera.com - I wanted to re-post it, because their version does not render as an HTML document (it is contained within the JavaScript code).



Opera Unite Web server APIs

This API document describes the JavaScript bindings for the Opera Unite Web Server, available through Opera Unite services.

Unite services are packaged like Opera Widgets with a config.xml file.

Note that no methods in this API return actual arrays. Instead they return objects whose properties can be iterated over in an array style syntax, i.e.array[i]. Methods like push(), shift(), sort(), and so on are not available. [my note: you can still use these Array methods by calling Array.prototype.push.apply(apiResponse,[newItem]); ]

Enabling the Web server API

In order to make the file system and its methods available, you need to add a feature element to your config.xml file like this:

<widget>
...
<feature name="http://xmlns.opera.com/webserver">
<param name="servicepath" value="chat" />
</feature>
...
</widget>

The servicepath parameter is required and defines the path the service will be accessed from. See the note on service URLs for more information.

The entry point to the API becomes available as a JavaScript object opera.io.webserver.

Opera Unite services and URLs

Opera Unite users may start a Web server on different devices running Opera, for example on a home and work computer, a living room media center and a mobile phone. The user may deploy a set of services on each Web server. A service is typically a single application, for example a file sharing service or a multiplayer chess service. Services can accessed on the following general URL:

http://device.user.proxy/service/path

For example:

http://work.john.operaunite.com/wiki/add

The username is the user's My Opera username.

The device name is whatever the user has chosen when enabling the Web server. It's tied to one instance of Opera.

The proxy is a server run by Opera, proxying requests between servers and clients.

The service, or service path, is the IRI name of the service, as defined in config.xml.

The path represents a file or instruction the service will handle.

Note that only valid IRI characters can be used as part of the device name, username and service name.

Working with requests and responses

The function of a Web server is to listen to requests from clients, process them and issue a response, for example send a Web page or the result of a script. Requests occur when the user visits a particular service URL. The request may contain form data, send via GET and POST and uploaded files.

In the case of the Opera Unite Web Server you listen to requests from clients by adding event listeners to the opera.io.webserver object:

opera.io.webserver.addEventListener('path',somehandler,false);

The path corresponds to the first path component in the URL after the service name, for example:

opera.io.webserver.addEventListener('add',addHandler,false);

In this case, if the user visits the URL

http://work.john.operaunite.com/wiki/add

The service will pick it up and call the supplied addHandler function.

Handler functions are passed a {@link WebServerRequestEvent} object as an argument. This event object gives access to the incoming connection ({@link WebServerConnection}), the incoming request ({@link WebServerRequest}), and the response ({@link WebServerResponse}) the author can write data to. The addhandler could look something like this:

function addHandler(e)
{
//Shorthands
var req = e.connection.request;
var res = e.connection.response;

//Get article from POST data
var article = req.bodyItems['article'][0];

//...Store the article data

//Write a response back to the client
res.write('Article updated');
res.close();

}

Data sent as GET are available in the queryItems property of the request. Data sent as POST are available in the bodyItems property. The body of the request is also available in the raw form in the body property. If files were attached, these are available in the files property of the request.

The response object has methods for sending different kinds of data, for example writeImage() and writeFile().

You may supply a custom protocol string, status code and headers, but these must be set before data is written to the response.

Note that requests may not begin with '_', as this is reserved for special, system generated events.

Special requests: _index, _request, _close

The Web server supports three special requests.

_index
This request occurs when a user visits the root of the service, e.g. http://work.john.operaunite.com/share. If you do not listen for this request, the file public_html/index.html will be served when the user visits the root of the server.
_request
This is a special request that catches all incoming requests, except for _close. Use this to gain fine grained control of the requests made to the server. You'll need to use {@link WebServerRequest#uri} to differentiate between different requests. This request also catches _index, but not _close.
_close
This request occurs when a connection is closed. The connection property of the resulting event is a dummy object, while the id property contains the id of the closed connection.

If the developer adds event listeners for both _request and some specific path, the event handler for the specific path will be called before the _request handler. Handlers for _index are also fired before _request.

Redispatching requests

You can redispatch a request to a new URI by changing the {@link WebServerRequest#uri} property and calling the {@link WebServerResponse#closeAndRedispatch} method. This is useful for example for correcting commonly mistyped URLs, chaining requests and providing authentication across the entire service. For example:


if ( ! authenticated )
{
request.uri = opera.io.webserver.currentServicePath + 'loginform';
response.closeAndRedispatch();
}

And headers, GET or POST data in the original request are sent along with the redispatched request.

Note that uri must be set to a relative URI path, starting with '/' and the name of the service (the {@link opera.io.webserver#currentServicePath} property), potentially followed by more path components, for example '/blog/add'.

If a request is redispatched, none of the special event handlers, like _request will be fired.

Requests and files

If an incoming request URI matches a shared file or a file in public_html, the file will be served.

However, if you listen for the _request event, even requests to files will be intercepted. In order to serve the file, you'll need to call {@link WebServerResponse#closeAndRedispatch} without changing the URI.

Sharing files

You can share files from a local disk through your Web server. Use the {@link opera.io.webserver#shareFile} and {@link opera.io.webserver#unshareFile} methods. The first takes two arguments - a File object retrieved using the File I/O API, and the name used to share it on the web.

Here's an example:

//mount a directory using File I/O, get a reference to it called 'dir'
opera.io.webserver.shareFile(dir,'myShare');

Assuming the device is called 'work', the user is called 'john' and the service is called 'fileSharing', the directory will now be available on the URL http://work.john.operaunite.com/fileSharing/myShare.

Note that for security reasons the contents of a directory is not listed automatically. You'll need to listen for the name of the share as a request, and serve a listing to the client yourself. You may still access files under the shared directory, for example:

http://work.john.operaunite.com/fileSharing/myShare/notes.txt

Uploading files

You can upload files using a normal file upload form. Once uploaded, the file will be available in the {@link WebServerRequest#files} property. This is a special File object that functions like a directory. You can iterate through it to locate your uploaded file.

The File object and its children will be deleted when the request object goes out of scope. You must make sure to read it or copy it before this happens.

for ( var i = 0, file; file = request.files[i]; i++ )
{
file.copyTo('/storage/' + file.name);
}

The value of the name property of the File object will be the same as the file name the uploaded file had on the user's disk.

Each uploaded file has a metaData property, which contains a dictionary of headers for that uploaded file.

Working with cookies

You can set cookies using the Set-Cookie header as you would for a normal Web page. Note that if you do not specify a path, the Web server will set it to the path of the current service. For security reasons, you are not allowed to set cookies for just the operaunite.com host itself. Cookies cannot be shared among services.

The unite:// URL protocol and WebServerRequest.isOwner

In order to allow administration of the service, the service developer will need to be able to positively identify a user as the owner of the service.

All links to pages generated by the service can be expressed as unite://[path] rather than http://[path]. Such a link will give the user a security error if it is not followed from a page in the same instance of Opera that is runing the service.

Such URLs can also be opened from the native UI, or from the root service page. In other cases, Opera will show a dialogue asking the user to confirm that he or she wants to log in and administrate the running service.

In cases where the service is accessed through unite://, the corresponding {@link WebServerConnection} object will have its isOwner property set to true. Service developers can check this property and provide an administration interface.

@author Hans S. Toemmerholt, Web Applications, Opera Software ASA


About Jason Miller:

I am a JavaScript developer from Waterloo, Ontario, Canada. When I am not typing green code onto a black screen, you might find me at the nearest coffee pub checking out the brew. I run a internet firm called developIT and maintain blogs and web apps when I can.
Comments
chenyan#
http://www.oakleysunglassesb.us.com/
http://www.oakleysunglass.us.com/
http://www.oakleysunglasses.qc.com/
http://www.rayban-sunglasses.net.co/
http://www.raybansunglasses.qc.com/
http://www.ray-banoutlet.name/
http://www.polo--ralphlauren.net/
http://www.ralphlauren-outlet.in.net/
http://www.burberryoutletonline.org/
http://www.burberry-outlet-sale.us.com/
http://www.toms-shoes.us.org/
http://www.tomsoutlet.com.co/
http://www.michaelkorsoutlet-mk.eu.com/
http://www.michaelkorsoutlet.in.net/
http://www.christian--louboutin.net/
http://www.tory--burch.org/
http://www.toryburch-outlet.net/
http://www.gucci--outlet.org/
http://www.guccihandbag.in.net/
http://www.cheap-jordan-shoes.net/
http://www.air-jordan.in.net/
http://www.cheap-jordans.in.net/
http://www.jordansretro.in.net/
http://www.nikeshoesinc.net/
http://www.nike-shoes.in.net/
http://www.air-max-2015.net/
http://www.airmax-90.in.net/
http://www.airmax-95.in.net/
http://www.nike-free-run.net/
http://www.nikefree-5.com/
http://www.chanel--handbags.net/
http://www.chanelbags-outlet.us/
http://www.true-religion.cc/
http://www.north-faceoutlet.in.net/
http://www.coach-outlet-store.in.net/
http://www.abercrombie.ar.com/
http://www.hollister.cn.com/
http://www.hermes.us.org/
http://www.new-balanceoutlet.net/
http://www.reebok-shoes.net/
http://www.lululemon.com.co/
http://www.yogapants.eu.com/
http://www.rolex--replica.us/
http://www.omega--watches.us.com/
http://www.wedding--dresses.net/
http://www.montblancpens.in.net/
http://www.insanityworkout.us.org/
http://www.nike-mercurial.org/
http://www.jimmy--choo.us/
http://www.uggboots.mex.com/
http://www.uggs--outlet.net/
http://www.nike-rosherun.net/
http://www.roshe--run.com/
http://www.nikeroshe.net/
http://www.tiffany-andco.net/
http://www.vans--shoes.com/
http://www.timberland--boots.com/
http://www.beatsheadphones.in.net/
http://www.salvatoreferragamo-shoes.com/
http://www.cheap--jerseys.us.com/
http://www.cheapnfl-jerseys.us.com/
http://www.jerseysfrom-china.us.com/
http://www.mizunowave.net/
http://www.mizunorunning-shoes.com/
http://www.basketball--shoes.net/
http://www.cheapey-eglasses.in.net/
http://www.tommyhilfiger.us.org/
http://www.tommyhilfigeroutlet.net/
http://www.ed-hardy.in.net/
http://www.levisjeans.org/
http://www.bcbgmax.in.net/
http://www.balenciaga.in.net/
http://www.marcjacobs-handbags.in.net/
http://www.chaussureslouboutin-soldes.fr/
http://www.sac-michael-kors.fr/
http://www.nikeblazer1.fr/
http://www.nikeairforce-1.fr/
http://www.michaelkorsukpurse.co.uk/
http://www.michael-kors-canada.ca/
http://www.burberryuk.co.uk/
http://www.timberlandboot.org.uk/
http://www.oakley--sunglasses.co.uk/
http://www.louboutinshoes.org.uk/
http://www.air-jordans.co.uk/
http://www.chanel-handbags.uk/
http://www.katespade-uk.org.uk/
http://www.truereligion-jeans.co.uk/
http://www.newbalance-trainers.co.uk/
http://www.conversesale.org.uk/
http://www.lululemon.me.uk/
http://www.beats-by-dre.co.uk/
http://www.macmakeupuk.co.uk/
http://www.maccosmetics-uk.co.uk/
http://www.adidas-trainersuk.co.uk/
http://www.gucciuk.uk/
http://www.fitflop.me.uk/
http://www.adidasoriginals.org.uk/
http://www.hermes.org.uk/
http://www.pandorauk.co.uk/
http://www.rolex--watches.co.uk/
http://www.omegawatches.org.uk/
http://www.michael-jordan.co.uk/
http://www.michael--kors.org.uk/
http://www.weddingdresses.me.uk/
http://www.converse.org.uk/
http://www.hollister-uk.me.uk/
http://www.tiffany-and-co.org.uk/
http://www.airmax-90.co.uk/
http://www.louis--vuitton.me.uk/
http://www.mulberryoutlet.me.uk/
http://www.north-face.org.uk/
http://www.timberland.org.uk/
http://www.rayban--sunglasses.org.uk/
http://www.asicstrainers.org.uk/
http://www.prada.me.uk/
http://www.toms-shoes.org.uk/
http://www.tommy-hilfiger.org.uk/
http://www.marcjacobs.org.uk/
http://www.uggs.org.uk/
http://www.ugg--boots.org.uk/
http://www.ugg-australia.co.uk/
http://www.insanityworkout.co.uk/
http://www.nike-free-run.co.uk/
http://www.ferragamo.org.uk/
http://www.fitflops.me.uk/
http://www.nike-mercurial-superfly.co.uk/
http://www.nikestoreuk.co.uk/
http://www.guessfactorycanada.ca/
http://www.nike--store.fr/
http://www.chaussure-nike-pas-cher.fr/
http://www.adidas--superstar.fr/
http://www.adidas-zxflux.fr/
http://www.montre-femme-homme.fr/
http://www.michaeljordan.fr/
http://www.air-max-90.fr/
http://www.bracelet-pandora-bijoux.fr/
http://www.bracelet-swarovski-bijoux.fr/
http://www.sac--chanel.fr/
http://www.ralph--lauren.fr/
http://www.new-balance-femme-574.fr/
http://www.michael--kors.fr/
http://www.lunetterayban.fr/
http://www.tommy--hilfiger.fr/
http://www.lunette-oakley.fr/
http://www.ugg-australia.fr/
http://www.nike--huarache.fr/
http://www.asics-gellyte.fr/
http://www.scarpe-nike-store.it/
http://www.scarpe-adidas-superstar.it/
http://www.pandora-bracciali.it/
http://www.orologi-rolex.it/
http://www.tiffany--gioielli.it/
http://www.michael-jordan.it/
http://www.hogan-outlet-online.it/
http://www.swarovski-gioielli.it/
http://www.coco-chanel.it/
http://www.michael-kors-borse.it/
http://www.burberry-outlet-online.it/
http://www.hollister-co.it/
http://www.mac-makeup.com/
http://www.guess-factory.net/
http://www.adidasoutlet.net/
http://www.adidasshoesoutlet.com/
http://www.tods-shoes.org/
http://www.fendi.in.net/
http://www.guccibelts.us.org/
http://www.fitflopsale.net/
http://www.kobe9shoes.net/
http://www.kobebryantshoes10.com/
http://www.lebronshoes12.net/
http://www.lebron-11.net/
http://www.lebronjames-shoes.com/
http://www.kevindurant-shoes.net/
http://www.fivefingersshoes.org/
http://www.puma-shoes.org/
http://www.salomon-shoes.net/
http://www.dolce-and-gabbana.org/
http://www.dansko-shoes.org/
http://www.stuart-weitzman.org/
http://www.chicagoblackhawksjersey.org/
http://www.pittsburghsteelersjerseys.com/
http://www.redskinsjerseys.com/
http://www.denverbroncosjerseys.net/
http://www.goldenstatewarriorsjersey.org/
http://www.dallascowboysjerseys.net/
http://www.coltsjerseys.org/
http://www.giantsjersey.net/
http://www.chiefsjersey.net/
http://www.atlantafalconsjersey.net/
http://www.lakersjersey.org/
http://www.sanfrancisco49ersjerseys.org/
http://www.spursjersey.com/
http://www.chicagobullsjersey.com/
http://www.baltimoreravensjersey.com/
http://www.clevelandcavaliersjersey.net/
http://www.chicagobears-jerseys.net/
http://www.newyorkknicksjersey.org/
http://www.seahawksjerseys.us/
http://www.newenglandpatriotsjerseys.net/
http://www.arizonacardinalsjerseys.net/
http://www.manchesterunitedjerseys.us/
http://www.greenbaypackersjerseys.us/
http://www.chelseajerseys.net/
http://www.oaklandraidersjerseys.net/
http://www.neworleanssaintsjerseys.net/
http://www.clippersjerseys.net/
http://www.barcelona-jerseys.com/
http://www.bostoncelticsjersey.com/
http://www.newyorkjetsjerseys.com/
http://www.minnesotavikingsjerseys.net/
http://www.realmadridjerseys.us/
http://www.thunderjersey.com/
http://www.miamidolphinsjerseys.net/
http://www.philadelphiaeaglesjersey.com/
http://www.miamiheatjersey.net/
http://www.bengalsjerseys.us/
http://www.detroitlionsjerseys.com/
http://www.tennesseetitansjersey.net/
http://www.houstontexansjerseys.net/
http://www.carolinapanthersjerseys.us/
http://www.jaguarsjersey.net/
http://www.buccaneersjerseys.net/
http://www.buffalobillsjerseys.com/
http://www.ramsjersey.net/
http://www.chargersjerseys.com/
http://www.nba-jerseys.net/
http://www.clevelandbrownsjerseys.net/
adityaseo#
Packers and movers in India @
http://www.5to10.in/
Packers and movers in Bangalore @
http://www.5to10.in/Bangalore/Packers-and-Movers
Packers and movers in Hyderabad @
http://www.5to10.in/Hyderabad/Packers-and-Movers
Packers and movers in Delhi @
http://www.5to10.in/delhi/Packers-and-Movers
Packers and movers in Chennai @
http://www.5to10.in/Chennai/Packers-and-Movers
Packers and movers in Kolkata @
http://www.5to10.in/Kolkata/Packers-and-Movers
Packers and movers in Pune @
http://www.5to10.in/Pune/Packers-and-Movers
Packers and movers in Mumbai @
http://www.5to10.in/Mumbai/Packers-and-Movers
Packers and movers in Ahmedabad @
http://www.5to10.in/Ahmedabad/Packers-and-Movers
Packers and movers in Jaipur @
http://www.5to10.in/Jaipur/Packers-and-Movers
Packers and movers in Udaipur @
http://www.5to10.in/Udaipur/Packers-and-Movers
Packers and movers in Indore @
http://www.5to10.in/Indore/Packers-and-Movers
Packers and movers in Bhopal @
http://www.5to10.in/Bhopal/Packers-and-Movers
Packers and movers in Kota @
http://www.5to10.in/Kota/Packers-and-Movers
Packers and movers in Ajmer @
http://www.5to10.in/Ajmer/Packers-and-Movers
Packers and movers in Vadodara @
http://www.5to10.in/vadodara/Packers-and-Movers
Packers and movers in Gurgaon @
http://www.5to10.in/Gurgaon/Packers-and-Movers

David#
Opera unit web server keep posting more on this it was very important for everyone.
https://www.couponpitara.com/offers/target/
David#
Opera unit web server keep posting more on this it was very important for everyone.
https://www.couponpitara.com/offers/target/
David#
Opera unit web server keep posting more on this it was very important for everyone.
https://www.couponpitara.com/offers/target/
David#
Opera unit web server keep posting more on this it was very important for everyone.
https://www.couponpitara.com/offers/target/
David#
Opera unit web server keep posting more on this it was very important for everyone.
https://www.couponpitara.com/offers/target/
David#
Opera unit web server keep posting more on this it was very important for everyone.
https://www.couponpitara.com/offers/target/
caspian#
Highly qualified information because it can provide useful info thanks.
http://robiherbal.com/pengobatan-lipoma-tanpa-operasi/
caspian#
Highly qualified information because it can provide useful info thanks.
http://robiherbal.com/pengobatan-lipoma-tanpa-operasi/
caspian#
Highly qualified information because it can provide useful info thanks.
http://robiherbal.com/pengobatan-lipoma-tanpa-operasi/
caspian#
Highly qualified information because it can provide useful info thanks.
http://robiherbal.com/pengobatan-lipoma-tanpa-operasi/
caspian#
Highly qualified information because it can provide useful info thanks.
http://robiherbal.com/pengobatan-lipoma-tanpa-operasi/
lionelabi21@gmail.com#
Highly qualified information because it can provide useful info thanks.
http://robiherbal.com/pengobatan-lipoma-tanpa-operasi/
dudung#
Highly qualified information because it can provide useful info thanks.
http://robiherbal.com/pengobatan-lipoma-tanpa-operasi/
dudung#
Highly qualified information because it can provide useful info thanks.
http://robiherbal.com/pengobatan-lipoma-tanpa-operasi/
dudung#
Highly qualified information because it can provide useful info thanks.
http://robiherbal.com/pengobatan-lipoma-tanpa-operasi/
dudung#
Highly qualified information because it can provide useful info thanks.
http://robiherbal.com/pengobatan-lipoma-tanpa-operasi/
dudung#
Highly qualified information because it can provide useful info thanks.
http://robiherbal.com/pengobatan-lipoma-tanpa-operasi/
dudung#
Highly qualified information because it can provide useful info thanks.
http://robiherbal.com/pengobatan-lipoma-tanpa-operasi/
dudung#
Highly qualified information because it can provide useful info thanks.
http://robiherbal.com/pengobatan-lipoma-tanpa-operasi/
dudung#
Highly qualified information because it can provide useful info thanks.
http://robiherbal.com/pengobatan-lipoma-tanpa-operasi/
dudung#
Highly qualified information because it can provide useful info thanks.
http://robiherbal.com/pengobatan-lipoma-tanpa-operasi/
dudung#
Highly qualified information because it can provide useful info thanks.
http://robiherbal.com/pengobatan-lipoma-tanpa-operasi/
Cara Mengobati Nyeri Sendi Lut#
lionelabi21@gmail.com#
By the way, Thank you for sharing with us, and we sincerely hope you will continue to update or post other articles.
http://www.ahlinyakesehatanherbal.com/cara-menghilangkan-stretch-mark-setelah-operasi/
lionelabi21@gmail.com#
By the way, Thank you for sharing with us, and we sincerely hope you will continue to update or post other articles.
http://www.ahlinyakesehatanherbal.com/cara-menghilangkan-stretch-mark-setelah-operasi/
lionelabi21@gmail.com#
By the way, Thank you for sharing with us, and we sincerely hope you will continue to update or post other articles.
http://www.ahlinyakesehatanherbal.com/cara-menghilangkan-stretch-mark-setelah-operasi/
lionelabi21@gmail.com#
By the way, Thank you for sharing with us, and we sincerely hope you will continue to update or post other articles.
http://www.ahlinyakesehatanherbal.com/cara-menghilangkan-stretch-mark-setelah-operasi/
Leave a Comment

Post Comment