Reading cookie in Velocity template - templates

Is it possible to read a cookie value in portal_normal.vm in Liferay 6.2?

You can use the cookie access method from the request:
#set($previousWeb = "...")
#foreach($cookie in $request.getCookies())
#if ($cookie.getName() eq "web")
#set($previousWeb = $cookie.getValue())
#end
#end

you can also do it using javaScript in portal_normal.vm
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";
}
Goto javaScript cookie

Related

wso2 carbon how to fix Cannot read property 'getTenantDomain' of undefined at getGadgetLocation?

I'm currently working in wso2 carbon. I have developed one gadget and successfully integrated as well but gadget-util.js file showing an error of
Uncaught TypeError: Cannot read property 'getTenantDomain' of undefined
at getGadgetLocation
my code is gadget-util.js
var getGadgetLocation = function (callback) {
var gadgetLocation = "/portal/store/carbon.super/fs/gadget/circle_d3";
var PATH_SEPERATOR = "/";
if (gadgetLocation.search("store") != -1)
{
wso2.gadgets.identity.getTenantDomain(function (tenantDomain) {
var gadgetPath = gadgetLocation.split(PATH_SEPERATOR);
var modifiedPath = '';
for (var i = 1; i < gadgetPath.length; i++) {
if (i === 3) {
modifiedPath = modifiedPath.concat(PATH_SEPERATOR, tenantDomain);
} else {
modifiedPath = modifiedPath.concat(PATH_SEPERATOR, gadgetPath[i])
}
}
callback(modifiedPath);
});
} else {
callback(gadgetLocation)
}
callback(gadgetLocation);
}
please help me
You need to require identity feature[1] in gadget.xml
<Require feature="wso2-gadgets-identity" />
e.g https://github.com/wso2/product-das/blob/master/modules/distribution/src/repository/conf/template-manager/gadget-templates/numberchart/gadget.xml#L5
[1] http://mail.wso2.org/mailarchive/dev/2016-August/066568.html

Extract info from email body with Google Scripts

I am trying to extract specific info from email in one of my labels in Gmail. I've hacked (my scripting knowledge is very limited) the following together based on a script from https://gist.github.com/Ferrari/9678772. I am getting an error though: "Cannot convert Array to Gmail Thread - Line 5"
Any help will be greatly appreciated.
/* Based on https://gist.github.com/Ferrari/9678772 */
function parseEmailMessages(start) {
/* var threads = GmailApp.getInboxThreads(start, 100); */
var threads = GmailApp.getMessagesForThread(GmailApp.search("label:labelname"));
var sheet = SpreadsheetApp.getActiveSheet();
var tmp, result = [];
for (var i = 0; i < threads.length; i++) {
// Get the first email message of a threads
var message = threads[i].getMessages()[0];
// Get the plain text body of the email message
// You may also use getRawContent() for parsing HTML
var content = messages[0].getPlainBody();
// Implement Parsing rules using regular expressions
if (content) {
tmp = content.match(/Name and Surname:\n([A-Za-z0-9\s]+)(\r?\n)/);
var username = (tmp && tmp[1]) ? tmp[1].trim() : 'No username';
tmp = content.match(/Phone Number:\n([\s\S]+)/);
var phone = (tmp && tmp[1]) ? tmp[1] : 'No phone';
tmp = content.match(/Email Address:\n([A-Za-z0-9#.]+)/);
var email = (tmp && tmp[1]) ? tmp[1].trim() : 'No email';
tmp = content.match(/Prefered contact office:\n([\s\S]+)/);
var comment = (tmp && tmp[1]) ? tmp[1] : 'No office';
sheet.appendRow([username, phone, email, comment]);
}
}
};
Thanks folks.. This did the trick:
// Adapted from https://gist.github.com/Ferrari/9678772
function processInboxToSheet() {
// Have to get data separate to avoid google app script limit!
var start = 0;
var label = GmailApp.getUserLabelByName("yourLabelName");
var threads = label.getThreads();
var sheet = SpreadsheetApp.getActiveSheet();
var result = [];
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
var content = messages[0].getPlainBody();
// implement your own parsing rule inside
if (content) {
var tmp;
tmp = content.match(/Name and Surname:\n([A-Za-z0-9\s]+)(\r?\n)/);
var username = (tmp && tmp[1]) ? tmp[1].trim() : 'No username';
tmp = content.match(/Phone Number:\n([\s\S]+)/);
var phone = (tmp && tmp[1]) ? tmp[1] : 'No phone';
tmp = content.match(/Email Address:\n([A-Za-z0-9#.]+)/);
var email = (tmp && tmp[1]) ? tmp[1].trim() : 'No email';
tmp = content.match(/Prefered contact office:\n([\s\S]+)/);
var comment = (tmp && tmp[1]) ? tmp[1] : 'No office';
sheet.appendRow([username, phone, email, comment]);
Utilities.sleep(500);
}
}
};
var threads = GmailApp.getMessagesForThread(GmailApp.search("label:labelname"));
should include an array index since GmailApp.search returns an array, even if only one item is found.
var threads = GmailApp.getMessagesForThread(GmailApp.search("label:labelname")[0]);
would work but is wordy.
var thread_list = GmailApp.search("label:labelname");
var threads = GmailApp.getMessagesForThread(thread_list[0]);
IMO, the above is clearer in meaning.

Amazon Web service - signature

I've been receiving an error from Amazon web service - InvalidParameterValue
Either Action or Operation query parameter must be present.
I believe it is most likely due to the signature being incorrect as the XML document and Header matches that of a test I did in their scratchpad.
Does anything stand out as being incorrect?
Thanks,
Clare
private static string ConstructCanonicalQueryString(SortedDictionary<string, string> sortedParameters)
{
var builder = new StringBuilder();
if (sortedParameters.Count == 0)
{
builder.Append(string.Empty);
return builder.ToString();
}
foreach (var kvp in sortedParameters)
{
builder.Append(PercentEncodeRfc3986(kvp.Key));
builder.Append("=");
builder.Append(PercentEncodeRfc3986(kvp.Value));
builder.Append("&");
}
var canonicalString = builder.ToString();
return canonicalString.Substring(0, canonicalString.Length - 1);
}
private static string PercentEncodeRfc3986(string value)
{
value = HttpUtility.UrlEncode(string.IsNullOrEmpty(value) ? string.Empty : value, Encoding.UTF8);
if (string.IsNullOrEmpty(value))
{
return string.Empty;
}
value = value.Replace("'", "%27")
.Replace("(", "%28")
.Replace(")", "%29")
.Replace("*", "%2A")
.Replace("!", "%21")
.Replace("%7e", "~")
.Replace("+", "%20")
.Replace(":", "%3A");
var sbuilder = new StringBuilder(value);
for (var i = 0; i < sbuilder.Length; i++)
{
if (sbuilder[i] != '%')
{
continue;
}
if (!char.IsLetter(sbuilder[i + 1]) && !char.IsLetter(sbuilder[i + 2]))
{
continue;
}
sbuilder[i + 1] = char.ToUpper(sbuilder[i + 1]);
sbuilder[i + 2] = char.ToUpper(sbuilder[i + 2]);
}
return sbuilder.ToString();
}
public string SignRequest(Dictionary<string, string> parametersUrl, Dictionary<string, string>
parametersSignture)
{
var secret = Encoding.UTF8.GetBytes(parametersSignture["Secret"]);
var signer = new HMACSHA256(secret);
var pc = new ParamComparer();
var sortedParameters = new SortedDictionary<string, string>(parametersUrl, pc);
var orderedParameters = ConstructCanonicalQueryString(sortedParameters);
var builder = new StringBuilder();
builder.Append(parametersSignture["RequestMethod"])
.Append(" \n")
.Append(parametersSignture["EndPoint"])
.Append("\n")
.Append("/\n")
.Append(orderedParameters);
var stringToSign = builder.ToString();
var toSign = Encoding.UTF8.GetBytes(stringToSign);
var sigBytes = signer.ComputeHash(toSign);
var signature = Convert.ToBase64String(sigBytes);
return signature.Replace("=", "%3D").Replace("/", "%2F").Replace("+", "%2B");
}
public class ParamComparer : IComparer<string>
{
public int Compare(string p1, string p2)
{
return string.CompareOrdinal(p1, p2);
}
}
The issue was that the Action wasn't included correctly into the Request

Get URL parameter - sharepoint

I am trying to extract an ID from my URL and paste it in the sharepoint List into the ID text box, I have tried it like that:
<script type=”text/javascript”>
function GetQueryStringParams(sParam)
{
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++)
{
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam)
{
return sParameterName[1];
}
}
}​
document.forms['aspnetForm'].ctl00_m_g_d9f5e4f9_7e05_4a4d_88b7_a6b1dcdda667_ctl00_ctl01_ctl00_ctl00_ctl00_ctl04_ctl00_ctl00_TextField.value=GetQueryStringParams(‘ID’);
</script>
however it does not work, my URL:
http://Lists/Systems%20Survey/NewForm.aspx?Source=http://Lists/Systems%2520Survey/overview.aspx&ID=14-01234
I have put it as a web part xml on my web.
Do you have any idea what I did wrong? :)
thanks for assistance
Use the function below as getQueryVariable('source')
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split('&');
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=');
if (decodeURIComponent(pair[0]) == variable) {
return decodeURIComponent(pair[1]);
}
}
console.log('Query variable %s not found', variable);
}

how to add cookies when the user login and how to delete cookies when user logout

I am already using this code add and delete in the cookies
deleted code:
String profileScore=null;
Cookie cookiesScore =new Cookie("profileScore","");
cookiesScore.setValue("");
cookiesScore.setMaxAge(0);
response.addCookie(cookiesScore);
but its not deleted properly please help me give any example
Try this if using javascript
create a cookie:
call a this method at login:
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
delete a cookie:
call a this method at logout:
function deleteCookies() {
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
var eqPos = cookie.indexOf("=");
var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}
}