Here, You can see the program which fetch the Leaked Connection count from weblogic. But, I always get it as zero. I have set maximum connection count to 10 from my data source and I run some code which not closes the connection so, Connection Unavailable count is increases to 10 but Liked Connection count is still 0.
So, What to do to increase the count of it because there are liked connections are there by application.
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Hashtable;
import java.util.List;
import java.util.Properties;
import java.util.logging.Logger;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class Test {
private static final Logger logger = Logger.getLogger(Test.class.getName());
private static final String PROTOCOL = "admin.server.protocol";
private static final String HOST = "admin.server.host";
private static final String PORT = "admin.server.port";
private static final String USERNAME = "admin.server.username";
private static final String PASSWORD = "admin.server.password";
private static final String JNDI_ROOT = "jndi.root";
private static final String DATA_SOURCE = "DataSourceName";
private static final String DATASOURCE_ORACLEDS_JTA = "dataSource-OracleDS_jta";
private static MBeanServerConnection connection;
private static JMXConnector connector;
public static void main(String[] args) throws InterruptedException {
Test test = new Test();
System.out.println(test.isConnectionLeaked());
}
public List<String> getDataSourceNames(){
return Arrays.asList(DATA_SOURCE,DATASOURCE_ORACLEDS_JTA);
}
/*
* Initialize connection to the Domain Runtime MBean Server.
*/
public static void initConnection() throws IOException,
MalformedURLException {
logger.info("Inside initConnection");
InputStream is = ClassLoader.getSystemResourceAsStream("jmx.properties");
Properties props = new Properties();
props.load(is);
String protocol = (String) props.get(PROTOCOL);
Integer portInteger = Integer.valueOf((String) props.get(PORT));
int port = portInteger.intValue();
String jndiroot = (String) props.get(JNDI_ROOT);
String hostname = (String) props.get(HOST);
JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname, port, jndiroot);
Hashtable<String, Object> h = new Hashtable<>();
h.put(Context.SECURITY_PRINCIPAL, (String) props.get(USERNAME));
h.put(Context.SECURITY_CREDENTIALS, (String) props.get(PASSWORD));
h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "weblogic.management.remote");
h.put("jmx.remote.x.request.waiting.timeout", new Long(10000));
connector = JMXConnectorFactory.connect(serviceURL, h);
connection = connector.getMBeanServerConnection();
logger.info("End initConnection");
}
public boolean isConnectionLeaked() {
List<String> dataPoolNames = getDataSourceNames();
boolean isLeaked = false;
try {
initConnection();
ObjectName service = new ObjectName("com.bea:Name=DomainRuntimeService,"
+ "Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean");
ObjectName[] number_of_servers = (ObjectName[]) connection.getAttribute(service, "ServerRuntimes");
int length = (int) number_of_servers.length;
for (int i = 0; i < length; i++) {
logger.info("Server Instance=" + number_of_servers[i]);
String name = (String) connection.getAttribute(number_of_servers[i], "Name");
ObjectName[] number_of_dbpools = (ObjectName[]) connection.getAttribute(new ObjectName("com.bea:Name="
+ name + ",ServerRuntime=" + name + ",Location=" + name + ",Type=JDBCServiceRuntime"),
"JDBCDataSourceRuntimeMBeans");
int pool_length = (int) number_of_dbpools.length;
for (int x = 0; x < pool_length; x++) {
String poolName = (String) connection.getAttribute(number_of_dbpools[x], "Name");
logger.info("********* PoolName=" + poolName + " ******");
int leakedConnectionCount = (Integer) connection.getAttribute(number_of_dbpools[x],
"LeakedConnectionCount");
logger.info("leakedConnectionCount : " + leakedConnectionCount);
if (leakedConnectionCount > 0) { // Send email alert
isLeaked = true;
}
}
}
} catch (Exception e) {
logger.severe("Exception in isConnectionLeaked method");
logger.severe("Message = " + e.getMessage());
} finally {
try {
if (connector != null) {
logger.info("Connectors JMXConnector.");
connector.close();
}
} catch (IOException e) {
logger.severe(e.getMessage());
}
}
logger.info("End isConnectionLeaked. isLeaked = "+isLeaked);
return isLeaked;
}
}
The Inactive Connection Timeout is set to 0?
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have write a readFromSharedInbox() method. When I am checking the code with sonarLint i am getting this error "Refractor this method to reduce its cognitive complexity". So i want to break this method in two parts. But i am not able to do so.I am trying to create a method to read the mail from inbox.
My Code look like this
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.ContentType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import javax.mail.search.ComparisonTerm;
import javax.mail.search.FlagTerm;
import javax.mail.search.ReceivedDateTerm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;
#Service
public class MailServiceImpl implements MailService {
#Value("${mail.host}")
private String host;
#Value("${mail.id}")
private String mailBoxId;
#Value("${mail.password}")
private String password;
#Value("${mail.protocol}")
private String protocol;
#Value("${mail.socketfactory}")
private String socketfactory;
#Value("${mail.fallback}")
private String fallback;
#Value("${mail.port}")
private String port;
#Value("${mail.savepath}")
private String savepath;
#Value("${submission.fields}")
private String fields;
#Value("${mail.endidentifier}")
private String mailEndIdentifier;
#Autowired
private EmailRequestRepository emailRequestRepository;
#Autowired
private CoreWorkflowService coreWorkflowService;
public void readFromSharedInbox() {
Properties props = new Properties();
props.setProperty("mail.store.protocol", protocol);
props.setProperty("mail.imaps.socketFactory.class", socketfactory);
props.setProperty("mail.imaps.socketFactory.fallback", fallback);
props.setProperty("mail.imaps.port", port);
props.setProperty("mail.imaps.socketFactory.port", port);
Session session = Session.getDefaultInstance(props, null);
try {
Store store = session.getStore(protocol);
store.connect(host, mailBoxId, password);
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_WRITE);
ReceivedDateTerm receivedDateTerm = new ReceivedDateTerm(ComparisonTerm.EQ, new Date());
FlagTerm seenTerm = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
Message[] todaysMessages = inbox.search(receivedDateTerm);
Message[] foundMessages = inbox.search(seenTerm, todaysMessages);
List<EmailRequest> emailRequestList = new ArrayList<>();
for (int i = 0; i < foundMessages.length; i++) {
Message message = foundMessages[i];
String subject = message.getSubject();
String content = message.getContent().toString();
String contentType = message.getContentType();
Address[] froms = message.getFrom();
String sender = froms == null ? null : ((InternetAddress) froms[0]).getAddress();
Date recieveDate = message.getReceivedDate();
// store attachment file name, separated by comma
StringBuilder attachFiles=new StringBuilder();
if (contentType.contains("multipart")) {
// content may contain attachments
MimeMultipart multiPart = (MimeMultipart) message.getContent();
content = getTextFromMimeMultipart(multiPart);
int numberOfParts = multiPart.getCount();
for (int partCount = 0; partCount < numberOfParts; partCount++) {
MimeBodyPart part = (MimeBodyPart) multiPart.getBodyPart(partCount);
if (Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())) {
// this part is attachment
String fileName = part.getFileName();
if (attachFiles.length() > 0) {
attachFiles.append(",");
attachFiles.append(fileName);
} else {
attachFiles.append(fileName);
}
System.out.println(fileName);
}
}
message.writeTo(new FileOutputStream(new File(savepath + sanitizeFileName(subject) + ".eml")));
}
System.out.println("Content type: " + contentType);
EmailRequest emailRequest = EmailRequest.builder().emailRecieveDate(recieveDate).subjectEmail(subject)
.emailContent(content).fromEmail(sender).toEmail(mailBoxId).isMailProcessed(false).build();
emailRequestList.add(emailRequest);
}
inbox.close(false);
store.close();
System.out.println("reading done!");
emailRequestRepository.saveAll(emailRequestList);
System.out.println("Email data saved successfully in db table");
} catch (Exception e) {
e.printStackTrace();
}
}
private String sanitizeFileName(String subject) {
return subject.replaceAll("[:\\\\/*?|<>]", "_");
}
private String getTextFromMimeMultipart(MimeMultipart mimeMultipart) throws IOException, MessagingException {
int count = mimeMultipart.getCount();
if (count == 0)
throw new MessagingException("Multipart with no body parts not supported.");
boolean multipartAlt = new ContentType(mimeMultipart.getContentType()).match("multipart/alternative");
if (multipartAlt)
// alternatives appear in an order of increasing
// faithfulness to the original content. Customize as req'd.
return getTextFromBodyPart(mimeMultipart.getBodyPart(count - 1));
StringBuilder result = new StringBuilder();
for (int i = 0; i < count; i++) {
BodyPart bodyPart = mimeMultipart.getBodyPart(i);
result.append(getTextFromBodyPart(bodyPart));
}
return result.toString();
}
private String getTextFromBodyPart(BodyPart bodyPart) throws IOException, MessagingException {
String result = "";
if (bodyPart.isMimeType("text/plain")) {
result = (String) bodyPart.getContent();
} else if (bodyPart.isMimeType("text/html")) {
result = (String) bodyPart.getContent();
} else if (bodyPart.getContent() instanceof MimeMultipart) {
result = getTextFromMimeMultipart((MimeMultipart) bodyPart.getContent());
}
return result;
}
#Override
public void readMailDetailsFromDb() {
EmailRequest email = new EmailRequest();
email.setIsMailProcessed(false);
Example<EmailRequest> whereClause = Example.of(email);
List<EmailRequest> findAll = emailRequestRepository.findAll(whereClause);
Map<String, Object> processVariables = null;
for (EmailRequest emaeil : findAll) {
System.out.println(emaeil.getSubjectEmail() + " : " + emaeil.getEmailId());
processVariables = EmailParserUtil.parse(fields, emaeil.getEmailContent(), mailEndIdentifier);
// workflow service
try {
Workflow startWorkflow = coreWorkflowService.startWorkflow(Constants.SUBMISSION_PROCESS_ID, Constants.MAIL, processVariables);
startWorkflow.toString();
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
// update mail
emaeil.setMailProcessedOn(LocalDateTime.now() ) ;
emaeil.setIsMailProcessed(true) ;
emailRequestRepository.save(emaeil) ;
}
}
}
This is MailService Class
import java.lang.reflect.InvocationTargetException;
public interface MailService {
public void readFromSharedInbox();
public void readMailDetailsFromDb() throws IllegalAccessException, InvocationTargetException;
}
You can use your IDE for things like that.
You select a part of your code, right click on it and refactor it by extracting the code in a new private method.
For exemple, you can do this on the "for" statement.
Or the "if (contentType.contains("multipart"))" part with a manageMultipart method.
I'm using snmp4j 3.4.2 inside my java application (full code below)
I'm trying to execute a snmpget with snmpv3, security DES and auth MD5 and custom OID (python script, which is executed by snmp's extend funtionality). To create better understanding I used SnmpConstants.sysUpTime in the example below.
The SNMP resource has this user configured:
defSecurityName demo
defSecurityLevel authPriv
defAuthType MD5
defPrivType DES
defAuthPassphrase pass
defPrivPassphrase pass
I'm already using this user and resource to successfully perform the snmpget with python (pysnmp) and bash (snmpget), so I can definitely tell that my setup works and the java code is the problem.
I have two java classes (Listener.java and ServerStatusHelper.java)
Listener.java contains main and calls the snmpGet inside ServerStatusHelper.java, other code of Listener is excluded as its not neccessary.
import org.snmp4j.PDU;
import org.snmp4j.ScopedPDU;
import org.snmp4j.Snmp;
import org.snmp4j.Target;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.security.AuthMD5;
import org.snmp4j.security.AuthSHA;
import org.snmp4j.security.PrivAES128;
import org.snmp4j.security.PrivDES;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.VariableBinding;
public class Listener {
public static void main(String[] args) {
ServerStatusHelper agent = new ServerStatusHelper("host.tld", "udp", 161, "demo", "demo",
"pass", "pass", new AuthMD5(), new PrivDES(), true);
try {
agent.startAgent();
ResponseEvent response = agent.snmpGetOperation(SnmpConstants.sysUpTime);
if (response != null) {
System.out.println(
"response null - error: "+ response.getError() +
"peerAddress: " + response.getPeerAddress() +
"source: " + response.getSource().toString() +
"request: " + response.getRequest());
}
} catch (
IOException e) {
e.printStackTrace();
}
}
}
ServerStatusHelper.java
import java.io.IOException;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.ScopedPDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.UserTarget;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.MPv3;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.security.AuthGeneric;
import org.snmp4j.security.AuthMD5;
import org.snmp4j.security.PrivDES;
import org.snmp4j.security.PrivacyGeneric;
import org.snmp4j.security.SecurityLevel;
import org.snmp4j.security.SecurityModels;
import org.snmp4j.security.SecurityProtocols;
import org.snmp4j.security.USM;
import org.snmp4j.security.UsmUser;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.TransportIpAddress;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultTcpTransportMapping;
import org.snmp4j.transport.DefaultUdpTransportMapping;
public class ServerStatusHelper {
private Address nmsIP;
private String user;
private String securityName;
private String privacyPassword;
private String authorizationPassword;
private AuthGeneric authProtocol;
private PrivacyGeneric privacyProtocol;
private String protocol;
private boolean encryption;
private long timeOut = 1000;
private int noOfRetries = 10;
private Snmp snmp;
private UserTarget target;
private CommunityTarget v1target;
ServerStatusHelper(String ip, String protocol, int snmpPort, String username, String securityName,
String privacyPassword, String authPassowrd, AuthGeneric authProtocol, PrivacyGeneric privacyProtocol,
boolean encryption) {
nmsIP = GenericAddress.parse(protocol + ":" + ip + "/" + snmpPort);
System.out.println("NMS IP set : " + nmsIP.toString());
this.protocol = protocol;
this.user = username;
this.securityName = securityName;
this.privacyPassword = privacyPassword;
this.authorizationPassword = authPassowrd;
this.authProtocol = authProtocol;
this.privacyProtocol = privacyProtocol;
this.encryption = encryption;
SecurityProtocols.getInstance().addAuthenticationProtocol(new AuthMD5());
SecurityProtocols.getInstance().addPrivacyProtocol(new PrivDES());
}
public void startAgent() throws IOException {
if (snmp == null) {
TransportMapping<? extends TransportIpAddress> transport = null;
if (protocol.equalsIgnoreCase("udp")) {
System.out.println("UDP Protocol selected.");
transport = new DefaultUdpTransportMapping();
} else {
System.out.println("TCP Protocol selected.");
transport = new DefaultTcpTransportMapping();
}
snmp = new Snmp(transport);
USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
SecurityModels.getInstance().addSecurityModel(usm);
transport.listen();
snmp.getUSM().addUser(new OctetString(user),
new UsmUser(new OctetString(securityName), authProtocol.getID(),
new OctetString(authorizationPassword), privacyProtocol.getID(),
new OctetString(privacyPassword)));
if (encryption)
target = createUserTarget();
else
v1target = createUserTargetWithoutEncryption();
}
}
public ResponseEvent snmpSetOperation(VariableBinding[] vars) throws IOException {
PDU setPdu = new ScopedPDU();
for (VariableBinding variableBinding : vars) {
setPdu.add(variableBinding);
}
return snmp.send(setPdu, target);
}
public ResponseEvent snmpGetOperation(OID oid) throws IOException {
if (encryption) {
PDU getPdu = new ScopedPDU();
getPdu.add(new VariableBinding(oid));
getPdu.setType(ScopedPDU.GET);
return snmp.get(getPdu, target);
} else {
PDU getPdu = new PDU();
getPdu.add(new VariableBinding(oid));
getPdu.setType(PDU.GET);
return snmp.get(getPdu, v1target);
}
}
private UserTarget createUserTarget() {
UserTarget target = new UserTarget();
target.setAddress(nmsIP);
target.setRetries(noOfRetries);
target.setTimeout(timeOut);
target.setVersion(SnmpConstants.version3);
target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
target.setSecurityName(new OctetString(securityName));
return target;
}
private CommunityTarget createUserTargetWithoutEncryption() {
CommunityTarget target = new CommunityTarget();
target.setCommunity(new OctetString("public"));
target.setAddress(nmsIP);
target.setRetries(noOfRetries);
target.setTimeout(timeOut);
target.setVersion(SnmpConstants.version1);
return target;
}
public long getTimeOut() {
return timeOut;
}
public void setTimeOut(long timeOut) {
this.timeOut = timeOut;
}
public int getNoOfRetries() {
return noOfRetries;
}
public void setNoOfRetries(int noOfRetries) {
this.noOfRetries = noOfRetries;
}
}
The execution of the program exits with
NMS IP set : **IPREMOVED**/161
UDP Protocol selected.
response null - error: nullpeerAddress: **IPREMOVED**/161source: org.snmp4j.Snmp#e580929 request: GET[{contextEngineID=80:00:1f:88:80:5e:2e:49:07:2f:68:44:57:00:00:00:00, contextName=}, requestID=588252045, errorStatus=0, errorIndex=0, VBS[1.3.6.1.2.1.1.3.0 = Null]]
Anyone has an idea what I'm doing wrong?
Edit:
From the servers syslog I can see, that the request arrives at the resource:
Jul 31 11:52:46 loadbalancer snmpd[1219]: Connection from UDP: [IP REMOVED]:54734->[IP REMOVED]:161
Jul 31 11:52:46 loadbalancer snmpd[1219]: Connection from UDP: [IP REMOVED]:54734->[IP REMOVED]:161
#i-shm I think you are doing everything ok.
The only problem in your code could be just the line in which you evaluate the response variable. Your are indicating response != null when it should be actually response == null. I mean:
import org.snmp4j.PDU;
import org.snmp4j.ScopedPDU;
import org.snmp4j.Snmp;
import org.snmp4j.Target;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.security.AuthMD5;
import org.snmp4j.security.AuthSHA;
import org.snmp4j.security.PrivAES128;
import org.snmp4j.security.PrivDES;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.VariableBinding;
public class Listener {
public static void main(String[] args) {
ServerStatusHelper agent = new ServerStatusHelper("host.tld", "udp", 161, "demo", "demo",
"pass", "pass", new AuthMD5(), new PrivDES(), true);
try {
agent.startAgent();
ResponseEvent event = agent.snmpGetOperation(SnmpConstants.sysUpTime);
final PDU response = event.getResponse();
if (response == null) {
System.out.println(
"response null - error: "+ event.getError() +
"peerAddress: " + event.getPeerAddress() +
"source: " + event.getSource().toString() +
"request: " + event.getRequest());
} else {
System.out.println("Response PDU:" + response.toString());
// Process the response as you need, maybe something like this:
long sysUpTime = response.get(0).getVariable().toLong();
// You can find relevant information in the javadocs of the library:
// https://agentpp.com/doc/snmp4j/index.html?org/snmp4j/package-summary.html
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
This is why you do not receive errors in the java code and your syslog indicates that the requests were actually sent.
i need help i'm trying to create the connection with the FTP'Server where i upload my file.
i'm using below dependency with java jdk1.8.0_45. when i run my code it's make connection and also login but when i try to upload file it's throw Exception and upload empty file
Exception in thread "main" javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:980)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1363)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1391)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1375)
at org.apache.commons.net.ftp.FTPSClient._openDataConnection_(FTPSClient.java:619)
at org.apache.commons.net.ftp.FTPClient._storeFile(FTPClient.java:633)
at org.apache.commons.net.ftp.FTPClient.__storeFile(FTPClient.java:624)
at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:1976)
at org.quorum.appnexusutility.transfer.TransferManager.uploadFile(TransferManager.java:176)
at org.quorum.appnexusutility.transfer.TransferManager.pushFile(TransferManager.java:220)
at org.quorum.appnexusutility.transfer.TransferManager.main(TransferManager.java:233)
Caused by: java.io.EOFException: SSL peer shut down incorrectly
at sun.security.ssl.InputRecord.read(InputRecord.java:505)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:961)
... 10 more
Dependency
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.3</version>
</dependency>
Below code i change the host may not work for you
package org.quorum.appnexusutility.transfer;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTPSClient;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSessionContext;
import javax.net.ssl.SSLSocket;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.Socket;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.Locale;
#Component
#Scope("prototype")
public class TransferManager {
public Logger logger = LogManager.getLogger(TransferManager.class);
// SSL/TLS
private final Integer TENSECONDS = 10*1000; // 10 second
private final String protocol = "TLS";//"TLS";
private Boolean isLogin = false;
private Boolean isUpload = false;
private String directoryPath;
private String host;
private Integer port;
private String user;
private String password;
private FTPSClient ftpsClient;
public TransferManager() { }
public String getHost() { return host; }
public TransferManager setHost(String host) {
this.host = host;
return this;
}
public Integer getPort() { return port; }
public TransferManager setPort(Integer port) {
this.port = port;
return this;
}
public String getUser() { return user; }
public TransferManager setUser(String user) {
this.user = user;
return this;
}
public String getPassword() { return password; }
public TransferManager setPassword(String password) {
this.password = password;
return this;
}
public String getDirectoryPath() { return directoryPath; }
public void setDirectoryPath(String directoryPath) {
this.directoryPath = directoryPath;
}
public void open() throws IOException {
//System.setProperty("jdk.tls.useExtendedMasterSecret", "false");
//this.ftpsClient = new SSLSessionReuseFTPSClient();
//ftpsClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
if(this.port > 100) {
this.ftpsClient = new FTPSClient(this.protocol, true);
} else {
this.ftpsClient = new FTPSClient(false);
}
this.ftpsClient.setControlKeepAliveTimeout(TENSECONDS);
this.showServerReply(this.ftpsClient);
logger.info("FTP :- Connection try :- IP :- (" + this.host + ") , Port :- " + this.port + " Start");
this.ftpsClient.connect(this.host, this.port);
this.ftpsClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
logger.info("FTP :- Connection try :- IP :- (" + this.host + ") , Port :- " + this.port + " Done");
int reply = this.ftpsClient.getReplyCode();
logger.info("FTP :- Connection Code :- " + reply);
if(!FTPReply.isPositiveCompletion(reply)) {
this.ftpsClient.disconnect();
throw new IOException("Exception in connecting to FTP Server");
}
this.isLogin = this.ftpsClient.login(user, password);
this.showServerReply(this.ftpsClient);
logger.info("FTP :- Login Status :- " + this.isLogin);
}
private Boolean makeDirectory() throws IOException {
Boolean isDirectoryCreate = false;
if(this.isLogin && this.directoryPath != null) {
isDirectoryCreate = this.ftpsClient.makeDirectory(this.directoryPath);
this.showServerReply(this.ftpsClient);
if (isDirectoryCreate) {
logger.info("Successfully created directory: " + this.directoryPath);
} else {
logger.info("Failed to create directory. See server's reply.");
}
}
return isDirectoryCreate;
}
// if returns 250, folder exists and if returned 550 folder does not exist***
private Boolean isDirectoryExist() throws IOException {
if(this.ftpsClient.cwd(this.directoryPath) == 550) {
logger.info("Directory Not Exist");
return false;
} else {
logger.info("Directory Exist");
return true;
}
}
// upload process
public Boolean uploadFile(File file) throws Exception {
if(this.isLogin && this.directoryPath != null) {
if(!this.isDirectoryExist()) {
// call when not exist
this.makeDirectory();
}
this.ftpsClient.enterLocalPassiveMode();
this.ftpsClient.execPBSZ(0);
this.ftpsClient.execPROT("P");
this.ftpsClient.setFileType(FTP.BINARY_FILE_TYPE);
// only the text file can be upload
//this.ftpsClient.setFileType(FTP.TELNET_TEXT_FORMAT);
this.ftpsClient.changeWorkingDirectory(this.getDirectoryPath());
logger.info("Current Directory " + this.ftpsClient.printWorkingDirectory());
FileInputStream fileInputStream = new FileInputStream(file);
String filePath = RandomStringUtils.randomAlphanumeric(8)+".txt";
logger.info("Final Path :- " + filePath);
//this.isUpload =
this.ftpsClient.storeFile(filePath, fileInputStream);
if(this.isUpload) {
logger.info("The file is uploaded successfully.");
}
// close the stream which use to store the file
fileInputStream.close();
}
return this.isUpload;
}
// connection close for client
public void close() throws IOException {
if (this.ftpsClient.isConnected()) {
this.ftpsClient.logout();
this.ftpsClient.disconnect();
}
}
public String getDirectoryPathRandom() {
Date date = new Date();
LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
int year = localDate.getYear();
int month = localDate.getMonthValue();
int day = localDate.getDayOfMonth();
int hours = date.getHours();
logger.info("Year :- (" + year + ") Month :- (" + month + ") Day :- (" + day + ") Hours :- (" + hours + ")");
return year + "/"+ month + "/" + day + "/" + hours;
}
private void showServerReply(FTPSClient ftpsClient) {
String[] replies = ftpsClient.getReplyStrings();
if (replies != null && replies.length > 0) {
for (String aReply : replies) {
logger.info("SERVER: " + aReply);
}
}
}
// this method use to push the file from 1 server to other server
public static void pushFile(String host, int port, String user, String password, File file) throws Exception {
TransferManager transferManager = new TransferManager().setHost(host).setPort(port).setUser(user).setPassword(password);
// we push the fiel on the base of yyyy/mm/dd/hr
// first check if the dir exist then no need to create the directory
transferManager.setDirectoryPath(transferManager.getDirectoryPathRandom());
transferManager.open(); // open connection
transferManager.uploadFile(file); // file
transferManager.close(); // close connection
}
//======================FTP=========================
//FTP URL: ftp.dlptest.com or ftp://ftp.dlptest.com/
//FTP User: dlpuser#dlptest.com
//Password: bbCKucPzfr4b9YXUY7tvsNKyh
public static void main(String args[]) throws Exception {
pushFile("ftps.xyzserver.com", 990, "macak", "Go447641", new File("C:\\Users\\Nabeel.Ahmed\\Desktop\\Study-Pending\\Detail Document.txt"));
}
}
It seems to be the problem with SSL/TLS protocol version issue. Go through the following post SSL peer shut down incorrectly in Java You should get the answer.
Can you try to replace the following line
private final String protocol = "TLS";//"TLS";
with
private final String protocol = "TLSv1.1";
All after lot of search and finally found the solution by reuse the method of prepareDataSocket here the code.
import org.apache.commons.net.ftp.FTPSClient;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSessionContext;
import javax.net.ssl.SSLSocket;
import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.Socket;
import java.util.Locale;
public class ModifiedFTPSClient extends FTPSClient {
public ModifiedFTPSClient() {
super("TLS", false);
}
public ModifiedFTPSClient(boolean isImplicit) {
super("TLS", isImplicit);
}
#Override
protected void _prepareDataSocket_(final Socket socket) throws IOException {
if (socket instanceof SSLSocket) {
final SSLSession session = ((SSLSocket) _socket_).getSession();
if (session.isValid()) {
final SSLSessionContext context = session.getSessionContext();
try {
final Field sessionHostPortCache = context.getClass().getDeclaredField("sessionHostPortCache");
sessionHostPortCache.setAccessible(true);
final Object cache = sessionHostPortCache.get(context);
final Method method = cache.getClass().getDeclaredMethod("put", Object.class, Object.class);
method.setAccessible(true);
method.invoke(cache, String.format("%s:%s", socket.getInetAddress().getHostName(), String.valueOf(socket.getPort())).toLowerCase(Locale.ROOT), session);
method.invoke(cache, String.format("%s:%s", socket.getInetAddress().getHostAddress(), String.valueOf(socket.getPort())).toLowerCase(Locale.ROOT), session);
} catch (NoSuchFieldException e) {
throw new IOException(e);
} catch (Exception e) {
throw new IOException(e);
}
} else {
throw new IOException("Invalid SSL Session");
}
}
}
Above Question Class TransferManager replace the class like this
private String user;
private String password;
private ModifiedFTPSClient ftpsClient;
Above Question Class TransferManager replace the value in open() method
if(this.port > 100) {
this.ftpsClient = new ModifiedFTPSClient(true); // fro ftps
} else {
this.ftpsClient = new ModifiedFTPSClient(); // fro ftp
}
This is the main class in which query is being fired
package extractKeyword;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.methods.GetMethod;
import org.dbpedia.spotlight.exceptions.AnnotationException;
import org.dbpedia.spotlight.model.DBpediaResource;
import org.dbpedia.spotlight.model.Text;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.LinkedList;
import java.util.List;
public class db extends AnnotationClient {
//private final static String API_URL = "http://jodaiber.dyndns.org:2222/";
private static String API_URL = "http://spotlight.dbpedia.org/";
private static double CONFIDENCE = 0.0;
private static int SUPPORT = 0;
// private static String powered_by ="non";
// private static String spotter ="CoOccurrenceBasedSelector";//"LingPipeSpotter"=Annotate all spots
//AtLeastOneNounSelector"=No verbs and adjs.
//"CoOccurrenceBasedSelector" =No 'common words'
//"NESpotter"=Only Per.,Org.,Loc.
//private static String disambiguator ="Default";//Default ;Occurrences=Occurrence-centric;Document=Document-centric
//private static String showScores ="yes";
#SuppressWarnings("static-access")
public void configiration(double CONFIDENCE,int SUPPORT)
//, String powered_by,String spotter,String disambiguator,String showScores)
{
this.CONFIDENCE=CONFIDENCE;
this.SUPPORT=SUPPORT;
// this.powered_by=powered_by;
//this.spotter=spotter;
//this.disambiguator=disambiguator;
//showScores=showScores;
}
public List<DBpediaResource> extract(Text text) throws AnnotationException {
// LOG.info("Querying API.");
String spotlightResponse;
try {
String Query=API_URL + "rest/annotate/?" +
"confidence=" + CONFIDENCE
+ "&support=" + SUPPORT
// + "&spotter=" + spotter
// + "&disambiguator=" + disambiguator
// + "&showScores=" + showScores
// + "&powered_by=" + powered_by
+ "&text=" + URLEncoder.encode(text.text(), "utf-8");
//LOG.info(Query);
GetMethod getMethod = new GetMethod(Query);
getMethod.addRequestHeader(new Header("Accept", "application/json"));
spotlightResponse = request(getMethod);
} catch (UnsupportedEncodingException e) {
throw new AnnotationException("Could not encode text.", e);
}
assert spotlightResponse != null;
JSONObject resultJSON = null;
JSONArray entities = null;
try {
resultJSON = new JSONObject(spotlightResponse);
entities = resultJSON.getJSONArray("Resources");
} catch (JSONException e) {
//throw new AnnotationException("Received invalid response from DBpedia Spotlight API.");
}
LinkedList<DBpediaResource> resources = new LinkedList<DBpediaResource>();
if(entities!=null)
for(int i = 0; i < entities.length(); i++) {
try {
JSONObject entity = entities.getJSONObject(i);
resources.add(
new DBpediaResource(entity.getString("#URI"),
Integer.parseInt(entity.getString("#support"))));
} catch (JSONException e) {
//((Object) LOG).error("JSON exception "+e);
}
}
return resources;
}
}
The extended class
package extractKeyword;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.dbpedia.spotlight.exceptions.AnnotationException;
import org.dbpedia.spotlight.model.DBpediaResource;
import org.dbpedia.spotlight.model.Text;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Logger;
import javax.ws.rs.HttpMethod;
/**
* #author pablomendes
*/
public abstract class AnnotationClient {
//public Logger LOG = Logger.getLogger(this.getClass());
private List<String> RES = new ArrayList<String>();
// Create an instance of HttpClient.
private static HttpClient client = new HttpClient();
public List<String> getResu(){
return RES;
}
public String request(GetMethod getMethod) throws AnnotationException {
String response = null;
// Provide custom retry handler is necessary
( getMethod).getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
try {
// Execute the method.
int statusCode = client.executeMethod((org.apache.commons.httpclient.HttpMethod) getMethod);
if (statusCode != HttpStatus.SC_OK) {
// LOG.error("Method failed: " + ((HttpMethodBase) method).getStatusLine());
}
// Read the response body.
byte[] responseBody = ((HttpMethodBase) getMethod).getResponseBody(); //TODO Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary data
response = new String(responseBody);
} catch (HttpException e) {
// LOG.error("Fatal protocol violation: " + e.getMessage());
throw new AnnotationException("Protocol error executing HTTP request.",e);
} catch (IOException e) {
//((Object) LOG).error("Fatal transport error: " + e.getMessage());
//((Object) LOG).error(((HttpMethodBase) method).getQueryString());
throw new AnnotationException("Transport error executing HTTP request.",e);
} finally {
// Release the connection.
((HttpMethodBase) getMethod).releaseConnection();
}
return response;
}
protected static String readFileAsString(String filePath) throws java.io.IOException{
return readFileAsString(new File(filePath));
}
protected static String readFileAsString(File file) throws IOException {
byte[] buffer = new byte[(int) file.length()];
#SuppressWarnings("resource")
BufferedInputStream f = new BufferedInputStream(new FileInputStream(file));
f.read(buffer);
return new String(buffer);
}
static abstract class LineParser {
public abstract String parse(String s) throws ParseException;
static class ManualDatasetLineParser extends LineParser {
public String parse(String s) throws ParseException {
return s.trim();
}
}
static class OccTSVLineParser extends LineParser {
public String parse(String s) throws ParseException {
String result = s;
try {
result = s.trim().split("\t")[3];
} catch (ArrayIndexOutOfBoundsException e) {
throw new ParseException(e.getMessage(), 3);
}
return result;
}
}
}
public void saveExtractedEntitiesSet(String Question, LineParser parser, int restartFrom) throws Exception {
String text = Question;
int i=0;
//int correct =0 ; int error = 0;int sum = 0;
for (String snippet: text.split("\n")) {
String s = parser.parse(snippet);
if (s!= null && !s.equals("")) {
i++;
if (i<restartFrom) continue;
List<DBpediaResource> entities = new ArrayList<DBpediaResource>();
try {
entities = extract(new Text(snippet.replaceAll("\\s+"," ")));
System.out.println(entities.get(0).getFullUri());
} catch (AnnotationException e) {
// error++;
//LOG.error(e);
e.printStackTrace();
}
for (DBpediaResource e: entities) {
RES.add(e.uri());
}
}
}
}
public abstract List<DBpediaResource> extract(Text text) throws AnnotationException;
public void evaluate(String Question) throws Exception {
evaluateManual(Question,0);
}
public void evaluateManual(String Question, int restartFrom) throws Exception {
saveExtractedEntitiesSet(Question,new LineParser.ManualDatasetLineParser(), restartFrom);
}
}
The Main Class
package extractKeyword;
public class startAnnonation {
public static void main(String[] args) throws Exception {
String question = "What is the winning chances of BJP in New Delhi elections?";
db c = new db ();
c.configiration(0.25,0);
//, 0, "non", "AtLeastOneNounSelector", "Default", "yes");
c.evaluate(question);
System.out.println("resource : "+c.getResu());
}
}
The main problem is here when I am using DBPedia spotlight using spotlight jar (above code)then i am getting different result as compared to the dbpedia spotlight endpoint(dbpedia-spotlight.github.io/demo/)
Result using the above code:-
Text :-What is the winning chances of BJP in New Delhi elections?
Confidence level:-0.35
resource : [Election]
Result on DBPedia Spotlight endpoint(//dbpedia-spotlight.github.io/demo/)
Text:-What is the winning chances of BJP in New Delhi elections?
Confidence level:-0.35
resource : [Bharatiya_Janata_Party, New_Delhi, Election]
Why also the spotlight now don't have support as a parameter?
I have written a code in java for web services of crm on demand. The code reads the 1st column (id) from excel and queries it in the crm and inserts the values accordingly. If the 'id' is found it inserts the other values, but when the 'id' is not found in the crm the code is terminated with error message. I want the code to query for a record and if found insert the other values, but if the record is not found it should skip that record and query for the next id without it being terminated in between.
Please help...
and also how to skip cells with no value(null)??
import crmondemand.ws.account.AccountWS_AccountInsertChild_Input;
import crmondemand.ws.account.AccountWS_AccountInsertChild_Output;
import crmondemand.ws.account.AccountWS_AccountUpdate_Input;
import crmondemand.ws.account.AccountWS_AccountUpdate_Output;
import crmondemand.ws.account.Default_BindingStub;
import crmondemand.xml.account.Account;
import crmondemand.xml.account.RelatedAccount;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.rmi.RemoteException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.TimeZone;
import javax.xml.rpc.ServiceException;
import jxl.Cell;
import jxl.CellType;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.apache.axis.AxisProperties;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class accrel1 {
private String inputFile;
public static int length;
public void setInputFile(String inputFile) {
this.inputFile = inputFile;
}
static Logger logger = Logger.getLogger(accrel1.class);
public static URL reauthentication(Properties properties) throws MalformedURLException {
// System.setProperty("https.proxyHost", "172.17.24.24");
// System.setProperty("https.proxyPort", "8003");
//System.setProperty("https.proxyHost", "145.247.13.164");
// System.setProperty("https.proxyPort", "3128");
// System.setProperty("javax.net.ssl.trustStore","C:\\ProgramFiles\\Java\\jdk1.6.0_31\\jre\\lib\\cacerts");
System.out.println("Loggin In");
String jsessionid_full =
logon("https://secure-ausomxapa.crmondemand.com/Services/Integration",
"################", "##########", properties);
System.out.println("Connecting to CRM..." + jsessionid_full);
String jsessionid = getSessionId(jsessionid_full);
System.out.println("JSessionid: " + jsessionid);
String endpoint =
"https://secure-ausomxapa.crmondemand.com/Services/Integration" +
";jsessionid=" + jsessionid;
URL urlAddr = new java.net.URL(endpoint);
System.out.println("Establishing Connection...");
return urlAddr;
}
public static void urlqueryWS1(URL urlAddr, List cellDataList,
Properties properties) throws RemoteException,
ServiceException,
MalformedURLException {
AxisProperties.setProperty("https.proxyHost",
properties.getProperty("proxyhost"));
AxisProperties.setProperty("https.proxyPort",
properties.getProperty("proxyport"));
// AxisProperties.setProperty("http.nonProxyHosts", "secure-ausomxapa.crmondemand.com");
crmondemand.ws.account.AccountWS_AccountQueryPage_Input accountlist =
new crmondemand.ws.account.AccountWS_AccountQueryPage_Input();
crmondemand.ws.account.AccountWS_AccountQueryPage_Output outlist =
new crmondemand.ws.account.AccountWS_AccountQueryPage_Output();
crmondemand.xml.account.Account[] accounts =
new crmondemand.xml.account.Account[1];
crmondemand.xml.account.Account account =
new crmondemand.xml.account.Account();
String stringCellValue[] = new String[6000];
int k = 0;
int flag = 0, notflag = 0, noparentflag = 0;
System.out.println("CellDatasize:" + cellDataList.size());
for (int i = 1; i < cellDataList.size(); i++) {
crmondemand.ws.account.Account service =
new crmondemand.ws.account.AccountLocator();
Default_BindingStub stub =
(Default_BindingStub)service.getDefault(urlAddr);
List cellTempList = (List)cellDataList.get(i);
for (int j = 0; j < cellTempList.size(); j++) {
HSSFCell hssfCell = (HSSFCell)cellTempList.get(j);
//System.out.println("Cell Type:" + i + "," + j + " " + hssfCell.getCellType());
if (hssfCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
stringCellValue[j] = hssfCell.getStringCellValue();
// logger.info(i + "," + j + "\t" + stringCellValue[j] + "\t");
} else if (hssfCell.getCellType() ==
HSSFCell.CELL_TYPE_NUMERIC) {
if (HSSFDateUtil.isCellDateFormatted(hssfCell)) {
System.out.println(hssfCell.getDateCellValue());
Date date = new Date();
date = hssfCell.getDateCellValue();
SimpleDateFormat df =
new SimpleDateFormat("MM/dd/yyyy");
stringCellValue[j] = df.format(date);
} else {
stringCellValue[j] = new BigDecimal(hssfCell.getNumericCellValue()).toPlainString();
//logger.info("Number"+i+","+j+ "\t"+stringCellValue[j] + "\t");
}
}
}
AccountWS_AccountInsertChild_Input child =
new AccountWS_AccountInsertChild_Input();
AccountWS_AccountInsertChild_Output childs =
new AccountWS_AccountInsertChild_Output();
crmondemand.xml.account.Account[] accrels =
new crmondemand.xml.account.Account[1];
crmondemand.xml.account.Account accrel =
new crmondemand.xml.account.Account();
RelatedAccount[] relaccounts = new RelatedAccount[1];
RelatedAccount relaccount = new RelatedAccount();
logger.info("Inserting " + i + "Record: "+stringCellValue[0]);
relaccount.setRelationshipRole(stringCellValue[1]);
relaccount.setRelatedAccountId(stringCellValue[2]);
relaccount.setStartDate(stringCellValue[6]);
relaccount.setEndDate(stringCellValue[7]);
relaccount.setReverseRelationshipRole(stringCellValue[3]);
relaccount.setComments(stringCellValue[4]);
relaccount.setRelationshipStatus(stringCellValue[5]);
relaccounts[0] = relaccount;
accrel.setAccountId(stringCellValue[0]); //JDE Account ID
accrel.setListOfRelatedAccount(relaccounts);
accrels[0] = accrel;
child.setListOfAccount(accrels);
try {
childs = stub.accountInsertChild(child);
logger.info(i + "th Record Inserted");
++flag;
} catch (Exception e) {
logger.info("Network Error: Re-Authenticating" + e);
urlAddr = reauthentication(properties);
stub = (Default_BindingStub)service.getDefault(urlAddr);
childs = stub.accountInsertChild(child);
logger.info(i + "th Record Inserted in 2nd Attempt");
++flag;
}
//logger.info("Total No. Of Records Processed"+flag);
}
logger.info("Total No. Of Records Processed"+flag);
}
private void readExcelFile(URL urlAddr, String fileName,
Properties properties) throws ServiceException,
RemoteException,
MalformedURLException {
System.out.println("Reading Excel File");
/**
* Create a new instance for cellDataList
*/
List cellDataList = new ArrayList();
try {
/**
* Create a new instance for FileInputStream class
*/
FileInputStream fileInputStream = new FileInputStream(fileName);
/**
* Create a new instance for POIFSFileSystem class
*/
POIFSFileSystem fsFileSystem =
new POIFSFileSystem(fileInputStream);
/*
* Create a new instance for HSSFWorkBook Class
*/
HSSFWorkbook workBook = new HSSFWorkbook(fsFileSystem);
HSSFSheet hssfSheet = workBook.getSheetAt(0);
/**
* Iterate the rows and cells of the spreadsheet
* to get all the datas.
*/
Iterator rowIterator = hssfSheet.rowIterator();
while (rowIterator.hasNext()) {
HSSFRow hssfRow = (HSSFRow)rowIterator.next();
Iterator iterator = hssfRow.cellIterator();
List cellTempList = new ArrayList();
while (iterator.hasNext()) {
HSSFCell hssfCell = (HSSFCell)iterator.next();
cellTempList.add(hssfCell);
}
cellDataList.add(cellTempList);
}
} catch (Exception e) {
e.printStackTrace();
}
/**
* Call the printToConsole method to print the cell data in the
* console.
*/
urlqueryWS1(urlAddr, cellDataList, properties);
}
private static String logon(String wsLocation, String userName,
String password, Properties properties) {
String sessionString = "FAIL";
int port =
Integer.parseInt(properties.getProperty("proxyport")); //Converting String Port Number to Integer.
try {
Proxy proxy =
new Proxy(Proxy.Type.HTTP, new InetSocketAddress(properties.getProperty("proxyhost"),
port));
// create an HTTPS connection to the On Demand webservices
URL wsURL = new URL(wsLocation + "?command=login");
HttpURLConnection wsConnection =
(HttpURLConnection)wsURL.openConnection(proxy);
// we don't want any caching to occur
wsConnection.setUseCaches(false);
// we want to send data to the server
// wsConnection.setDoOutput(true);
// set some http headers to indicate the username and passwod we are using to logon
wsConnection.setRequestProperty("UserName", userName);
wsConnection.setRequestProperty("Password", password);
wsConnection.setRequestMethod("GET");
// see if we got a successful response
if (wsConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
// get the session id from the cookie setting
sessionString = getCookieFromHeaders(wsConnection);
}
} catch (Exception e) {
System.out.println("Logon Exception generated :: " + e);
}
return sessionString;
}
private static String getCookieFromHeaders(HttpURLConnection wsConnection) {
// debug code - display all the returned headers
String headerName;
String headerValue = "FAIL";
for (int i = 0; ; i++) {
headerName = wsConnection.getHeaderFieldKey(i);
if (headerName != null && headerName.equals("Set-Cookie")) {
// found the Set-Cookie header (code assumes only one cookie is being set)
headerValue = wsConnection.getHeaderField(i);
break;
}
}
// return the header value (FAIL string for not found)
return headerValue;
}
private static String getSessionId(String cookie) {
StringTokenizer st = new StringTokenizer(cookie, ";");
String jsessionid = st.nextToken();
st = new StringTokenizer(jsessionid, "=");
st.nextToken();
return st.nextToken();
}
public static void main(String[] args) throws IOException,
ServiceException {
String jsessionid, jsessionid_full;
String endpoint;
AxisProperties.setProperty("https.proxyHost", "172.25.9.240");
AxisProperties.setProperty("https.proxyPort", "2006");
System.setProperty("https.proxyHost", "172.25.9.240");
System.setProperty("https.proxyPort", "2006");
// System.setProperty("https.proxyHost", "145.247.13.164");
// System.setProperty("https.proxyPort", "3128");
Properties properties = new Properties();
properties.load(new FileInputStream("C:\\Users\\10608011\\Documents\\Account_Config.properties")); //Windows Path
System.setProperty("javax.net.ssl.trustStore","C:\\Oracle\\Middleware3\\jdk160_24\\jre\\lib\\security\\cacerts");
System.out.println("Logging In");
jsessionid_full =
logon("https://secure-ausomxapa.crmondemand.com/Services/Integration",
"############", "###########", properties);
System.out.println("Establishing " + jsessionid_full);
jsessionid = getSessionId(jsessionid_full);
System.out.println("Jsessionid: " + jsessionid);
endpoint =
"https://secure-ausomxapa.crmondemand.com/Services/Integration" +
";jsessionid=" + jsessionid;
URL urlAddr = new java.net.URL(endpoint);
String fileName =
"D:" + File.separator + "Test2.xls"; // Windows Path
// String fileName =File.separator + "u01"+File.separator +"CRM_DEV"+File.separator +
// "sofwaredepot"+File.separator +"PALS_200_11Feb2013.xls"; //Linux Path /u01/CRM_DEV/softwaredepot
// String fileName="PALS_200_11Feb2013.xls";
final long start = System.currentTimeMillis();
logger.info("Start Time:" + start);
new accrel1().readExcelFile(urlAddr, fileName, properties);
final long durationInMilliseconds = System.currentTimeMillis() - start;
System.out.println("Time(Min) for Data Upload: " +
durationInMilliseconds / 60000 + "mins.");
logger.info("Duration for Data Upload: " +
durationInMilliseconds / 60000 + "mins.");
}
}