Exception in thread "main" javax.mail.MessagingException: Folder is not using SocketChannels - jakarta-mail

Using idle manager this exception appears:
Exception in thread "main" javax.mail.MessagingException: Folder is not using SocketChannels
How can i solve this problem ?
ExecutorService es = Executors.newCachedThreadPool();
final IdleManager idleManager = new IdleManager(session, es);
folder.open(Folder.READ_WRITE);
folder.addMessageCountListener(new MessageCountAdapter() {
public void messagesAdded(MessageCountEvent ev) {
Folder folder = (Folder)ev.getSource();
Message[] msgs = ev.getMessages();
System.out.println("Folder: " + folder +
" got " + msgs.length + " new messages");
try {
// process new messages
System.out.println("***********************process new messages : ");
idleManager.watch(folder); // keep watching for new messages
} catch (IOException | MessagingException ex) {
Logger.getLogger(mail2.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
idleManager.watch(folder);

As the IdleManager javadocs say:
.. set the mail.imap.usesocketchannels property in the Session

public void watch_mail(Folder folder) throws IOException, MessagingException, InterruptedException {
Thread t = new Thread(new Runnable() {
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
System.out.println("hi, interrupted = " + Thread.currentThread().isInterrupted());
folder.getMessageCount();
idleManager.watch(folder);
Thread.currentThread().sleep(30000);
} catch (MessagingException | InterruptedException | IOException ex) {
Logger.getLogger(listening_test.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
folder.addMessageCountListener(new MessageCountAdapter() {
public void messagesAdded(MessageCountEvent ev) {
Folder folder = (Folder) ev.getSource();
Message[] msgs = ev.getMessages();
System.out.println("Folder: " + folder
+ " got " + msgs.length + " new messages");
for (int i = 0; i < msgs.length; i++) {
try {
// Get dates date 1 (day_month_year) and date 2(hours:min:second)
String dates[] = getDates(msgs[i]);
System.out.println("*************date 1: " + dates[0]);
System.out.println("*************date 2: " + dates[1]);
idleManager.watch(folder);
} catch (ParseException | MessagingException | IOException ex) {
Logger.getLogger(listening_test.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
t.start();
}
hi, interrupted = false
DEBUG IMAP: IdleManager watching INBOX
A4 IDLE
+ idling
DEBUG IMAP: IdleManager selected 0 channels
DEBUG IMAP: IdleManager adding INBOX to selector
DEBUG IMAP: IdleManager waiting...
hi, interrupted = false
DEBUG IMAP: IdleManager selected 0 channels

Related

Extent report version 4 - Create two extent reports instead of one html report for all extectued testcases

I am using extent reportversion 4 and want one .html report after executing of all the testcases but it creates two html reports for executing the 3 methods in testclass
In the testclass, I have writteh code like that #beforemethod will execute before executing each testcase, followed by executing the testcase & in #aftermethod it will flush the repot to generate Html report and afterthat using #afterclass annotations to quit the driver**
**Testclass:**
public class HomePageTest extends BaseClass {
HomePage homePage;
public HomePageTest() {
super();
}
#BeforeMethod
#Parameters({ "platformName", "url", "udid" })
public void setUpHomePageClass(String platformName, String url, String udid) throws Exception {
try {
BaseClass baseClass = new BaseClass();
baseClass.initialize_driver(platformName, url, udid);
homePage = new HomePage(driver);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
#BeforeMethod
#Parameters({ "platformName", "url", "udid" })
public void setUpHomePageClass(String platformName, String url, String udid) throws Exception {
try {
BaseClass baseClass = new BaseClass();
baseClass.initialize_driver(platformName, url, udid);
homePage = new HomePage(driver);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Test(priority = 1, description = "Verify element i.e Top50 Txt on homepage test")
#Severity(SeverityLevel.NORMAL)
#Description("TestCase Description: Verify element i.e Top50 Txt on homepage")
public void verifyeElementsOnHomePageTest() throws Exception {
log.info("***Executing verifyElementsOnHomeScreenTest***");
logger = extent.createTest("Verify the elements on HomePage after redirecting to the splash screen");
log.info("wait for continue_button to be clickable");
TestUtil.waitForElementToBeClickable(By.id("continue_button"));
homePage.clickContinueBtnAfterSplashScreen();
log.info("Clicked on continue_button");
log.info("waitForUserNameToBeClickable - username");
boolean flag = homePage.validateTop50Txt();
Assert.assertTrue(flag);
log.info("Top50Txt isDisplayed");
log.info("verifyElementsonHomeScreenTest Ended");
}
#Test(priority = 2, description = "Swipe to next video test")
#Severity(SeverityLevel.NORMAL)
#Description("TestCase Description: Swipe from one video to another")
public void swipeToNxtVideoTest() throws InterruptedException {
try {
logger = extent.createTest("Swipe from one video to another & get the username ");
log.info("***Executing swipeToNxtVideoTest***");
log.info("waitForElementToPresenceOfElementLocated - username");
TestUtil.waitForElementToPresenceOfElementLocated(By.id("user_name"));
log.info("swipeverticalDown for nxt video");
TestUtil.swipeverticalDown();
log.info("swipeToNxtVideoTest Ended");
} catch (Exception e) {
e.printStackTrace();
log.error("Found Exception - swipeToNxtVideoTest");
}
}
/*
* #Test(priority = 3, retryAnalyzer =
* com.automation.listeners.RetryAnalyzer.class ) public void checkFailure() {
* Assert.assertEquals(true, false); System.out.println("failed");
*
* }
*/
#AfterMethod
public void getResult(ITestResult result) throws Exception {
if (result.getStatus() == ITestResult.FAILURE) {
logger.log(Status.FAIL,
MarkupHelper.createLabel(result.getName() + " - Test Case Failed", ExtentColor.RED));
logger.log(Status.FAIL,
MarkupHelper.createLabel(result.getThrowable() + " - Test Case Failed", ExtentColor.RED));
String screenshotPath = TestUtil.captureScreenAsBase64(driver, result.getName());
logger.fail("Snapshot below: " + logger.addScreenCaptureFromPath(screenshotPath));
} else if (result.getStatus() == ITestResult.SKIP) {
logger.log(Status.SKIP,
MarkupHelper.createLabel(result.getName() + " - Test Case Skipped", ExtentColor.ORANGE));
} else if (result.getStatus() == ITestResult.SUCCESS) {
logger.log(Status.PASS,
MarkupHelper.createLabel(result.getName() + " Test Case PASSED", ExtentColor.GREEN));
}
extent.flush();
}
#AfterClass
public void quitDriver() {
getDriver().quit();
}
Please do let me know where I have been lacking in code; I might have a intuitions that there is an issue in testng annotations in my code
Base Class:
DesiredCapabilities capabilities = new DesiredCapabilities();
public void setDriver(AppiumDriver<MobileElement> driver) {
tdriver.set(driver);
}
public static synchronized AppiumDriver<MobileElement> getDriver() {
return tdriver.get();
}
public BaseClass() {
try {
prop = new Properties();
FileInputStream ip = new FileInputStream(
System.getProperty("user.dir") + "/src/main/java/com/automation/config/config.properties");
prop.load(ip);
// extend reports
Date date = new Date();
SimpleDateFormat dateFormatFolder = new SimpleDateFormat("dd_MMM_yyyy");
File ResultDir = new File(System.getProperty("user.dir") + File.separator + "/FrameworkReports/"
+ dateFormatFolder.format(date));
// Defining Directory/Folder Name
if (!ResultDir.exists()) { // Checks that Directory/Folder Doesn't Exists!
ResultDir.mkdir();
}
SimpleDateFormat dateFormat = new SimpleDateFormat("dd_MMM_yyyy_hh_mm_ssaa");
htmlReporter = new ExtentHtmlReporter(
ResultDir + "/" + "Report" + " " + dateFormat.format(date) + " .html");
htmlReporter.config().setDocumentTitle("Automation Report");
htmlReporter.config().setReportName("YOVO AUTOMATION");
htmlReporter.config().setTheme(Theme.DARK);
extent = new ExtentReports();
extent.attachReporter(htmlReporter);
extent.setSystemInfo("Host Name", "localhost");
extent.setSystemInfo("Environment", "Windows 7");
extent.setSystemInfo("User Name", "Abhishek Chauhan");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void initialize_driver(String platformName, String url, String udid) throws Exception {
log = LogManager.getLogger(BaseClass.class);
BasicConfigurator.configure();
File appDir = new File("/src/main/resources/apk");
File app = new File(appDir, "yovoapp-release.apk");
mDirpath = System.getProperty("user.dir");
mApkfilepath = mDirpath + "/app/yovoapp-release.apk";
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, platformName);
capabilities.setCapability(MobileCapabilityType.UDID, udid);
switch (platformName) {
case "Android":
capabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 60);
capabilities.setCapability("appPackage", prop.getProperty("androidAppPackage"));
capabilities.setCapability("appActivity", prop.getProperty("androidAppActivity"));
capabilities.setCapability("app", mApkfilepath);
capabilities.setCapability("noReset", true);
driver = new AppiumDriver<MobileElement>(new URL(url), capabilities);
// tdriver.set(driver);
// return getDriver();
case "IOS":
File classpathRoot = new File(System.getProperty("user.dir"));
// File appDir = new File(classpathRoot, "/build/");
// File app = new File(appDir, "WordPress.app");
capabilities.setCapability("platformVersion", "9.2");
capabilities.setCapability("deviceName", "iPhone 6");
capabilities.setCapability("app", app.getAbsolutePath());
// driver = new IOSDriver<MobileElement>(new
// URL("http://127.0.0.1:4723/wd/hub"), caps);
break;
default:
throw new Exception("Invalid platform! - " + platformName);
}
setDriver(driver);
}

How to correctly access rocksdb in flink stream task?

I tried to use rocksdb to cache information required by a ProcessFunction, and following seems to be the only way to get it to work by far:
(1) load data from datastore (eg. mysql) and put the data into rocksdb then close the rocksdb handle in open().
(2) open & close rocksdb handle whenever the processElement() is invoked.
like this:
public static class MatchFunction extends ProcessFunction<TaxiRide, TaxiRide> {
// keyed, managed state
// holds an END event if the ride has ended, otherwise a START event
private ValueState<TaxiRide> rideState;
private RocksDB rocksdb = null;
private String dbPath = null;
#Override
public void close() throws Exception {
super.close();
if(rocksdb != null) {
rocksdb.close();
}
}
#Override
public void open(Configuration config) {
ValueStateDescriptor<TaxiRide> startDescriptor =
new ValueStateDescriptor<>("saved ride", TaxiRide.class);
rideState = getRuntimeContext().getState(startDescriptor);
if(rocksdb == null) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connect = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connect = DriverManager
.getConnection("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&"
+ "user=user&password=password");
preparedStatement = connect.prepareStatement("select * from test.feature");
resultSet = preparedStatement.executeQuery();
RocksDB.loadLibrary();
try (final Options options = new Options().setCreateIfMissing(true)) {
// a factory method that returns a RocksDB instance
dbPath = "/tmp/checkpoints/rocksdb/test01_" + UUID.randomUUID();
try (final RocksDB db = RocksDB.open(options, dbPath)) {
rocksdb = db;
System.out.println("db opened: " + dbPath);
String key01 = "key01";
String val01 = "val01";
while (resultSet.next()) {
key01 = resultSet.getString(1);
val01 = resultSet.getString(2);
System.out.println("before put " + key01 + ":" + val01);
rocksdb.put(key01.getBytes(), val01.getBytes());
System.out.println("after put " + key01 + ":" + val01);
}
}
} catch (RocksDBException e) {
// do some error handling
e.printStackTrace();
} finally {
if(rocksdb != null) {
rocksdb.close();
System.out.println("db closed: " + dbPath);
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if(resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connect != null) {
try {
connect.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
#Override
public void processElement(TaxiRide ride, Context context, Collector<TaxiRide> out) throws Exception {
TimerService timerService = context.timerService();
try (final Options options = new Options().setCreateIfMissing(true)) {
// a factory method that returns a RocksDB instance
try (final RocksDB db = RocksDB.open(options, dbPath)) {
rocksdb = db;
// System.out.println("db opened: " + dbPath);
String val01 = new String(rocksdb.get("f8416af7-b895-4f28-bcea-be1eef6bbdb2".getBytes()));
// System.out.println(">>> val01 = " + val01);
rocksdb.close();
// System.out.println("db closed: " + dbPath);
}
} catch (RocksDBException e) {
// do some error handling
e.printStackTrace();
}
if (ride.isStart) {
// the matching END might have arrived first (out of order); don't overwrite it
if (rideState.value() == null) {
rideState.update(ride);
}
} else {
rideState.update(ride);
}
timerService.registerEventTimeTimer(ride.getEventTime() + 120 * 60 * 1000);
}
#Override
public void onTimer(long timestamp, OnTimerContext context, Collector<TaxiRide> out) throws Exception {
TaxiRide savedRide = rideState.value();
if (savedRide != null && savedRide.isStart) {
out.collect(savedRide);
}
rideState.clear();
}
}
This is very inefficient since lots of IO happens in processElement(). This ProcessFunction was able to process all data in 10 minutes, it takes more then 40 minutes to process partial data after adding the rocksdb related lines. So I tried to resuse the rocksdb handled created in open() with the following implementation.
public static class MatchFunction extends ProcessFunction<TaxiRide, TaxiRide> {
// keyed, managed state
// holds an END event if the ride has ended, otherwise a START event
private ValueState<TaxiRide> rideState;
private RocksDB rocksdb = null;
private String dbPath = null;
#Override
public void close() throws Exception {
super.close();
if(rocksdb != null) {
rocksdb.close();
}
}
#Override
public void open(Configuration config) {
ValueStateDescriptor<TaxiRide> startDescriptor =
new ValueStateDescriptor<>("saved ride", TaxiRide.class);
rideState = getRuntimeContext().getState(startDescriptor);
if(rocksdb == null) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connect = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
connect = DriverManager
.getConnection("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&"
+ "user=user&password=password");
preparedStatement = connect.prepareStatement("select * from test.feature");
resultSet = preparedStatement.executeQuery();
RocksDB.loadLibrary();
try (final Options options = new Options().setCreateIfMissing(true)) {
// a factory method that returns a RocksDB instance
dbPath = "/tmp/checkpoints/rocksdb/test01_" + UUID.randomUUID();
try (final RocksDB db = RocksDB.open(options, dbPath)) {
rocksdb = db;
System.out.println("db opened: " + dbPath);
String key01 = "key01";
String val01 = "val01";
while (resultSet.next()) {
key01 = resultSet.getString(1);
val01 = resultSet.getString(2);
System.out.println("before put " + key01 + ":" + val01);
rocksdb.put(key01.getBytes(), val01.getBytes());
System.out.println("after put " + key01 + ":" + val01);
}
}
} catch (RocksDBException e) {
// do some error handling
e.printStackTrace();
} finally {
// if(rocksdb != null) {
// rocksdb.close();
// System.out.println("db closed: " + dbPath);
// }
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if(resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connect != null) {
try {
connect.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
#Override
public void processElement(TaxiRide ride, Context context, Collector<TaxiRide> out) throws Exception {
TimerService timerService = context.timerService();
//try (final Options options = new Options().setCreateIfMissing(true)) {
// // a factory method that returns a RocksDB instance
// try (final RocksDB db = RocksDB.open(options, dbPath)) {
// rocksdb = db;
// System.out.println("db opened: " + dbPath);
String val01 = new String(rocksdb.get("f8416af7-b895-4f28-bcea-be1eef6bbdb2".getBytes()));
// System.out.println(">>> val01 = " + val01);
// rocksdb.close();
// System.out.println("db closed: " + dbPath);
// }
//} catch (RocksDBException e) {
// // do some error handling
// e.printStackTrace();
//}
if (ride.isStart) {
// the matching END might have arrived first (out of order); don't overwrite it
if (rideState.value() == null) {
rideState.update(ride);
}
} else {
rideState.update(ride);
}
timerService.registerEventTimeTimer(ride.getEventTime() + 120 * 60 * 1000);
}
#Override
public void onTimer(long timestamp, OnTimerContext context, Collector<TaxiRide> out) throws Exception {
TaxiRide savedRide = rideState.value();
if (savedRide != null && savedRide.isStart) {
out.collect(savedRide);
}
rideState.clear();
}
}
The problem with this implementation is that it just doesn't work and here is the error message I got:
#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x000000012c94cf55, pid=64626, tid=39683
#
# JRE version: Java(TM) SE Runtime Environment (8.0_60-b27) (build 1.8.0_60-b27)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.60-b23 mixed mode bsd-amd64 compressed oops)
# Problematic frame:
# [thread 39171 also had an error]
06:52:56.163 [pool-11-thread-1] INFO o.a.flink.contrib.streaming.state.RocksDBKeyedStateBackend - Asynchronous RocksDB snapshot (File Stream Factory # file:/tmp/checkpoints/53224270d2f2be67a9d20f9deac66d09, asynchronous part) in thread Thread[pool-11-thread-1,5,Flink Task Threads] took 10 ms.
06:52:56.163 [pool-16-thread-1] INFO o.a.flink.contrib.streaming.state.RocksDBKeyedStateBackend - Asynchronous RocksDB snapshot (File Stream Factory # file:/tmp/checkpoints/53224270d2f2be67a9d20f9deac66d09, asynchronous part) in thread Thread[pool-16-thread-1,5,Flink Task Threads] took 12 ms.
C06:52:56.163 [pool-13-thread-1] INFO o.a.flink.contrib.streaming.state.RocksDBKeyedStateBackend - Asynchronous RocksDB snapshot (File Stream Factory # file:/tmp/checkpoints/53224270d2f2be67a9d20f9deac66d09, asynchronous part) in thread Thread[pool-13-thread-1,5,Flink Task Threads] took 9 ms.
[librocksdbjni-osx.jnilib+0x3ff55] _Z18rocksdb_get_helperP7JNIEnv_PN7rocksdb2DBERKNS1_11ReadOptionsEPNS1_18ColumnFamilyHandleEP11_jbyteArrayii+0xe5
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
06:52:56.163 [pool-12-thread-1] INFO o.a.flink.contrib.streaming.state.RocksDBKeyedStateBackend - Asynchronous RocksDB snapshot (File Stream Factory # file:/tmp/checkpoints/53224270d2f2be67a9d20f9deac66d09, asynchronous part) in thread Thread[pool-12-thread-1,5,Flink Task Threads] took 13 ms.
[thread 46339 also had an error]
# An error report file with more information is saved as:
# /Users/abc/MyFiles/workspace/flink-java-project/hs_err_pid64626.log
[thread 22279 also had an error]
[thread 33027 also had an error]
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
Process finished with exit code 134 (interrupted by signal 6: SIGABRT)
Detail trace from "/Users/abc/MyFiles/workspace/flink-java-project/hs_err_pid64626.log" can be found in this link (http://memyselfandtaco.blogspot.tw/2018/04/how-to-correctly-access-rocksdb-in.html)

Sending SMS from BlackBerry Simulator

I'm developing a BlackBerry Application where I should send Text SMS from BlackBerry Device.
As I'm new to Blackberry, started few days back I'm unable to proceed.
Can anyone Help with providing code snippets for send SMS from BlackBerry Device or Simulator?
Thanks in Advance.
Suresh.
public static void sendSMS(final String no, final String msg) {
// try {
new Thread() {
public void run() {
boolean smsSuccess = false;
if (RadioInfo.getNetworkType() == RadioInfo.NETWORK_CDMA) {
DatagramConnection dc = null;
try {
dc = (DatagramConnection) Connector.open("sms://" + no);
byte[] data = msg.getBytes();
Datagram dg = dc.newDatagram(dc.getMaximumLength());
dg.setData(data, 0, data.length);
dc.send(dg);
// / send successfully
smsSuccess = true;
} catch (Exception e) {
System.out.println("Exception 1 : " + e.toString());
e.printStackTrace();
smsSuccess = false;
} finally {
try {
dc.close();
dc = null;
} catch (IOException e) {
System.out.println("Exception 2 : " + e.toString());
e.printStackTrace();
}
}
} else {
MessageConnection conn = null;
try {
conn = (MessageConnection) Connector
.open("sms://" + no);
TextMessage tmsg = (TextMessage) conn
.newMessage(MessageConnection.TEXT_MESSAGE);
tmsg.setAddress("sms://" + no);
tmsg.setPayloadText(msg);
conn.send(tmsg);
smsSuccess = true;
} catch (Exception e) {
smsSuccess = false;
System.out.println("Exception 3 : " + e.toString());
e.printStackTrace();
} finally {
try {
conn.close();
conn = null;
} catch (IOException e) {
System.out.println("Exception 4 : " + e.toString());
e.printStackTrace();
}
}
}
if(smsSuccess)
{
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
// TODO Auto-generated method stub
Dialog.alert("success");
}
});
}else
{
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
// TODO Auto-generated method stub
Dialog.alert("failure");
}
});
}
}
}.start();
}
Check out the the above code function .... to send SMS from Blackberry
You haven't specified what language you are developing in, but if you are developing in java and, if you are using Eclipse for your development with the Blackberry Java plugins, you will find a wealth of sample applications in the plugins folder hierarchy. The actual location will depend on where you have installed Eclipse, but e.g. on my machine they are at: C:\Program Files\Eclipse\eclipse 3.6.2 BlackBerry\plugins\net.rim.ejde.componentpack7.0.0_7.0.0.33\components\samples\com\rim\samples\device for the OS7 samples. Similar samples will exist for the different OS plugins you have installed.
There is a long standing sample in most OS sample sets called smsdemo which should give you all the code you need. Even if you are not developing in java, this sample should give you an indication of the path you need to take to fulfil your requirement.

GAE/J, PayPal request - NoClassDefFoundError for AuthorizationRequiredException

I am new to Google App Engine (Java) and PayPal process. I am using tutorial provided http://googleappengine.blogspot.com/2010/06/paypal-introduces-paypal-x-platform.html and NOT sure why I am getting following exception:
Uncaught exception from servlet
java.lang.NoClassDefFoundError: com/paypal/adaptive/exceptions/AuthorizationRequiredException
Here is my Servlet Class file which suppose to do preapproval and provide preapproval key and authorization url ..
import java.io.IOException;
import java.util.logging.Logger;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.paypal.adaptive.api.requests.fnapi.ParallelPay;
import com.paypal.adaptive.api.responses.PayResponse;
import com.paypal.adaptive.core.APICredential;
import com.paypal.adaptive.core.AckCode;
import com.paypal.adaptive.core.CurrencyCodes;
import com.paypal.adaptive.core.PaymentType;
import com.paypal.adaptive.core.Receiver;
import com.paypal.adaptive.core.ServiceEnvironment;
import com.paypal.adaptive.exceptions.AuthorizationRequiredException;
import com.paypal.adaptive.exceptions.InvalidAPICredentialsException;
import com.paypal.adaptive.exceptions.InvalidResponseDataException;
import com.paypal.adaptive.exceptions.MissingAPICredentialsException;
import com.paypal.adaptive.exceptions.MissingParameterException;
import com.paypal.adaptive.exceptions.NotEnoughReceivers;
import com.paypal.adaptive.exceptions.PayPalErrorException;
import com.paypal.adaptive.exceptions.PaymentExecException;
import com.paypal.adaptive.exceptions.PaymentInCompleteException;
import com.paypal.adaptive.exceptions.ReceiversCountMismatchException;
import com.paypal.adaptive.exceptions.RequestAlreadyMadeException;
import com.paypal.adaptive.exceptions.RequestFailureException;
#SuppressWarnings("serial")
public class CWEMartServlet extends HttpServlet {
private static final Logger log = Logger.getLogger(CWEMartServlet.class.getName());
private static APICredential credentialObj;
#Override
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
super.init(config);
// Get the value of APIUsername
String APIUsername = getServletConfig().getInitParameter("PPAPIUsername");
String APIPassword = getServletConfig().getInitParameter("PPAPIPassword");
String APISignature = getServletConfig().getInitParameter("PPAPISignature");
String AppID = getServletConfig().getInitParameter("PPAppID");
String AccountEmail = getServletConfig().getInitParameter("PPAccountEmail");
if(APIUsername == null || APIUsername.length() <= 0
|| APIPassword == null || APIPassword.length() <=0
|| APISignature == null || APISignature.length() <= 0
|| AppID == null || AppID.length() <=0 ) {
// requires API Credentials not set - throw exception
throw new ServletException("APICredential(s) missing");
}
credentialObj = new APICredential();
credentialObj.setAPIUsername(APIUsername);
credentialObj.setAPIPassword(APIPassword);
credentialObj.setSignature(APISignature);
credentialObj.setAppId(AppID);
credentialObj.setAccountEmail(AccountEmail);
log.info("Servlet initialized successfully");
}
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
try {
String id = req.getParameter("id");
String title = req.getParameter("title");
String order = req.getParameter("order");
String returnParam = req.getParameter("return");
String cancel = req.getParameter("cancel");
if(cancel != null && cancel.equals("1")) {
// user canceled the payment
getServletConfig().getServletContext().getRequestDispatcher("/paymentcancel.jsp").forward(req, resp);
} else if(returnParam != null && returnParam.equals("1")){
getServletConfig().getServletContext().getRequestDispatcher("/paymentsuccess.jsp").forward(req, resp);
} else if(order != null && order.length() > 0){
// process order
try {
StringBuilder url = new StringBuilder();
url.append(req.getRequestURL());
String returnURL = url.toString() + "?return=1&payKey=${payKey}&id="+ id + "&title=" + title;
String cancelURL = url.toString() + "?cancel=1&id="+ id + "&title=" + title;
//String ipnURL = url.toString() + "?action=ipn";
ParallelPay parallelPay = new ParallelPay(2);
parallelPay.setCancelUrl(cancelURL);
parallelPay.setReturnUrl(returnURL);
parallelPay.setCredentialObj(credentialObj);
parallelPay.setUserIp(req.getRemoteAddr());
parallelPay.setApplicationName("Sample app on GAE");
parallelPay.setCurrencyCode(CurrencyCodes.USD);
parallelPay.setEnv(ServiceEnvironment.SANDBOX);
//parallelPay.setIpnURL(ipnURL);
parallelPay.setLanguage("en_US");
parallelPay.setMemo(title);
// set the receivers
Receiver primaryReceiver = new Receiver();
primaryReceiver.setAmount(5.0);
primaryReceiver.setEmail("jagdis_1325390370_biz#yahoo.com");
primaryReceiver.setPaymentType(PaymentType.GOODS);
parallelPay.addToReceivers(primaryReceiver);
// set the second receivers
Receiver rec1 = new Receiver();
rec1.setAmount(3.0);
rec1.setEmail("jagdis_1331173124_biz#yahoo.com");
rec1.setPaymentType(PaymentType.GOODS);
parallelPay.addToReceivers(rec1);
PayResponse payResponse = parallelPay.makeRequest();
log.info("Payment success - payKey:" + payResponse.getPayKey());
} catch (IOException e) {
resp.getWriter().println("Payment Failed w/ IOException");
} catch (MissingAPICredentialsException e) {
// No API Credential Object provided - log error
e.printStackTrace();
resp.getWriter().println("No APICredential object provided");
} catch (InvalidAPICredentialsException e) {
// invalid API Credentials provided - application error - log error
e.printStackTrace();
resp.getWriter().println("Invalid API Credentials " + e.getMissingCredentials());
} catch (MissingParameterException e) {
// missing parameter - log error
e.printStackTrace();
resp.getWriter().println("Missing Parameter error: " + e.getParameterName());
} catch(ReceiversCountMismatchException e){
// missing receiver - log error
e.printStackTrace();
resp.getWriter().println("Receiver count did not match - expected: "
+ e.getExpectedNumberOfReceivers()
+ " - actual:" + e.getActualNumberOfReceivers());
} catch (RequestFailureException e) {
// HTTP Error - some connection issues ?
e.printStackTrace();
resp.getWriter().println("Request HTTP Error: " + e.getHTTP_RESPONSE_CODE());
} catch (InvalidResponseDataException e) {
// PayPal service error
// log error
e.printStackTrace();
resp.getWriter().println("Invalid Response Data from PayPal: \"" + e.getResponseData() + "\"");
} catch (PayPalErrorException e) {
// Request failed due to a Service/Application error
e.printStackTrace();
if(e.getResponseEnvelope().getAck() == AckCode.Failure){
// log the error
resp.getWriter().println("Received Failure from PayPal (ack)");
resp.getWriter().println("ErrorData provided:");
resp.getWriter().println(e.getPayErrorList().toString());
if(e.getPaymentExecStatus() != null){
resp.getWriter().println("PaymentExecStatus: " + e.getPaymentExecStatus());
}
} else if(e.getResponseEnvelope().getAck() == AckCode.FailureWithWarning){
// there is a warning - log it!
resp.getWriter().println("Received Failure with Warning from PayPal (ack)");
resp.getWriter().println("ErrorData provided:");
resp.getWriter().println(e.getPayErrorList().toString());
}
} catch (RequestAlreadyMadeException e) {
// shouldn't occur - log the error
e.printStackTrace();
resp.getWriter().println("Request to send a request that has already been sent!");
} catch (PaymentExecException e) {
resp.getWriter().println("Failed Payment Request w/ PaymentExecStatus: " + e.getPaymentExecStatus().toString());
resp.getWriter().println("ErrorData provided:");
resp.getWriter().println(e.getPayErrorList().toString());
}catch (PaymentInCompleteException e){
resp.getWriter().println("Incomplete Payment w/ PaymentExecStatus: " + e.getPaymentExecStatus().toString());
resp.getWriter().println("ErrorData provided:");
resp.getWriter().println(e.getPayErrorList().toString());
} catch (NumberFormatException e) {
// invalid number passed
e.printStackTrace();
resp.getWriter().println("Invalid number of receivers sent");
} catch (NotEnoughReceivers e) {
// not enough receivers - min requirements for Parallel pay not met
e.printStackTrace();
resp.getWriter().println("Min number of receivers not met - Min Required:"
+ e.getMinimumRequired() + " - actual set:" + e.getActualNumber());
} catch (AuthorizationRequiredException e) {
// redirect the user to PayPal for Authorization
resp.getWriter().println("\"PPAuthzUrl\": \"" + e.getAuthorizationUrl(ServiceEnvironment.SANDBOX) + "\", \"Status\": \"CREATED\"");
// resp.sendRedirect(e.getAuthorizationUrl(ServiceEnvironment.SANDBOX));
// resp.getWriter().println("\"PPAuthzUrl\": \"" + e.getEmbeddedPaymentsAuthorizationUrl(ServiceEnvironment.SANDBOX, ExpType.LIGHTBOX) + "\", \"Status\": \"CREATED\"");
}
} else if(id == null || id.length() <= 0) {
getServletConfig().getServletContext().getRequestDispatcher("/index.jsp").forward(req, resp);
} else {
getServletConfig().getServletContext().getRequestDispatcher("/order.jsp").forward(req, resp);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Check if you have the paypal.jar in lib directory. If you are not sure, unjar the paypal related lib files and check if you have AuthorizationRequiredException.class in at least one of them.

How to print from servlet to webpage?

Hi I am trying to print the list from servlet to web screen (jsp)
I am using log and it's not working.
Is there anyway to do or am I using this wrong?
private static final Logger log = Logger.getLogger(TodoServiceServlet.class.getName());
.....
Todo tmp = pm.getObjectById(Todo.class, user.getEmail());
System.out.println("user email: " + user.getEmail());
if(tmp==null){
log.info("You have not stored any todo lists yet");
}else{
System.out.println("user email is there?: " + tmp.getEmail());
System.out.println("start printing");
ArrayList<String> todolists = tmp.getList();
if(todolists==null)
System.out.println("Arraylist null");
if(!todolists.isEmpty()){
for(String t : todolists){
System.out.println("In the list: " + t);
log.info("You need to do: " + t);
}
}else{
log.info("You have nothing to do chil out!");
}
System.out would print to "standard out" on the web server (usually the console) not to the screen. What you have to do is instead write to the HttpServletResponse
so something like this:
public void doGet(HttpServletRequest req, HttpServletResponse resp) {
try {
resp.getWriter().println("user email is there?: " + tmp.getEmail());
} catch (IOException e) {
// handle your error here
}
}

Resources