-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Load Callable classes without reflection
- Loading branch information
Mourjo Sen
committed
Apr 25, 2020
1 parent
e48306e
commit 6670bbf
Showing
5 changed files
with
112 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package lestrange; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.lang.reflect.Method; | ||
import java.util.concurrent.Callable; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.annotation.MultipartConfig; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.eclipse.jetty.http.HttpStatus; | ||
|
||
public class CallableLoader extends HttpServlet { | ||
|
||
@Override | ||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | ||
resp.setStatus(HttpStatus.OK_200); | ||
resp.getWriter().println("Use the POST method."); | ||
} | ||
|
||
@Override | ||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | ||
try { | ||
String cl = req.getParameter("classname"); | ||
|
||
InputStream x = req.getPart("file").getInputStream(); | ||
SimpleClassLoader hc = new SimpleClassLoader(x); | ||
|
||
@SuppressWarnings("unchecked") | ||
Class<Callable<Object>> callable = (Class<Callable<Object>>) hc.loadClass(cl); | ||
|
||
Object result = callable.newInstance().call(); | ||
|
||
resp.setStatus(HttpStatus.OK_200); | ||
resp.getWriter().println(result == null ? "Result is a null" : result.toString()); | ||
|
||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
resp.setStatus(HttpStatus.INTERNAL_SERVER_ERROR_500); | ||
resp.getWriter().println("That's too bad... " + e.getMessage()); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import java.io.BufferedReader; | ||
import java.io.DataOutputStream; | ||
import java.io.InputStreamReader; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.util.concurrent.Callable; | ||
|
||
public class CallableAPI implements Callable<String> { | ||
|
||
public String call() throws Exception { | ||
URL url = new URL("http://artii.herokuapp.com/make?text=Lestrange"); | ||
HttpURLConnection con = (HttpURLConnection) url.openConnection(); | ||
|
||
con.setRequestMethod("GET"); | ||
con.setConnectTimeout(5000); | ||
con.setReadTimeout(5000); | ||
con.setDoOutput(true); | ||
|
||
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); | ||
String inputLine; | ||
StringBuffer content = new StringBuffer(); | ||
while ((inputLine = in.readLine()) != null) { | ||
content.append(inputLine+"\n"); | ||
} | ||
|
||
in.close(); | ||
con.disconnect(); | ||
return content.toString(); | ||
} | ||
} |