I am using external interface to store cookies in client side of application. Like I have created a cookie in html and i am using those methods in flex using External Interface. I am saving a username in cookie when I re use cookie is displaying, I have deployed in server and i ran like http://localhost/[Path]/index.html.in this html I am embedded swf file and I have saved cookie in html JavaScript, now if I open this url cookie is saving if I open a new window what ever the cookies are a raised and it is loading from start. for cookies saving i am using this code in flex:`package Name{
import flash.external.ExternalInterface;
/**
* The Cookie class provides a simple way to create or access
* cookies in the embedding HTML document of the application.
*
*/
public class Cookies {
/**
* Flag if the class was properly initialized.
*/
private static var _initialized:Boolean = false;
/**
* Name of the cookie.
*/
private var _name:String;
/**
* Contents of the cookie.
*/
private var _value:String;
/**
* Flag indicating if a cookie was just created. It is <code>true</code>
* when the cookie did not exist before and <code>false</code> otherwise.
*/
private var _isNew:Boolean;
/**
* Name of the external javascript function used for getting
* cookie information.
*/
private static const GET_COOKIE:String = "cookieGetCookie";
/**
* Name of the external javascript function used for setting
* cookie information.
*/
private static const SET_COOKIE:String = "cookieSetCookie";
/**
* Javascript code to define the GET_COOKIE function.
*/
private static var FUNCTION_GET_COOKIE:String =
"function () { " +
"if (document." + GET_COOKIE + " == null) {" +
GET_COOKIE + " = function (name) { " +
"if (document.cookie) {" +
"cookies = document.cookie.split('; ');" +
"for (i = 0; i < cookies.length; i++) {" +
"param = cookies[i].split('=', 2);" +
"if (decodeURIComponent(param[0]) == name) {" +
"value = decodeURIComponent(param[1]);" +
"return value;" +
"}" +
"}" +
"}" +
"return null;" +
"};" +
"}" +
"}";
/**
* Javascript code to define the SET_COOKIE function.
*/
private static var FUNCTION_SET_COOKIE:String =
"function () { " +
"if (document." + SET_COOKIE + " == null) {" +
SET_COOKIE + " = function (name, value) { " +
"document.cookie = name + '=' + value;" +
"};" +
"}" +
"}";
/**
* Initializes the class by injecting javascript code into
* the embedding document. If the class was already initialized
* before, this method does nothing.
*/
private static function initialize():void {
if (Cookies._initialized) {
return;
}
if (!ExternalInterface.available) {
throw new Error("ExternalInterface is not available in this container. Internet Explorer ActiveX, Firefox, Mozilla 1.7.5 and greater, or other browsers that support NPRuntime are required.");
}
// Add functions to DOM if they aren't already there
ExternalInterface.call(FUNCTION_GET_COOKIE);
ExternalInterface.call(FUNCTION_SET_COOKIE);
Cookies._initialized = true;
}
/**
* Creates a new Cookie object. If a cookie with the specified
* name already exists, the existing value is used. Otherwise
* a new cookie is created as soon as a value is assigned to it.
*
* #param name The name of the cookie
*/
public function Cookies(name:String) {
Cookies.initialize();
this._name = name;
this._value = ExternalInterface.call(GET_COOKIE, name) as String;
this._isNew = this._value == null;
}
/**
* The name of the cookie.
*/
public function get name():String {
return this._name;
}
/**
* The value of the cookie. If it is a new cookie, it is not
* made persistent until a value is assigned to it.
*/
public function get value():String {
return this._value;
}
/**
* #private
*/
public function set value(value:String):void {
this._value = value;
ExternalInterface.call(SET_COOKIE, this._name, this._value);
}
/**
* The <code>isNew</code> property indicates if the cookie
* already exists or not.
*/
public function get isNew():Boolean {
return this._isNew;
}
}
}
I am using cookie like this var anotherCookie:Cookies = new Cookies("username");
anotherCookie.value=[Textinput].text;.is there any code i need to use save cookie in new window also? Please help me Thanks in Advance.
By default browsers will delete cookies at the end of the current session, e.g. when they close the browser. You can set an "expires" date to some far future date in order for it to stick around for a while. Note that if they have certain types of anti-virus programs it might delete the cookie as well.
Related
I'm Having some trouble making an POST Http Request to my firebase cloud-functions project from Unity3D game engine.
I keep getting a code 400 response, and in the firebase console I can see the following error:
Error: invalid json at parse
I don't really have a lot of knowledge about Http requests, and after quite some time of trying to find out a solution I'd like to ask for help.
Here is the client code:
public void RateLevel(string guid, int rating)
{
RateLevelRequest rlr = new RateLevelRequest (guid, rating.ToString());
string body = rlr.ToJson ();
UnityWebRequest www = UnityWebRequest.Post("myurl", body);
www.SetRequestHeader ("Content-Type", "application/json");
StartCoroutine (MakeRequest (www));
}
/* * * * * * * * * * * * * * * * * * *
* AUXILIAR CLASS FOR HTTP REQUESTS *
* * * * * * * * * * * * * * * * * * */
[System.Serializable]
public class RateLevelRequest
{
public string guid;
public string rating;
public RateLevelRequest(string _guid, string _rating)
{
guid = _guid;
rating = _rating;
}
public string ToJson()
{
string json = JsonUtility.ToJson (this);
Debug.Log ("RateLevelRequest Json: " + json);
return json;
}
}
I can guarantee that the json is well formed, with values like this.
{"guid":"fake-guid","rating":"-1"}
And here is my current deployed function in firebase-functions.
exports.rate_level = functions.https.onRequest((req, res) => {
if(req.method === 'POST')
{
console.log('guid: ' + req.body.guid);
console.log('rating: ' + req.body.rating);
console.log('invented var: ' + req.body.myinvention);
if(req.body.guid && req.body.rating &&
(req.body.rating == 1 || req.body.rating == -1))
{
res.status(200).send('You are doing a post request with the right fields and values');
}
else
{
res.status(403).send('Required Fields are not Defined!')
}
}
else
{
res.status(403).send('Wrong Request Method!');
}
});
Has anyone tried this and succeeded before?
Thanks in advance!
Ok I found the answer in an excellent blog entry.
I really don't know what was wrong, but I instead replaced my code to the one indicated in the article mentioned above, which works. I'll post it for the rest of you having issues.
public void RateLevel(string guid, int rating)
{
RateLevelRequest rlr = new RateLevelRequest (guid, rating.ToString());
string body = rlr.ToJson ();
byte[] bodyRaw = new System.Text.UTF8Encoding ().GetBytes (body);
UnityWebRequest www = new UnityWebRequest("myurl", UnityWebRequest.kHttpVerbPOST);
www.uploadHandler = (UploadHandler)new UploadHandlerRaw (bodyRaw);
www.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
www.SetRequestHeader ("Content-Type", "application/json");
StartCoroutine (MakeRequest (www));
}
Best!
So I followed Will Abson's guide and source code for extending custom media viewers in Alfresco.
I have a couple of issues though.
I'm already using 4.2+ Alfresco, so no need to use head.ftl as deprecated, I'm using a second extensibility module to add my own configuration automatically, BUT:
how can I access the jsNode in my web-preview.get.js? Or better, is there a way to access properties values and aspects of a node which is being displayed?
I know about both server and client side var jsNode = new Alfresco.util.Node(model.widgets[i].options.nodeRef) and var jsNode = AlfrescoUtil.getNodeDetails(model.widgets[i].options.nodeRef);
which were mentioned in another question here, but it seems like except of default values like, mimeType, size, nodeRef, I'm not able to use those to get data from the file.
These are my changes:
web-preview.get.js in -config folder of my custom media-viewer
//<import resource="classpath:/alfresco/templates/org/alfresco/import/alfresco-util.js">
if (model.widgets)
{
for (var i = 0; i < model.widgets.length; i++)
{
var at = "test";
//var jsNode = AlfrescoUtil.getNodeDetails(model.widgets[i].options.nodeRef);
//var author = jsNode.properties["cm:author"];
var widget = model.widgets[i];
if (widget.id == "WebPreview")
{
var conditions = [];
// Insert new pluginCondition(s) at start of the chain
conditions.push({
attributes: {
mimeType: "application/pdf"
},
plugins: [{
name: "PDF",
attributes: {
}
}]
});
var oldConditions = eval("(" + widget.options.pluginConditions + ")");
// Add the other conditions back in
for (var j = 0; j < oldConditions.length; j++)
{
conditions.push(oldConditions[j]);
}
// Override the original conditions
model.pluginConditions = jsonUtils.toJSONString(conditions);
widget.options.pluginConditions = model.pluginConditions;
}
}
}
PDF.js
/**
* Copyright (C) 2014 Will Abson
*/
/**
* This is the "PDF" plug-in used to display documents directly in the web browser.
*
* Supports the "application/pdf" mime types.
*
* #namespace Alfresco.WebPreview.prototype.Plugins
* #class Alfresco.WebPreview.prototype.Plugins.PDF
*/
(function()
{
/**
* PDF plug-in constructor
*
* #param wp {Alfresco.WebPreview} The Alfresco.WebPreview instance that decides which plugin to use
* #param attributes {Object} Arbitrary attributes brought in from the <plugin> element
*/
Alfresco.WebPreview.prototype.Plugins.PDF = function(wp, attributes)
{
this.wp = wp;
this.attributes = YAHOO.lang.merge(Alfresco.util.deepCopy(this.attributes), attributes);
//this.wp.options.nodeRef = this.wp.nodeRef;
return this;
};
Alfresco.WebPreview.prototype.Plugins.PDF.prototype =
{
/**
* Attributes
*/
attributes:
{
/**
* Maximum size to display given in bytes if the node's content is used.
* If the node content is larger than this value the image won't be displayed.
* Note! This doesn't apply if src is set to a thumbnail.
*
* #property srcMaxSize
* #type String
* #default "2000000"
*/
srcMaxSize: "2000000"
},
/**
* Tests if the plugin can be used in the users browser.
*
* #method report
* #return {String} Returns nothing if the plugin may be used, otherwise returns a message containing the reason
* it cant be used as a string.
* #public
*/
report: function PDF_report()
{
// TODO: Detect whether Adobe PDF plugin is installed, or if navigator is Chrome
// See https://stackoverflow.com/questions/185952/how-do-i-detect-the-adobe-acrobat-version-installed-in-firefox-via-javascript
var srcMaxSize = this.attributes.srcMaxSize;
if (!this.attributes.src && srcMaxSize.match(/^\d+$/) && this.wp.options.size > parseInt(srcMaxSize))
{
return this.wp.msg("pdf.tooLargeFile", this.wp.options.name, Alfresco.util.formatFileSize(this.wp.options.size), Alfresco.util.formatFileSize(this.attributes.srcMaxSize));
}
},
/**
* Display the node.
*
* #method display
* #public
*/
display: function PDF_display()
{
// TODO: Support rendering the content of the thumbnail specified
var src = this.wp.getContentUrl();
var test = this.attributes.author;
//var test = this.wp.options.nodeRef;
//var jsNode = new Alfresco.util.Node(test);
//var jsNode = AlfrescoUtil.getNodeDetails(this.wp.options.nodeRef);
//var author = jsNode.properties["cm:author"];
//var test = this.wp.options.author;
//var test1 = this.wp.options.mimeType;
//var test = this.attributes.author.replace(/[^\w_\-\. ]/g, "");
//.replace(/[^\w_\-\. ]/g, "");
return '<iframe name="' + test + '" src="' + src + '"></iframe>';
}
};
})();
As you can see by commented sections I tried different methods to access node properties/values, even simple strings, but I'm missing something for sure.
Thanks.
If you take a look at the source code you'll see that the helper method is nothing else than doing a remote call to var url = '/slingshot/doclib2/node/' + nodeRef.replace('://', '/');
So take a look at what that Repository WebScript is returning en match it to the properties you need.
I normally don't use this one and I know for sure the /api/metadata returns all the properties.
Hi I need to save email-id in my login form through the cookies. if I use shared object I am able to save but my requirement is need to save in cookies. How can I save? I got sample code from net. Attaching that code `package com {
import flash.external.ExternalInterface;
/**
* The Cookie class provides a simple way to create or access
* cookies in the embedding HTML document of the application.
*
*/
public class Cookie {
/**
* Flag if the class was properly initialized.
*/
private static var _initialized:Boolean = false;
/**
* Name of the cookie.
*/
private var _name:String;
/**
* Contents of the cookie.
*/
private var _value:String;
/**
* Flag indicating if a cookie was just created. It is <code>true</code>
* when the cookie did not exist before and <code>false</code> otherwise.
*/
private var _isNew:Boolean;
/**
* Name of the external javascript function used for getting
* cookie information.
*/
private static const GET_COOKIE:String = "cookieGetCookie";
/**
* Name of the external javascript function used for setting
* cookie information.
*/
private static const SET_COOKIE:String = "cookieSetCookie";
/**
* Javascript code to define the GET_COOKIE function.
*/
private static var FUNCTION_GET_COOKIE:String =
"function () { " +
"if (document." + GET_COOKIE + " == null) {" +
GET_COOKIE + " = function (name) { " +
"if (document.cookie) {" +
"cookies = document.cookie.split('; ');" +
"for (i = 0; i < cookies.length; i++) {" +
"param = cookies[i].split('=', 2);" +
"if (decodeURIComponent(param[0]) == name) {" +
"value = decodeURIComponent(param[1]);" +
"return value;" +
"}" +
"}" +
"}" +
"return null;" +
"};" +
"}" +
"}";
/**
* Javascript code to define the SET_COOKIE function.
*/
private static var FUNCTION_SET_COOKIE:String =
"function () { " +
"if (document." + SET_COOKIE + " == null) {" +
SET_COOKIE + " = function (name, value) { " +
"document.cookie = name + '=' + value;" +
"};" +
"}" +
"}";
/**
* Initializes the class by injecting javascript code into
* the embedding document. If the class was already initialized
* before, this method does nothing.
*/
private static function initialize():void {
if (Cookie._initialized) {
return;
}
if (!ExternalInterface.available) {
throw new Error("ExternalInterface is not available in this container. Internet Explorer ActiveX, Firefox, Mozilla 1.7.5 and greater, or other browsers that support NPRuntime are required.");
}
// Add functions to DOM if they aren't already there
ExternalInterface.call(FUNCTION_GET_COOKIE);
ExternalInterface.call(FUNCTION_SET_COOKIE);
Cookie._initialized = true;
}
/**
* Creates a new Cookie object. If a cookie with the specified
* name already exists, the existing value is used. Otherwise
* a new cookie is created as soon as a value is assigned to it.
*
* #param name The name of the cookie
*/
public function Cookie(name:String) {
Cookie.initialize();
this._name = name;
this._value = ExternalInterface.call(GET_COOKIE, name) as String;
this._isNew = this._value == null;
}
/**
* The name of the cookie.
*/
public function get name():String {
return this._name;
}
/**
* The value of the cookie. If it is a new cookie, it is not
* made persistent until a value is assigned to it.
*/
public function get value():String {
return this._value;
}
/**
* #private
*/
public function set value(value:String):void {
this._value = value;
ExternalInterface.call(SET_COOKIE, this._name, this._value);
}
/**
* The <code>isNew</code> property indicates if the cookie
* already exists or not.
*/
public function get isNew():Boolean {
return this._isNew;
}
}
}
`How can I use this? I need to integrate this with jsp by default swf file is embedded in jsp.
How can I save only username (email-id)? If user reenters again it should show in dropdown. how can I pass text input text into the cookie? Please help me in these Thanks in advance.
The posted code uses the ExternalInterface class to access the browsers cookies through JavaScript. I guess your question is about how to use that class. Here you go:
// Initialize the object:
var cookie:Cookie = new Cookie("NAME OF THE COOKIE");
// Retrieve and trace the value of the cookie:
trace(cookie.value());
// Set new value for the cookie:
cookie.value = "NEW VALUE";
I cannot answer the remaining questions about the drop down. You'll need to be more specific what your actual question is.
I'm creating a facelets template for all my company's internal applications. Its appearance is based on the skin which the user selects (like gmail themes).
It makes sense to store the user's preferred skin in a cookie.
My "user-preferences" WAR can see this cookie. However, my other applications are unable to find the cookie. They are on the same domain/subdomain as the user-preferences WAR.
Is there some reason for this?
Here is my bean which is used to create/find the preferred skin. This same file is used in all the projects:
// BackingBeanBase is just a class with convenience methods. Doesn't
// really affect anything here.
public class UserSkinBean extends BackingBeanBase {
private final static String SKIN_COOKIE_NAME = "preferredSkin";
private final static String DEFAULT_SKIN_NAME = "classic";
/**
* Get the name of the user's preferred skin. If this value wasn't set previously,
* it will return a default value.
*
* #return
*/
public String getSkinName() {
Cookie skinNameCookie = findSkinCookie();
if (skinNameCookie == null) {
skinNameCookie = initializeSkinNameCookie(DEFAULT_SKIN_NAME);
addCookie(skinNameCookie);
}
return skinNameCookie.getValue();
}
/**
* Set the skin to the given name. Must be the name of a valid richFaces skin.
*
* #param skinName
*/
public void setSkinName(String skinName) {
if (skinName == null) {
skinName = DEFAULT_SKIN_NAME;
}
Cookie skinNameCookie = findSkinCookie();
if (skinNameCookie == null) {
skinNameCookie = initializeSkinNameCookie(skinName);
}
else {
skinNameCookie.setValue(skinName);
}
addCookie(skinNameCookie);
}
private void addCookie(Cookie skinNameCookie) {
((HttpServletResponse)getFacesContext().getExternalContext().getResponse()).addCookie(skinNameCookie);
}
private Cookie initializeSkinNameCookie(String skinName) {
Cookie ret = new Cookie(SKIN_COOKIE_NAME, skinName);
ret.setComment("The purpose of this cookie is to hold the name of the user's preferred richFaces skin.");
//set the max age to one year.
ret.setMaxAge(60 * 60 * 24 * 365);
ret.setPath("/");
return ret;
}
private Cookie findSkinCookie() {
Cookie[] cookies = ((HttpServletRequest)getFacesContext().getExternalContext().getRequest()).getCookies();
Cookie ret = null;
for (Cookie cookie : cookies) {
if (cookie.getName().equals(SKIN_COOKIE_NAME)) {
ret = cookie;
break;
}
}
return ret;
}
}
Can anyone see what I'm doing wrong?
Update: I've narrowed it down a bit...it works fine in FF, but IE still doesn't like it (of course).
Thanks,
Zack
I think you need to assign domain/subdomain to the cookies.
Like, (Note that the domain should start with a dot)
ret.setDomain(".test.com");
ret.setDomain(".test.co.uk");
http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Cookies.html
I found a solution.
I just used javascript on the client-side to create the cookies.
This worked fine.
In PHP there is a setcookie function based to store it. In Flash is it possible ?.
If it is possible then how?
I want to store value.
First off: PHP runs on the server and therefore can send the necessary HTTP header to set a cookie, Flash runs in the client's browser and thus can not do the same.
However, there is a way to access and store cookies from flash/flex by using the flash.external.ExternalInterface and calling JavaScript functions to get and set cookies.
I've developed a class for doing this very easily:
package de.slashslash.util {
import flash.external.ExternalInterface;
/**
* The Cookie class provides a simple way to create or access
* cookies in the embedding HTML document of the application.
*
*/
public class Cookie {
/**
* Flag if the class was properly initialized.
*/
private static var _initialized:Boolean = false;
/**
* Name of the cookie.
*/
private var _name:String;
/**
* Contents of the cookie.
*/
private var _value:String;
/**
* Flag indicating if a cookie was just created. It is <code>true</code>
* when the cookie did not exist before and <code>false</code> otherwise.
*/
private var _isNew:Boolean;
/**
* Name of the external javascript function used for getting
* cookie information.
*/
private static const GET_COOKIE:String = "cookieGetCookie";
/**
* Name of the external javascript function used for setting
* cookie information.
*/
private static const SET_COOKIE:String = "cookieSetCookie";
/**
* Javascript code to define the GET_COOKIE function.
*/
private static var FUNCTION_GET_COOKIE:String =
"function () { " +
"if (document." + GET_COOKIE + " == null) {" +
GET_COOKIE + " = function (name) { " +
"if (document.cookie) {" +
"cookies = document.cookie.split('; ');" +
"for (i = 0; i < cookies.length; i++) {" +
"param = cookies[i].split('=', 2);" +
"if (decodeURIComponent(param[0]) == name) {" +
"value = decodeURIComponent(param[1]);" +
"return value;" +
"}" +
"}" +
"}" +
"return null;" +
"};" +
"}" +
"}";
/**
* Javascript code to define the SET_COOKIE function.
*/
private static var FUNCTION_SET_COOKIE:String =
"function () { " +
"if (document." + SET_COOKIE + " == null) {" +
SET_COOKIE + " = function (name, value) { " +
"document.cookie = name + '=' + value;" +
"};" +
"}" +
"}";
/**
* Initializes the class by injecting javascript code into
* the embedding document. If the class was already initialized
* before, this method does nothing.
*/
private static function initialize():void {
if (Cookie._initialized) {
return;
}
if (!ExternalInterface.available) {
throw new Error("ExternalInterface is not available in this container. Internet Explorer ActiveX, Firefox, Mozilla 1.7.5 and greater, or other browsers that support NPRuntime are required.");
}
// Add functions to DOM if they aren't already there
ExternalInterface.call(FUNCTION_GET_COOKIE);
ExternalInterface.call(FUNCTION_SET_COOKIE);
Cookie._initialized = true;
}
/**
* Creates a new Cookie object. If a cookie with the specified
* name already exists, the existing value is used. Otherwise
* a new cookie is created as soon as a value is assigned to it.
*
* #param name The name of the cookie
*/
public function Cookie(name:String) {
Cookie.initialize();
this._name = name;
this._value = ExternalInterface.call(GET_COOKIE, name) as String;
this._isNew = this._value == null;
}
/**
* The name of the cookie.
*/
public function get name():String {
return this._name;
}
/**
* The value of the cookie. If it is a new cookie, it is not
* made persistent until a value is assigned to it.
*/
public function get value():String {
return this._value;
}
/**
* #private
*/
public function set value(value:String):void {
this._value = value;
ExternalInterface.call(SET_COOKIE, this._name, this._value);
}
/**
* The <code>isNew</code> property indicates if the cookie
* already exists or not.
*/
public function get isNew():Boolean {
return this._isNew;
}
}
}
Yes. Flash applications can store up to 100kb of data (by default) on a user's computer. This is stored in Flash cookies (Separate from browser cookies). Users can adjust how much applications can store by right clicking on a flash application and going to settings.
Here is the SharedObject API for AS3:
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/net/SharedObject.html
You can get more info by googling "Flash SharedObject"
Make sure to be prepared to handle cases where a user does not let you put data into SharedObjects. This is becoming a popular trend among tech-savy users that are concerned about their privacy.
Good luck!
for an actual code example:
import flash.net.SharedObject;
// get/create the shared object with a unique name.
// If the shared object exists this grab it, if not
// then it will create a new one
var so: SharedObject = SharedObject.getLocal("UniqueName");
// the shared object has a propery named data, it's
// an object on which you can create, read, or modify
// properties (you can't set the data property itself!)
// you can check to see if it already has something set
// using hasOwnProperty, so we'll check if it has a var
// use it if it does, or set it to a default if it doesn't
if (so.data.hasOwnProperty("theProp"))
{
trace("already has data! It reads: " + so.data.theProp);
}
else
{
so.data.theProp = "default value";
so.flush(); // flush saves the data
trace("It didn't have a value, so we set it.");
}
Paste this in flash, publish it twice and see how it stored the data :)