Need to replace a new line in xpage - xpages

I want to replace newline in text by space or comma in serverside javascript(XPage). I tried the following code.
function setValueFunction(lines){
var oneline = "";
while(lines.length > 0) {
if(#Contains(lines, #NewLine())) {
oneline = oneline+#Left(lines, #NewLine())+";";
lines = #Right(lines, #NewLine());
}
else{
oneline=oneline+lines
lines=""
}
}
print("Final line="+oneline)
}
Input every word has only one new line .example First\nLine\nSecond\nLine\nThird\nLine
First
Line
Second
Line
Third
Line
But it is printing in the new line only.
OutPut
31-01-2014 AM 10:48:25 HTTP JVM: Final line=First
31-01-2014 AM 10:48:25 HTTP JVM: ;Line
31-01-2014 AM 10:48:25 HTTP JVM: ;Second
31-01-2014 AM 10:48:25 HTTP JVM: ;Line
31-01-2014 AM 10:48:25 HTTP JVM: ;Third
31-01-2014 AM 10:48:25 HTTP JVM: ;Line
I need output as below
First;Line;Second;Line;Third;Line
I tried replace all,#replaceSubstring,#replace all. but I could not get the solution.
Please help me get out of this problem.
Thanks in advance

This should cover and replace all kind of newlines:
#ReplaceSubstring(lines, [#NewLine(), '\r'], ";")

I would use the JavaScript replace to remove \n (new line) and \r (carraige return) wich are creating the new line.
text.replace(/[\n\r]/g, '');

Related

Python string substitution picking up junk characters in api requests

I am using Pagerduty python api client - pdpyras And below is the block of code
session = APISession(api_token)
changedate = os.popen("date -u -d '24 hours ago' +'%FT%T%.2Z'").read()
print(changedate)
changeurl = "change_events?include[]=integration&since=" + changedate
print(changeurl)
change_dump = session.rget(changeurl)
print(change_dump[0])
This should ideally get me the 1st event from change-events page of pagerduty. However, it fails due to the junk characters that gets added in the changeurl when it is passed to session.rget
See the below output
2021-03-26T09:47:53.2Z
change_events?include[]=integration&since=2021-03-26T09:47:53.2Z
Traceback (most recent call last):
File "dailyincidents.py", line 28, in <module>
change_dump = session.rget(changeurl)
File "/Users/saha/.pyenv/versions/3.7.7/lib/python3.7/site-packages/pdpyras.py", line 190, in call
return method(self, url, **kw)
File "/Users/saha/.pyenv/versions/3.7.7/lib/python3.7/site-packages/pdpyras.py", line 143, in call
r = raise_on_error(method(self, path, **pass_kw))
File "/Users/saha/.pyenv/versions/3.7.7/lib/python3.7/site-packages/pdpyras.py", line 84, in raise_on_error
), response=r
pdpyras.PDClientError: GET /change_events?include%5B%5D=integration&since=2021-03-26T09:47:53.2Z%0A: API responded with non-success status (400)
The problem here is the junk characters you see in the last line of error "include%5B%5D" and then "%0A" in the end. Because if I run the below code block directly, I am able to successfully pull the details.
change_dump = session.rget("change_events?include[]=integration&since=2021-03-26T09:47:53.2Z")
The issue occurs only when the string gets substituted, it takes these junk characters. Not sure how it gets picked and how to get around this. Any pointers?
EDIT
I was able to partly get rid of the junk characters. However, there is still one at the end, which I am not sure how to. Below is the change I did.
parameters = "include[]=integration&since=" + changedate
change_dump = session.rget("change_events", params=parameters)
Now, the error is like below
pdpyras.PDClientError: GET
/change_events?include[]=integration&since=2021-03-26T14:14:08.2Z%0A:
API responded with non-success status (400)
As you can see, now the last 3 characters in the url which is %0A: is causing the problem. Any pointers please?
The below format should work for you.
parameters = "include[]=integration&since=" + changedate
change_dump = session.rget("change_events",params=parameters.rstrip('\n').replace("\n", ""))

Unexpected token ? in JSON at position 0

I'm making a get request in node.js to a url which returns an object i'm trying to parse. However I am getting the unexpected token error. I have played with different encodings as well as converting the response body into string and then removing those tokens but nothing works. Setting encoding to null also did not solve my problem.
Below is the response body im getting:
��[{"unit":"EN15","BOX":"150027","CD":"12 - Natural Gas Leak","Levl":"1","StrName":"1000 N Madison Ave","IncNo":"2020102317","Address":"1036 N Madison Ave","CrossSt":"W 5TH ST/NECHES ST"},{"unit":"EN23","BOX":"230004","CD":"44 - Welfare Check","Levl":"1","StrName":"S Lancaster Rd / E Overton Rd","IncNo":"2020102314","Address":"S Lancaster Rd / E Overton Rd","CrossSt":""}]
Those are the headers which go with my request
headers: {'Content-Type': 'text/plain; charset=utf-8'}
Here is how I'm parsing response body
const data = JSON.parse(response.body)
Any help would be greatly appreciated!
UPDATE: had to do this with response body to make it work
const data = response.body.replace(/^\uFFFD/, '').replace(/^\uFFFD/, '').replace(/\0/g, '')
You are probably getting byte order mark (BOM) of the UTF-8 string.
The simplest workeround would be to remove it before the parsing.
const data = JSON.parse(response.body.toString('utf8').replace(/^\uFFFD/, ''));
Update: Your first 2 characters are Unicode REPLACEMENT CHARACTER. In order to remove it, use the \uFFD character.

Python: How to get HTTP header using RAW_Sockets

I'm beginner in Python and I would like to build simple port sniffer.
For this purpose I'm using code from this site, as example: Simple packege snffer using python
And I would like to unpack bites from socket to exctract http header, using function struct.unpack()
What format of string should I use in unpack HTTP header, (e.g '!BBH', "!BBHHHBBH4s4s",'!HHLLBBHHH')
the HTTP header is not fixed-length, so you'll need to parse it other way, for example:
import logging
log = logging.getLogger(__name__)
def parse_http(data):
lines = data.split(b'\r\n')
if len(lines) < 1:
log.error('Invalid http header: %s', lines)
return
request = lines[0]
header = {}
rest = []
in_header = True
for line in lines[1:]:
if line == b'':
in_header = False
continue
if in_header:
try:
key, val = line.split(b': ')
except ValueError:
log.error('Invalid header line: %s', line)
continue
header[key] = val
else:
rest.append(line)
return request, header, b'\r\n'.join(rest)
In order to detect a HTTP packet, you could check if the payload starts with POST, GET, HTTP ... etc

Azure: Output not as expected in Blob Write

I am trying to write output to a text file stored in my azure container.
Following is my woker role snippet for it :
string copyTemp="";
copyTemp += "hi" + "\n";
copyTemp += "hello" + "\n";
if (String.IsNullOrEmpty(copyTemp))
return;
using (var memoryStream = new MemoryStream())
{
memoryStream.Write(System.Text.Encoding.UTF8.GetBytes(copyTemp), 0, System.Text.Encoding.UTF8.GetBytes(copyTemp).Length);
memoryStream.Position = 0;
blockBlob.UploadFromStream(memoryStream);
}
Now, when i download and check my blob,the output doesn't feature a new line.
"hihello"
Does anyone have an idea,what's goin wrong ?
Have you tried using Environment.NewLine rather than adding "\n" to your strings?
It might just be that your "\n" is not a full new line in the place you are reading it. In windows I believe you need a "\r\n" to get a proper new line character.
You can read about the differences between new line (\n) and carriage return (\r) and which systems use which on Wikipedia but that clarifies h that windows uses a carriage return and a line feed to signify a new line.
So if you had downloaded your blob on a Mac or on Linux it probably would have displayed as you expected.
The best solution for C# is to use the static string property Environment.NewLine. You can use it for writing text content to files, to azure blobs, to console output, and you will never have to think whether it is \n or \r\n. In your case the code will be:
string copyTemp="";
copyTemp += "hi" + Environment.NewLine;
copyTemp += "hello" + Environment.NewLine;

Use groovy script output as input for another groovy script

I'll apologise in advance, I'm new to groovy. The problem I have is I have 3 groovy scripts which perform different functionality, and I need to call them from my main groovy script, using the output from script 1 as input for script 2 and script 2's output as input for script 3.
I've tried the following code:
script = new GroovyShell(binding)
script.run(new File("script1.groovy"), "--p", "$var" ) | script.run(new File("script2.groovy"), "<", "$var" )
When I run the above code the first script runs successfully but the 2nd doesn't run at all.
Script 1 takes an int as a parameter using the "--p", "$var" code. This runs successfully in the main script using: script.run(new File("script1.groovy"), "--p", "$var" ) - Script 1's output is an xml file.
When I run script.run(new File("script2.groovy"), "<", "$var" ) on its own in the main groovy script nothing happens and the system hangs.
I can run script 2 from the command line using groovy script2.groovy < input_file and it works fine.
Any help would be greatly appreciated.
You cannot pass the < as an argument to the script as redirection is handled by the Shell when you run things from the command line...
Redirecting output from Scripts into other scripts is notoriously difficult, and basically relies on you changing System.out for the duration of each script (and hoping that nothing else in the JVM prints and messes up your data)
Better to use java processes like the following:
Given these 3 scripts:
script1.groovy
// For each argument
args.each {
// Wrap it in xml and write it out
println "<woo>$it</woo>"
}
linelength.groovy
// read input
System.in.eachLine { line ->
// Write out the number of chars in each line
println line.length()
}
pretty.groovy
// For each line print out a nice report
int index = 1
System.in.eachLine { line ->
println "Line $index contains $line chars (including the <woo></woo> bit)"
index++
}
We can then write something like this to get a new groovy process to run each in turn, and pipe the outputs into each other (using the overloaded or operator on Process):
def s1 = 'groovy script1.groovy arg1 andarg2'.execute()
def s2 = 'groovy linelength.groovy'.execute()
def s3 = 'groovy pretty.groovy'.execute()
// pipe the output of process1 to process2, and the output
// of process2 to process3
s1 | s2 | s3
s3.waitForProcessOutput( System.out, System.err )
Which prints out:
Line 1 contains 15 chars (including the <woo></woo> bit)
Line 2 contains 18 chars (including the <woo></woo> bit)
//store standard I/O
PrintStream systemOut = System.out
InputStream systemIn = System.in
//Buffer for exchanging data between scripts
ByteArrayOutputStream buffer = new ByteArrayOutputStream()
PrintStream out = new PrintStream(buffer)
//Redirecting "out" of 1st stream to buffer
System.out = out
//RUN 1st script
evaluate("println 'hello'")
out.flush()
//Redirecting buffer to "in" of 2nd script
System.in = new ByteArrayInputStream(buffer.toByteArray())
//set standard "out"
System.out = systemOut
//RUN 2nd script
evaluate("println 'message from the first script: ' + new Scanner(System.in).next()")
//set standard "in"
System.in = systemIn
result is: 'message from the first script: hello'

Resources