I have written one Java class to submit JCL to Mainframe JES using FTP. The code is able to submit the JCL but it is not retrieving the JOB log from JES SPOOL.
package com.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTPClient;
public class MVSSpool {
private FTPClient ftp = null;
private String fUserId,fPassword,fHost,fJobPerfix,replyText;
public MVSSpool(String fUserId, String fPassword,
String fHost, String fJobPerfix) {
this.fUserId = fUserId;
this.fPassword = fPassword;
this.fHost = fHost;
this.fJobPerfix = fJobPerfix;
ftp = new FTPClient();
}
public String getfUserId() {
return fUserId;
}
public void setfUserId(String fUserId) {
this.fUserId = fUserId;
}
public void setfPassword(String fPassword) {
this.fPassword = fPassword;
}
public String getfHost() {
return fHost;
}
public void setfHost(String fHost) {
this.fHost = fHost;
}
public String getfJobPerfix() {
return fJobPerfix;
}
public void setfJobPerfix(String fJobPerfix) {
this.fJobPerfix = fJobPerfix;
}
public void submitJobToSpool(String jobName) throws Exception{
if (jobName != null){
connectMVSFtp();
InputStream is = null;
BufferedReader br = null;
String currentLine = null;
try{
ftp.retrieveFileStream("'"+jobName.trim()+"'");
replyText = ftp.getReplyString();
System.out.println("some " + replyText);
String[] replies = ftp.getReplyStrings();
String remoteFileName = replies[replies.length - 1].split(" ")[2]+ ".2";
for(String rep :replies){
System.out.println("checking .. " + rep);
}
Thread.sleep(10000);
System.out.println("getting sysout of the file " + remoteFileName);
is = ftp.retrieveFileStream(remoteFileName);
replies = ftp.getReplyStrings();
for(String rep :replies){
System.out.println("checking 2 .. " + rep);
}
if (is != null){
br = new BufferedReader(new InputStreamReader(is));
while((currentLine = br.readLine()) != null){
System.out.println(currentLine);
}
}
ftp.completePendingCommand();
System.out.println("Done...");
}catch(Exception e){
e.printStackTrace();
throw new Exception("Error in submitting Job from spool");
}finally{
ftp.disconnect();
if(br != null){
br.close();
}
if(is != null){
is.close();
}
}
}
}
/**
* #throws SocketException
* #throws IOException
*/
private void connectMVSFtp() throws SocketException, IOException {
// check if the required parameters are set already
if (fUserId == null | fPassword == null | fHost == null){
}else{
ftp.connect(fHost);
replyText = ftp.getReplyString();
System.out.println(replyText);
// login using user name and password
ftp.login(fUserId, fPassword);
replyText = ftp.getReplyString();
System.out.println(replyText);
// point the FTP to JES spool
ftp.site("filetype=jes");
replyText = ftp.getReplyString();
System.out.println(replyText);
}
}
}
I am getting below messages
230 XXXXX is logged on. Working directory is "XXXXX.".
200 SITE command was accepted
some 125-Submitting job 'XXXXX.XXXX.JCLLIB(ALIAS)' FIXrecfm 80
125 When JOB07591 is done, will retrieve its output
checking .. 125-Submitting job 'XXXXX.XXXX.JCLLIB(ALIAS)' FIXrecfm 80
checking .. 125 When JOB07591 is done, will retrieve its output
getting sysout of the file JOB07591.2
checking 2 .. 550 No spool files available for JOB07591, JesPutGet aborted
Done...enter code here
Related
This question already has answers here:
How to get the Desktop path in java
(9 answers)
Closed 2 years ago.
How would I be able to automatically direct BufferReader Path to the desktop even in different computer..
'''BufferedReader in = new BufferedReader(new FileReader("C:\Users\.....\ ".txt"));''
You can use
public static void main(String[] args) throws IOException {
String path = System.getProperty("user.home") + "/Desktop/test.txt";
BufferedReader in = new BufferedReader(new FileReader(path));
String line;
while ((line = in.readLine()) != null) {
System.out.println("line = " + line);
}
in.close();
}
To get the desktop path
and read the text file from it
Here’s how I get the desktop directory:
In Windows, I look at the DESKTOP registry entry in HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders.
In Linux, I scan the plain text file $XFG_CONFIG_HOME/user-dirs.dirs for a shell-like definition of the XDG_DESKTOP_DIR variable.
On other systems, or if the above fails, fall back on the Desktop directory in the user’s home directory.
My implementation looks like this:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.util.regex.Pattern;
public class DesktopDirectoryLocator {
public static Path getDesktopDirectory()
throws IOException,
InterruptedException {
String os = System.getProperty("os.name");
if (os.contains("Windows")) {
String dir = null;
ProcessBuilder builder = new ProcessBuilder("reg.exe", "query",
"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer"
+ "\\Shell Folders", "/v", "DESKTOP");
builder.redirectError(ProcessBuilder.Redirect.INHERIT);
Process regQuery = builder.start();
try (BufferedReader outputReader = new BufferedReader(
new InputStreamReader(regQuery.getInputStream()))) {
String line;
while ((line = outputReader.readLine()) != null) {
if (dir == null) {
String replaced =
line.replaceFirst("^\\s*DESKTOP\\s+REG_SZ\\s+", "");
if (!replaced.equals(line)) {
dir = replaced.trim();
}
}
}
}
int exitCode = regQuery.waitFor();
if (exitCode != 0) {
throw new IOException(
"Command " + builder.command() + " returned " + exitCode);
}
if (dir != null) {
return Paths.get(dir);
}
} else if (os.contains("Linux")) {
// Reference:
// https://www.freedesktop.org/wiki/Software/xdg-user-dirs/
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
Path configDir;
String configHome = System.getenv("XDG_CONFIG_HOME");
if (configHome != null) {
configDir = Paths.get(configHome);
} else {
configDir = Paths.get(
System.getProperty("user.home"), ".config");
}
Path userDirsFile = configDir.resolve("user-dirs.dirs");
if (Files.isRegularFile(userDirsFile)) {
try (BufferedReader userDirs =
Files.newBufferedReader(userDirsFile)) {
String line;
while ((line = userDirs.readLine()) != null) {
if (line.trim().startsWith("XDG_DESKTOP_DIR=")) {
String value =
line.substring(line.indexOf('=') + 1);
if (value.startsWith("\"") &&
value.endsWith("\"")) {
value = value.substring(1, value.length() - 1);
}
Pattern varPattern = Pattern.compile(
"\\$(\\{[^}]+\\}|[a-zA-Z0-9_]+)");
value = varPattern.matcher(value).replaceAll(r -> {
String varName = r.group(1);
if (varName.startsWith("{")) {
varName = varName.substring(1,
varName.length() - 1);
}
String varValue = System.getenv(varName);
return (varValue != null ?
varValue : r.group());
});
return Paths.get(value);
}
}
}
}
}
return Paths.get(System.getProperty("user.home"), "Desktop");
}
public static void main(String[] args)
throws IOException,
InterruptedException {
System.out.println(getDesktopDirectory());
}
}
To read a file in that directory, you would use:
Path desktopDir = getDesktopDirectory();
Path file = desktopDir.resolve("example.txt");
try (BufferedReader reader = Files.newBufferedReader(file,
Charset.defaultCharset())) {
// ...
}
I have this issue:
I have a function that works fine but takes longer than I can handle for closing the window. So I have another function that checks with tasklist calls if the other window is finished.
It only checks it once so I need to have it check it again and again until finished.
The check function:
private int getWin() {
Process Checkprocess;
try {
String Checkcommand = "cmd /c tasklist /V /FI \"WINDOWTITLE eq Admin:*\"";
Checkprocess = Runtime.getRuntime().exec(Checkcommand);
String Checkline;
String Checkval="PID";
is = new BufferedReader(new InputStreamReader(Checkprocess.getInputStream()));
while((Checkline = is.readLine()) != null) {
retval = Checkline.contains(Checkval);
if (retval)
test = 1;
else
test = 0;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return test;
}
that should return 0 if false or 1 if there is a window with that title.
The whole source:
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.concurrent.TimeUnit;
public class RunCommand extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 5711290708294275382L;
String commands= null;
//this could be set to a specific directory, if desired
File dir = null;
BufferedReader is = null;
BufferedReader es = null;
boolean retval;
int test;
private BufferedReader outfile;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
performTask(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
performTask(request, response);
}
private int getWin() {
Process Checkprocess;
try {
String Checkcommand = "cmd /c tasklist /V /FI \"WINDOWTITLE eq Admin:*\"";
Checkprocess = Runtime.getRuntime().exec(Checkcommand);
String Checkline;
String Checkval="PID";
is = new BufferedReader(new InputStreamReader(Checkprocess.getInputStream()));
while((Checkline = is.readLine()) != null) {
retval = Checkline.contains(Checkval);
if(retval)
test = 1;
else
test = 0;
System.out.println("HERE test" + retval);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return test;
}
private void performTask (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out= response.getWriter();
try
{
String commandbuild = request.getParameter("commandinput");
String commandfilename = request.getParameter("commandfile");
final SimpleDateFormat datefor = new SimpleDateFormat("ddMMyyyy_HHmmss");
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
String timeadd = datefor.format(timestamp);
System.out.println(timeadd);
String completefilename = "C:\\tmp\\" + commandfilename +"_"+ timeadd + ".txt ";
String commandfull = "db2 -tvz "+ completefilename + commandbuild ;
commands= "cmd /c db2cwadmin.bat " + commandfull;
//System.out.println(commands);
Process process;
if (dir != null)
process = Runtime.getRuntime().exec(commands, null, dir);
else
process = Runtime.getRuntime().exec(commands);
String line;
StringBuilder data = new StringBuilder();
is = new BufferedReader(new InputStreamReader(process.getInputStream()));
data.append("Command: " + commands);
data.append("\n\n\n");
while((line = is.readLine()) != null) {
//System.out.println(line);
data.append(line);
data.append("\n");
}
es = new BufferedReader(new InputStreamReader(process.getErrorStream()));
while((line = es.readLine()) != null)
System.err.println(line);
int exitCode = process.waitFor();
if (exitCode == 0) {
System.out.println("It worked");
while(test != 1) {
getWin();
if (test == 1)
break;
}
Process EndProcess;
String Endcommands = "taskkill /F /FI \"WINDOWTITLE eq Admin:*\"";
EndProcess = Runtime.getRuntime().exec(Endcommands);
int exitCodeEnd = EndProcess.waitFor();
System.out.println(exitCodeEnd);
//TimeUnit.SECONDS.sleep(10);
data.append("Window closed!");
data.append("\n");
data.append("\n");
outfile = new BufferedReader(new FileReader(completefilename));
String outfilelines;
while( (outfilelines = outfile.readLine()) != null ) {
data.append(outfilelines);
data.append("\n");
}
request.setAttribute("data", data);
out.append(data);
RequestDispatcher dispatcher= request.getRequestDispatcher("runCommand.jsp");
dispatcher.forward(request, response);
}
else
System.out.println("Something bad happend. Exit code: " + exitCode);
} //try
catch(Exception e)
{
System.out.println("Something when wrong: " + e.getMessage());
e.printStackTrace();
} //catch
finally
{
if (is != null)
try { is.close(); } catch (IOException e) {}
if (es != null)
try { es.close(); } catch (IOException e) {}
} //finally
}}
The problem ist that even if the getWin returns true (there is a println() to check that) the while loop is not exited and the prog is not going on. Furthermore it returns true than goes on and returns a couple of false and than true again and so on
How can I have the while loop quit when test is 1?
Thanks
How can I have the while loop quit when test is 1?
You can use break
if (retval){
test = 1;
break; //exit loop
}else{
test = 0;
}
I am trying to control a GPIO pin in my program to turn on a simple relay all of the code works apart from one issue; the GPIO pin code to send the signal high isn't working, and isn't causing any errors either, i am using the pi4j libraries to control the pins on the RaspberryPi Board.
Here is my code:
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.sql.Timestamp;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.Calendar;
public class FileWatch {
static String clkID;
static String clkID2;
static String ts;
static String ts1;
static boolean done = false;
static boolean REdone = false;
static boolean finished = false;
static boolean ready;
static String host ="jdbc:mysql://localhost/dancers";
static String username ="root";
static String password ="beaker19";
public static void main(String[] args) throws IOException,
InterruptedException {
// create gpio controller
final GpioController gpio = GpioFactory.getInstance();
// provision gpio pin #01 as an output pin and turn on
final GpioPinDigitalOutput pin = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_02, "MyLED", PinState.LOW);
while (true) {
done = false;
REdone=false;
checkFile();
System.out.println("worked");
pin.high();
Thread.sleep(3000);
REcheckFile();
Thread.sleep(500);
//Thread.
if (clkID.equals(clkID2)) {
uploadTimes();
}
else {
System.out.println("Wrong matching ID's");
}
Thread.sleep(1000);
pin.low();
}
}
// Thread th = new Thread(new FileWatch());
// th.start();
// checkFile();
// REcheckFile();
// if (clkID2.equals(clkID2)){
// System.out.println("worked");
// }
// else {
// System.out.println("not worked");
// }
//
public static void check() throws InterruptedException{
checkFile();
Thread.sleep(3000);
REcheckFile();
Thread.sleep(500);
}
public String getClkId() {
return clkID;
}
public static void connection() {
// while(!finished){
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("worked");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// }
}
public static void checkFile() {
while (!done) {
try {
WatchService watcher = FileSystems.getDefault()
.newWatchService();
Path dir = Paths.get("/home/pi/rpi2ardu");
dir.register(watcher, ENTRY_MODIFY);
System.out.println("Watch Service registered for dir: "
+ dir.getFileName());
WatchKey key;
try {
key = watcher.take();
} catch (InterruptedException ex) {
return;
}
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
#SuppressWarnings("unchecked")
WatchEvent<Path> ev = (WatchEvent<Path>) event;
Path fileName = ev.context();
System.out.println(kind.name() + ": " + fileName);
if (kind == ENTRY_MODIFY
&& fileName.toString().equals("example.txt")) {
System.out.println("My source file has changed!!!");
String sCurrentLine = null;
try (BufferedReader br = new BufferedReader(
new FileReader("/home/pi/rpi2ardu/example.txt"))) {
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
clkID = sCurrentLine;
System.out.println(clkID);
java.util.Date date = new java.util.Date();
date = new Timestamp(date.getTime());
// System.out.println(new
// Timestamp(date.getTime()));
ts = date.toString();
System.out.println(ts);
}
} catch (IOException e) {
e.printStackTrace();
}
File inputFile = new File("/home/pi/rpi2ardu/example.txt"); // Your
// file
File tempFile = new File("/home/pi/rpi2ardu/temp.txt");// temp
// file
BufferedReader reader = new BufferedReader(
new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(
new FileWriter(tempFile));
String currentLine;
while ((currentLine = reader.readLine()) != null) {
currentLine = ("");
writer.write(currentLine);
}
writer.close();
reader.close();
done = true;
boolean successful = tempFile.renameTo(inputFile);
System.out.println(successful);
}
}
boolean valid = key.reset();
if (!valid) {
break;
}
}
catch (IOException ex) {
System.err.println(ex);
}
}
}
public static void REcheckFile() {
while (!REdone) {
try {
WatchService watcher = FileSystems.getDefault()
.newWatchService();
Path dir = Paths.get("/home/pi/rpi2ardu");
dir.register(watcher, ENTRY_MODIFY);
System.out.println("Watch Service registered for dir: "
+ dir.getFileName());
WatchKey key;
try {
key = watcher.take();
} catch (InterruptedException ex) {
return;
}
for (WatchEvent<?> event : key.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
#SuppressWarnings("unchecked")
WatchEvent<Path> ev = (WatchEvent<Path>) event;
Path fileName = ev.context();
System.out.println(kind.name() + ": " + fileName);
if (kind == ENTRY_MODIFY
&& fileName.toString().equals("example.txt")) {
System.out.println("My source file has changed!!!");
String sCurrentLine = null;
try (BufferedReader br = new BufferedReader(
new FileReader("/home/pi/rpi2ardu/example.txt"))) {
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
clkID2 = sCurrentLine;
System.out.println(clkID2);
java.util.Date date1 = new java.util.Date();
date1 = new Timestamp(date1.getTime());
// System.out.println(new
// Timestamp(date.getTime()));
String ts1 = date1.toString();
System.out.println(ts1);
}
} catch (IOException e) {
e.printStackTrace();
}
File inputFile = new File("/home/pi/rpi2ardu/example.txt"); // Your
// file
File tempFile = new File("/home/pi/rpi2ardu/temp.txt");// temp
// file
BufferedReader reader = new BufferedReader(
new FileReader(inputFile));
BufferedWriter writer = new BufferedWriter(
new FileWriter(tempFile));
String currentLine;
while ((currentLine = reader.readLine()) != null) {
currentLine = ("");
writer.write(currentLine);
}
writer.close();
reader.close();
REdone = true;
boolean successful = tempFile.renameTo(inputFile);
System.out.println(successful);
}
}
boolean valid = key.reset();
if (!valid) {
break;
}
}
catch (IOException ex) {
System.err.println(ex);
}
}
}
public static void uploadTimes(){
try
{
String host ="jdbc:mysql://localhost/dancers";
String username ="root";
String password ="beaker19";
connection();
Connection conn = DriverManager.getConnection(host, username, password);
System.out.println("Connected:");
String t1= ts;
String t2 =ts1;
String id =clkID;
// the mysql insert statement
String query = " insert into test (id, ts, ts1)"
+ " values (?, ?, ?)";
// create the mysql insert preparedstatement
java.sql.PreparedStatement preparedStmt = conn.prepareStatement(query);
preparedStmt.setString (1, id);
preparedStmt.setString (2, t1);
preparedStmt.setString (3, t2);
// execute the preparedstatement
preparedStmt.execute();
System.out.println("worked");
conn.close();
}
catch (Exception e)
{
System.err.println("Got an exception!");
System.err.println(e.getMessage());
}
}
}
Im not sure why it isnt sending the gpio signal high and i am not seeing any results its just ommits the code like it wasnt there.
Any help would be great thanks!
Two things:
Make sure you are using the correct GPIO pin number. The pin numbering of Pi4J is different than the usual two ways of numbering in python. Take a look at this link: http://pi4j.com/pins/model-2b-rev1.html
Relays usually require high current to switch. The current provided by a GPIO pin is not enough to trigger most relays. Hook up an LED and resistor to that GPIO pin to test whether the problem is the pin not turning high or whether it is the relay requiring more current than the pin can supply.
In my quest to create a simple Java program to extract tweets from Twitter's streaming API, I have modified this (http://cotdp.com/dl/TwitterConsumer.java) code snippet to work with the OAuth method. The result is the below code, which when executed, throws a Connection Refused Exception.
I am aware of Twitter4J however I want to create a program that relies least on other APIs.
I have done my research and it looks like the oauth.signpost library is suitable for Twitter's streaming API. I have also ensured my authentication details are correct. My Twitter Access level is 'Read-only'.
I couldn't find a simple Java example that shows how to use the streaming API without relying on e.g. Twitter4j.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
/**
* A hacky little class illustrating how to receive and store Twitter streams
* for later analysis, requires Apache Commons HTTP Client 4+. Stores the data
* in 64MB long JSON files.
*
* Usage:
*
* TwitterConsumer t = new TwitterConsumer("username", "password",
* "http://stream.twitter.com/1/statuses/sample.json", "sample");
* t.start();
*/
public class TwitterConsumer extends Thread {
//
static String STORAGE_DIR = "/tmp";
static long BYTES_PER_FILE = 64 * 1024 * 1024;
//
public long Messages = 0;
public long Bytes = 0;
public long Timestamp = 0;
private String accessToken = "";
private String accessSecret = "";
private String consumerKey = "";
private String consumerSecret = "";
private String feedUrl;
private String filePrefix;
boolean isRunning = true;
File file = null;
FileWriter fw = null;
long bytesWritten = 0;
public static void main(String[] args) {
TwitterConsumer t = new TwitterConsumer(
"XXX",
"XXX",
"XXX",
"XXX",
"http://stream.twitter.com/1/statuses/sample.json", "sample");
t.start();
}
public TwitterConsumer(String accessToken, String accessSecret, String consumerKey, String consumerSecret, String url, String prefix) {
this.accessToken = accessToken;
this.accessSecret = accessSecret;
this.consumerKey = consumerKey;
this.consumerSecret = consumerSecret;
feedUrl = url;
filePrefix = prefix;
Timestamp = System.currentTimeMillis();
}
/**
* #throws IOException
*/
private void rotateFile() throws IOException {
// Handle the existing file
if (fw != null)
fw.close();
// Create the next file
file = new File(STORAGE_DIR, filePrefix + "-"
+ System.currentTimeMillis() + ".json");
bytesWritten = 0;
fw = new FileWriter(file);
System.out.println("Writing to " + file.getAbsolutePath());
}
/**
* #see java.lang.Thread#run()
*/
public void run() {
// Open the initial file
try { rotateFile(); } catch (IOException e) { e.printStackTrace(); return; }
// Run loop
while (isRunning) {
try {
OAuthConsumer consumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret);
consumer.setTokenWithSecret(accessToken, accessSecret);
HttpGet request = new HttpGet(feedUrl);
consumer.sign(request);
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(request);
BufferedReader reader = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
while (true) {
String line = reader.readLine();
if (line == null)
break;
if (line.length() > 0) {
if (bytesWritten + line.length() + 1 > BYTES_PER_FILE)
rotateFile();
fw.write(line + "\n");
bytesWritten += line.length() + 1;
Messages++;
Bytes += line.length() + 1;
}
}
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Sleeping before reconnect...");
try { Thread.sleep(15000); } catch (Exception e) { }
}
}
}
}
I tried to simulate the code and found that the error was very simple. You should use https instead of http in the url :)
I had a look in the reference doc, and Spring seems to have pretty good support for sending mail. However, I need to login to a mail account, read the messages, and download any attachments. Is downloading mail attachments supported by the Spring mail API?
I know you can do this with the Java Mail API, but in the past I've found that very verbose and unpleasant to work with.
EDIT: I've received several replies pointing towards tutorials that describe how to send mail with attachments, but what I'm asking about is how to read attachments from received mail.
Cheers,
Don
Here's the class that I use for downloading e-mails (with attachment handling). You'll have to glance by some of the stuff it's doing (like ignore the logging classes and database writes). I've also re-named some of the packages for ease of reading.
The general idea is that all attachments are saved as individual files in the filesystem, and each e-mail is saved as a record in the database with a set of child records that point to all of the attachment file paths.
Focus on the doEMailDownload method.
/**
* Copyright (c) 2008 Steven M. Cherry
* All rights reserved.
*/
package utils.scheduled;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Timestamp;
import java.util.Properties;
import java.util.Vector;
import javax.mail.Address;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MimeBodyPart;
import glob.ActionLogicImplementation;
import glob.IOConn;
import glob.log.Log;
import logic.utils.sql.Settings;
import logic.utils.sqldo.EMail;
import logic.utils.sqldo.EMailAttach;
/**
* This will connect to our incoming e-mail server and download any e-mails
* that are found on the server. The e-mails will be stored for further processing
* in our internal database. Attachments will be written out to separate files
* and then referred to by the database entries. This is intended to be run by
* the scheduler every minute or so.
*
* #author Steven M. Cherry
*/
public class DownloadEMail implements ActionLogicImplementation {
protected String receiving_host;
protected String receiving_user;
protected String receiving_pass;
protected String receiving_protocol;
protected boolean receiving_secure;
protected String receiving_attachments;
/** This will run our logic */
public void ExecuteRequest(IOConn ioc) throws Exception {
Log.Trace("Enter");
Log.Debug("Executing DownloadEMail");
ioc.initializeResponseDocument("DownloadEMail");
// pick up our configuration from the server:
receiving_host = Settings.getValue(ioc, "server.email.receiving.host");
receiving_user = Settings.getValue(ioc, "server.email.receiving.username");
receiving_pass = Settings.getValue(ioc, "server.email.receiving.password");
receiving_protocol = Settings.getValue(ioc, "server.email.receiving.protocol");
String tmp_secure = Settings.getValue(ioc, "server.email.receiving.secure");
receiving_attachments = Settings.getValue(ioc, "server.email.receiving.attachments");
// sanity check on the parameters:
if(receiving_host == null || receiving_host.length() == 0){
ioc.SendReturn();
ioc.Close();
Log.Trace("Exit");
return; // no host defined.
}
if(receiving_user == null || receiving_user.length() == 0){
ioc.SendReturn();
ioc.Close();
Log.Trace("Exit");
return; // no user defined.
}
if(receiving_pass == null || receiving_pass.length() == 0){
ioc.SendReturn();
ioc.Close();
Log.Trace("Exit");
return; // no pass defined.
}
if(receiving_protocol == null || receiving_protocol.length() == 0){
Log.Debug("EMail receiving protocol not defined, defaulting to POP");
receiving_protocol = "POP";
}
if(tmp_secure == null ||
tmp_secure.length() == 0 ||
tmp_secure.compareToIgnoreCase("false") == 0 ||
tmp_secure.compareToIgnoreCase("no") == 0
){
receiving_secure = false;
} else {
receiving_secure = true;
}
if(receiving_attachments == null || receiving_attachments.length() == 0){
Log.Debug("EMail receiving attachments not defined, defaulting to ./email/attachments/");
receiving_attachments = "./email/attachments/";
}
// now do the real work.
doEMailDownload(ioc);
ioc.SendReturn();
ioc.Close();
Log.Trace("Exit");
}
protected void doEMailDownload(IOConn ioc) throws Exception {
// Create empty properties
Properties props = new Properties();
// Get the session
Session session = Session.getInstance(props, null);
// Get the store
Store store = session.getStore(receiving_protocol);
store.connect(receiving_host, receiving_user, receiving_pass);
// Get folder
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
try {
// Get directory listing
Message messages[] = folder.getMessages();
for (int i=0; i < messages.length; i++) {
// get the details of the message:
EMail email = new EMail();
email.fromaddr = messages[i].getFrom()[0].toString();
Address[] to = messages[i].getRecipients(Message.RecipientType.TO);
email.toaddr = "";
for(int j = 0; j < to.length; j++){
email.toaddr += to[j].toString() + "; ";
}
Address[] cc;
try {
cc = messages[i].getRecipients(Message.RecipientType.CC);
} catch (Exception e){
Log.Warn("Exception retrieving CC addrs: %s", e.getLocalizedMessage());
cc = null;
}
email.cc = "";
if(cc != null){
for(int j = 0; j < cc.length; j++){
email.cc += cc[j].toString() + "; ";
}
}
email.subject = messages[i].getSubject();
if(messages[i].getReceivedDate() != null){
email.received_when = new Timestamp(messages[i].getReceivedDate().getTime());
} else {
email.received_when = new Timestamp( (new java.util.Date()).getTime());
}
email.body = "";
Vector<EMailAttach> vema = new Vector<EMailAttach>();
Object content = messages[i].getContent();
if(content instanceof java.lang.String){
email.body = (String)content;
} else if(content instanceof Multipart){
Multipart mp = (Multipart)content;
for (int j=0; j < mp.getCount(); j++) {
Part part = mp.getBodyPart(j);
String disposition = part.getDisposition();
if (disposition == null) {
// Check if plain
MimeBodyPart mbp = (MimeBodyPart)part;
if (mbp.isMimeType("text/plain")) {
Log.Debug("Mime type is plain");
email.body += (String)mbp.getContent();
} else {
Log.Debug("Mime type is not plain");
// Special non-attachment cases here of
// image/gif, text/html, ...
EMailAttach ema = new EMailAttach();
ema.name = decodeName(part.getFileName());
File savedir = new File(receiving_attachments);
savedir.mkdirs();
File savefile = File.createTempFile("emailattach", ".atch", savedir );
ema.path = savefile.getAbsolutePath();
ema.size = part.getSize();
vema.add(ema);
ema.size = saveFile(savefile, part);
}
} else if ((disposition != null) &&
(disposition.equals(Part.ATTACHMENT) || disposition.equals(Part.INLINE) )
){
// Check if plain
MimeBodyPart mbp = (MimeBodyPart)part;
if (mbp.isMimeType("text/plain")) {
Log.Debug("Mime type is plain");
email.body += (String)mbp.getContent();
} else {
Log.Debug("Save file (%s)", part.getFileName() );
EMailAttach ema = new EMailAttach();
ema.name = decodeName(part.getFileName());
File savedir = new File(receiving_attachments);
savedir.mkdirs();
File savefile = File.createTempFile("emailattach", ".atch", savedir );
ema.path = savefile.getAbsolutePath();
ema.size = part.getSize();
vema.add(ema);
ema.size = saveFile( savefile, part);
}
}
}
}
// Insert everything into the database:
logic.utils.sql.EMail.insertEMail(ioc, email);
for(int j = 0; j < vema.size(); j++){
vema.get(j).emailid = email.id;
logic.utils.sql.EMail.insertEMailAttach(ioc, vema.get(j) );
}
// commit this message and all of it's attachments
ioc.getDBConnection().commit();
// Finally delete the message from the server.
messages[i].setFlag(Flags.Flag.DELETED, true);
}
// Close connection
folder.close(true); // true tells the mail server to expunge deleted messages.
store.close();
} catch (Exception e){
folder.close(true); // true tells the mail server to expunge deleted messages.
store.close();
throw e;
}
}
protected int saveFile(File saveFile, Part part) throws Exception {
BufferedOutputStream bos = new BufferedOutputStream( new FileOutputStream(saveFile) );
byte[] buff = new byte[2048];
InputStream is = part.getInputStream();
int ret = 0, count = 0;
while( (ret = is.read(buff)) > 0 ){
bos.write(buff, 0, ret);
count += ret;
}
bos.close();
is.close();
return count;
}
protected String decodeName( String name ) throws Exception {
if(name == null || name.length() == 0){
return "unknown";
}
String ret = java.net.URLDecoder.decode( name, "UTF-8" );
// also check for a few other things in the string:
ret = ret.replaceAll("=\\?utf-8\\?q\\?", "");
ret = ret.replaceAll("\\?=", "");
ret = ret.replaceAll("=20", " ");
return ret;
}
}
I worked Steven's example a little bit and removed the parts of the code specific to Steven. My code won't read the body of an email if it has attachments. That is fine for my case but you may want to refine it further for yours.
package utils;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MimeBodyPart;
public class IncomingMail {
public static List<Email> downloadPop3(String host, String user, String pass, String downloadDir) throws Exception {
List<Email> emails = new ArrayList<Email>();
// Create empty properties
Properties props = new Properties();
// Get the session
Session session = Session.getInstance(props, null);
// Get the store
Store store = session.getStore("pop3");
store.connect(host, user, pass);
// Get folder
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_WRITE);
try {
// Get directory listing
Message messages[] = folder.getMessages();
for (int i = 0; i < messages.length; i++) {
Email email = new Email();
// from
email.from = messages[i].getFrom()[0].toString();
// to list
Address[] toArray = messages[i] .getRecipients(Message.RecipientType.TO);
for (Address to : toArray) { email.to.add(to.toString()); }
// cc list
Address[] ccArray = null;
try {
ccArray = messages[i] .getRecipients(Message.RecipientType.CC);
} catch (Exception e) { ccArray = null; }
if (ccArray != null) {
for (Address c : ccArray) {
email.cc.add(c.toString());
}
}
// subject
email.subject = messages[i].getSubject();
// received date
if (messages[i].getReceivedDate() != null) {
email.received = messages[i].getReceivedDate();
} else {
email.received = new Date();
}
// body and attachments
email.body = "";
Object content = messages[i].getContent();
if (content instanceof java.lang.String) {
email.body = (String) content;
} else if (content instanceof Multipart) {
Multipart mp = (Multipart) content;
for (int j = 0; j < mp.getCount(); j++) {
Part part = mp.getBodyPart(j);
String disposition = part.getDisposition();
if (disposition == null) {
MimeBodyPart mbp = (MimeBodyPart) part;
if (mbp.isMimeType("text/plain")) {
// Plain
email.body += (String) mbp.getContent();
}
} else if ((disposition != null) && (disposition.equals(Part.ATTACHMENT) || disposition .equals(Part.INLINE))) {
// Check if plain
MimeBodyPart mbp = (MimeBodyPart) part;
if (mbp.isMimeType("text/plain")) {
email.body += (String) mbp.getContent();
} else {
EmailAttachment attachment = new EmailAttachment();
attachment.name = decodeName(part.getFileName());
File savedir = new File(downloadDir);
savedir.mkdirs();
// File savefile = File.createTempFile( "emailattach", ".atch", savedir);
File savefile = new File(downloadDir,attachment.name);
attachment.path = savefile.getAbsolutePath();
attachment.size = saveFile(savefile, part);
email.attachments.add(attachment);
}
}
} // end of multipart for loop
} // end messages for loop
emails.add(email);
// Finally delete the message from the server.
messages[i].setFlag(Flags.Flag.DELETED, true);
}
// Close connection
folder.close(true); // true tells the mail server to expunge deleted messages
store.close();
} catch (Exception e) {
folder.close(true); // true tells the mail server to expunge deleted
store.close();
throw e;
}
return emails;
}
private static String decodeName(String name) throws Exception {
if (name == null || name.length() == 0) {
return "unknown";
}
String ret = java.net.URLDecoder.decode(name, "UTF-8");
// also check for a few other things in the string:
ret = ret.replaceAll("=\\?utf-8\\?q\\?", "");
ret = ret.replaceAll("\\?=", "");
ret = ret.replaceAll("=20", " ");
return ret;
}
private static int saveFile(File saveFile, Part part) throws Exception {
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(saveFile));
byte[] buff = new byte[2048];
InputStream is = part.getInputStream();
int ret = 0, count = 0;
while ((ret = is.read(buff)) > 0) {
bos.write(buff, 0, ret);
count += ret;
}
bos.close();
is.close();
return count;
}
}
You also need these two helper classes
package utils;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Email {
public Date received;
public String from;
public List<String> to = new ArrayList<String>();
public List<String> cc = new ArrayList<String>();
public String subject;
public String body;
public List<EmailAttachment> attachments = new ArrayList<EmailAttachment>();
}
and
package utils;
public class EmailAttachment {
public String name;
public String path;
public int size;
}
I used this to test the above classes
package utils;
import java.util.List;
public class Test {
public static void main(String[] args) {
String host = "some host";
String user = "some user";
String pass = "some pass";
String downloadDir = "/Temp";
try {
List<Email> emails = IncomingMail.downloadPop3(host, user, pass, downloadDir);
for ( Email email : emails ) {
System.out.println(email.from);
System.out.println(email.subject);
System.out.println(email.body);
List<EmailAttachment> attachments = email.attachments;
for ( EmailAttachment attachment : attachments ) {
System.out.println(attachment.path+" "+attachment.name);
}
}
} catch (Exception e) { e.printStackTrace(); }
}
}
More info can be found at http://java.sun.com/developer/onlineTraining/JavaMail/contents.html
Here is an error:
else if ((disposition != null) && (disposition.equals(Part.ATTACHMENT)
|| disposition.equals(Part.INLINE) )
it should be:
else if ((disposition.equalsIgnoreCase(Part.ATTACHMENT)
|| disposition.equalsIgnoreCase(Part.INLINE))
Thanks #Stevenmcherry for your answer
I used Apache Commons Mail for this task:
import java.util.List;
import javax.activation.DataSource;
import javax.mail.internet.MimeMessage;
import org.apache.commons.mail.util.MimeMessageParser;
public List<DataSource> getAttachmentList(MimeMessage message) throws Exception {
msgParser = new MimeMessageParser(message);
msgParser.parse();
return msgParser.getAttachmentList();
}
From a DataSource object you can retrieve an InputStream (beside name and type) of the attachment (see API: http://docs.oracle.com/javase/6/docs/api/javax/activation/DataSource.html?is-external=true).
Thus far, I've only used the JavaMail APIs (and I've been reasonably happy with them for my purposes). If the full JavaMail package is too heavy for you, the underlying transport engine can be used without the top layers of the package. The lower you go in the SMTP, POP3 and IMAP stacks, the more you have to be prepared to do for yourself.
On the bright side, you'll also be able to ignore the parts that aren't required for your application.
import java.io.IOException;
import java.io.InputStream;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamSource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
#Controller
#RequestMapping("/sendEmail.do")
public class SendEmailAttachController {
#Autowired
private JavaMailSender mailSender;
#RequestMapping(method = RequestMethod.POST)
public String sendEmail(HttpServletRequest request,
final #RequestParam CommonsMultipartFile attachFile) {
// Input here
final String emailTo = request.getParameter("mailTo");
final String subject = request.getParameter("subject");
final String yourmailid = request.getParameter("yourmail");
final String message = request.getParameter("message");
// Logging
System.out.println("emailTo: " + emailTo);
System.out.println("subject: " + subject);
System.out.println("Your mail id is: "+yourmailid);
System.out.println("message: " + message);
System.out.println("attachFile: " + attachFile.getOriginalFilename());
mailSender.send(new MimeMessagePreparator() {
#Override
public void prepare(MimeMessage mimeMessage) throws Exception {
MimeMessageHelper messageHelper = new MimeMessageHelper(
mimeMessage, true, "UTF-8");
messageHelper.setTo(emailTo);
messageHelper.setSubject(subject);
messageHelper.setReplyTo(yourmailid);
messageHelper.setText(message);
// Attachment with mail
String attachName = attachFile.getOriginalFilename();
if (!attachFile.equals("")) {
messageHelper.addAttachment(attachName, new InputStreamSource() {
#Override
public InputStream getInputStream() throws IOException {
return attachFile.getInputStream();
}
});
}
}
});
return "Result";
}
}