I've intentionally copy-pasted this question dont be angry for duplicate.
There are several unclear moments for me in the topic:
1)
xmpp.login(apiKey + "|" + sessionKey, sessionSecret, "Application");
sessionKey is the second part of the access token. If the token is in
this form, AAA|BBB|CCC, the BBB is the session key
But my access token looks like: BAADcfjCWMLABAIyzRSZA69eAtA9Dr3EQVlXA8Ql6rr5odDWxNYZCHhssiaar8S0gaPLZAm1ZBKCqWO3QFegJPR39hT0JR5ZCyIP1AJZC19qh9mFAExUd9KDjJ05yjE3IUZD
so I cant see any "second part"...
2)
sessionSecret is obtained using the old REST API with the method
auth.promoteSession. To use it, it's needed to make a Http get to this
url:
https://api.facebook.com/method/auth.promoteSession?access_token=yourAccessToken
Despite of the Facebook Chat documentation says that it's needed to
use your application secret key, only when I used the key that
returned that REST method I was able to make it works. To make that
method works, you have to disable the Disable Deprecated Auth Methods
option in the Advance tab in your application settings.
I've read here that REST is deprecated and
We will not be supporting this method in the Graph API.
What should I do? I'm using only Graph API. Is there any other way to get sessionSecret?
Thanks!
I tested this one and it worked for me .
First of all Edit your SASLXFacebookPlatformMechanism class . Copy and paste this code .
package com.facebook.android;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;
import org.apache.harmony.javax.security.auth.callback.CallbackHandler;
import org.apache.harmony.javax.security.sasl.Sasl;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.sasl.SASLMechanism;
import org.jivesoftware.smack.util.Base64;
import android.util.Log;
public class SASLXFacebookPlatformMechanism extends SASLMechanism {
private static final String NAME = "X-FACEBOOK-PLATFORM";
private String apiKey = "";
private String accessToken = "";
/**
* Constructor.
*/
public SASLXFacebookPlatformMechanism(SASLAuthentication saslAuthentication) {
super(saslAuthentication);
}
#Override
protected void authenticate() throws IOException, XMPPException {
getSASLAuthentication().send(new AuthMechanism(NAME, ""));
}
#Override
public void authenticate(String apiKey, String host, String accessToken) throws IOException, XMPPException {
if (apiKey == null || accessToken == null) {
throw new IllegalArgumentException("Invalid parameters");
}
this.apiKey = apiKey;
this.accessToken = accessToken;
this.hostname = host;
String[] mechanisms = { "DIGEST-MD5" };
Map<String, String> props = new HashMap<String, String>();
this.sc = Sasl.createSaslClient(mechanisms, null, "xmpp", host, props, this);
authenticate();
}
#Override
public void authenticate(String username, String host, CallbackHandler cbh) throws IOException, XMPPException {
String[] mechanisms = { "DIGEST-MD5" };
Map<String, String> props = new HashMap<String, String>();
this.sc = Sasl.createSaslClient(mechanisms, null, "xmpp", host, props, cbh);
authenticate();
}
#Override
protected String getName() {
return NAME;
}
#Override
public void challengeReceived(String challenge) throws IOException {
byte[] response = null;
if (challenge != null) {
String decodedChallenge = new String(Base64.decode(challenge));
Map<String, String> parameters = getQueryMap(decodedChallenge);
String version = "1.0";
String nonce = parameters.get("nonce");
String method = parameters.get("method");
String composedResponse =
"method=" + URLEncoder.encode(method, "utf-8") +
"&nonce=" + URLEncoder.encode(nonce, "utf-8") +
"&access_token=" + URLEncoder.encode(accessToken, "utf-8") +
"&api_key=" + URLEncoder.encode(apiKey, "utf-8") +
"&call_id=0" +
"&v=" + URLEncoder.encode(version, "utf-8");
response = composedResponse.getBytes();
}
String authenticationText = "";
if (response != null) {
authenticationText = Base64.encodeBytes(response);
}
// Send the authentication to the server
getSASLAuthentication().send(new Response(authenticationText));
}
private Map<String, String> getQueryMap(String query) {
Map<String, String> map = new HashMap<String, String>();
String[] params = query.split("\\&");
for (String param : params) {
String[] fields = param.split("=", 2);
map.put(fields[0], (fields.length > 1 ? fields[1] : null));
}
return map;
}
}
Then use this method in your activity class from where you want to login to facebook.
private void testLogin(){
ConnectionConfiguration config = new ConnectionConfiguration("chat.facebook.com", 5222);
config.setSASLAuthenticationEnabled(true);
config.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);
xmpp = new XMPPConnection(config);
SASLAuthentication.registerSASLMechanism("X-FACEBOOK-PLATFORM",SASLXFacebookPlatformMechanism.class);
SASLAuthentication.supportSASLMechanism("X-FACEBOOK-PLATFORM", 0);
Log.i("XMPPClient",
"Access token to " + mFacebook.getAccessToken());
Log.i("XMPPClient",
"Access token to " + mFacebook.getAppId());
Log.i("XMPPClient",
"Access token to " + mFacebook.getAccessToken());
try {
xmpp.connect();
Log.i("XMPPClient",
"Connected to " + xmpp.getHost());
} catch (XMPPException e1) {
Log.i("XMPPClient",
"Unable to " + xmpp.getHost());
e1.printStackTrace();
}
try {
xmpp.login(PreferenceConnector.APP_ID, mFacebook.getAccessToken());
getRoster(xmpp);
} catch (XMPPException e) {
e.printStackTrace();
}
}
Related
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
}
I have a server and a client that sends requests. I don't need to identify the requests when it works in synch mode. But in asynch mode each request must have an identificator, so I could be sure that the response corresponds to exact request. The server is not to be updated, I have to put the identificator in the client's code. Is there a way do do it? I can't find out any.
Here is my main class. I guess all must be clear, the class is very simple.
public class MainAPITest {
private static int userId = 0;
private final static int NUM_OF_THREADS = 10;
private final static int NUM_OF_USERS = 1000;
public static void main(String[] args) {
Security.addProvider(new GammaTechProvider());
ExecutorService threadsExecutor = Executors.newFixedThreadPool(NUM_OF_THREADS);
for (int i = 0; i < NUM_OF_USERS; i++) {
MyRunnable user = new MyRunnable();
userId++;
user.uId = userId;
threadsExecutor.execute(user);
}
threadsExecutor.shutdown();
}
}
class MyRunnable implements Runnable {
int uId;
#Override
public void run() {
try {
abstrUser user = new abstrUser();
user.setUserId(uId);
user.registerUser();
user.chooseAndImplementTests();
user.revokeUser();
} catch (Exception e) {
e.printStackTrace();
}
}
}
User class describes the user's behaviour. It is long enough, idk if it is needed here. User runs a number of random tests. Each test has its own class that extends abstract test class, where the http connection is established:
import org.json.JSONObject;
import javax.xml.bind.DatatypeConverter;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
public abstract class abstractRESTAPITest {
protected String apiUrl;
public abstractRESTAPITest() {
}
protected byte[] sendRequest(JSONObject jsonObject) throws IOException {
return this.sendRequest(jsonObject, (String) null, (String) null);
}
protected byte[] sendRequest(JSONObject jsonObject, String username, String password) throws IOException {
HttpURLConnection httpURLConnection = (HttpURLConnection) (new URL(this.apiUrl)).openConnection();
if (username != null && password != null) {
String userPassword = username + ":" + password;
httpURLConnection.setRequestProperty("Authorization", "Basic " + DatatypeConverter.printBase64Binary(userPassword.getBytes()));
}
httpURLConnection.setRequestProperty("Content-Type", "application/json");
httpURLConnection.setDoOutput(true);
DataOutputStream dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream());
dataOutputStream.write(jsonObject.toString().getBytes());
dataOutputStream.flush();
System.out.println("REST send: " + jsonObject.toString());
if (httpURLConnection.getResponseCode() != 200) {
System.out.println("REST send error, http code " + httpURLConnection.getResponseCode() + " " + httpURLConnection.getResponseMessage());
throw new IOException();
} else {
byte[] responseBody = null;
StringBuilder data = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
String line = "";
while ((line = br.readLine()) != null) {
data.append(line);
responseBody = data.toString().getBytes();
}
if (br != null) {
br.close();
}
return responseBody;
}
}
public abstract boolean test();
}
Now I am trying to transform the httpUrlConnection part of the code into a kind of this.
Jsonb jsonb = JsonbBuilder.create();
var client = HttpClient.newHttpClient();
var httpRequest = HttpRequest.newBuilder()
.uri(new URI(apiUrl))
.version(HttpClient.Version.HTTP_2)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(jsonb.toJson(jsonObject)))
.build();
client.sendAsync(httpRequest, HttpResponse.BodyHandlers.ofString());
It must send a JSON request and receive JSON response. HttpClient, introduced in Java11, has sendAsync native method, so I try to use it. But I don't understand fully how it works, so I have no success.
The Microsoft Translator API for android samples has already been deprecated since march 2017 (just this yr). So I'm having a hard time translating text on android. Can anyone help me make this work on android?. It's just that I already have this working in java but I can't make it work on Android. I already tried using asynctask , but to no avail, no translation is being outputed. Or prolly I just did a wrong asyc task?.
The Interface of the application is like this:
It just have a simple textfield with translation that should be outputed on the other textfield. The translation is from Korean to English.
Github Project File is in here
https://github.com/iamjoshuabcxvii/MSTranslateAPIforAndroid
Android code is this.
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import org.apache.commons.io.IOUtils;
import java.net.URL;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.net.ssl.HttpsURLConnection;
public class MainActivity extends AppCompatActivity {
//MS Translator Key is in here
public static String key = "<The MS Assigned Translator Key>";
private TextView txtTranslatedText, txtOriginalText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtOriginalText = findViewById(R.id.txtOriginalText);
txtTranslatedText = findViewById(R.id.txtTranslatedText);
}
public void translate(View view) {
// String textOrig = txtOriginalText.getText().toString();
// String textOrig ="안녕하세요 123 -)^ 친구";
String textOrig;
textOrig=txtOriginalText.getText().toString();
String output;
output=getTranslation(textOrig);
txtTranslatedText.setText(output);
}
public static String getTranslation(String translatedTextStr) {
try {
// Get the access token
// The key got from Azure portal, please see https://learn.microsoft.com/en-us/azure/cognitive-services/cognitive-services-apis-create-account
String authenticationUrl = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken";
HttpsURLConnection authConn = (HttpsURLConnection) new URL(authenticationUrl).openConnection();
authConn.setRequestMethod("POST");
authConn.setDoOutput(true);
authConn.setRequestProperty("Ocp-Apim-Subscription-Key", key);
IOUtils.write("", authConn.getOutputStream(), "UTF-8");
String token = IOUtils.toString(authConn.getInputStream(), "UTF-8");
// System.out.println(token); //Code to Display Token
// Using the access token to build the appid for the request url
String appId = URLEncoder.encode("Bearer " + token, "UTF-8");
String text = URLEncoder.encode(translatedTextStr, "UTF-8");
String from = "ko";
String to = "en";
String translatorTextApiUrl = String.format("https://api.microsofttranslator.com/v2/http.svc/GetTranslations?appid=%s&text=%s&from=%s&to=%s&maxTranslations=5", appId, text, from, to);
HttpsURLConnection translateConn = (HttpsURLConnection) new URL(translatorTextApiUrl).openConnection();
translateConn.setRequestMethod("POST");
translateConn.setRequestProperty("Accept", "application/xml");
translateConn.setRequestProperty("Content-Type", "text/xml");
translateConn.setDoOutput(true);
String TranslationOptions = "<TranslateOptions xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\">" +
"<Category>general</Category>" +
"<ContentType>text/plain</ContentType>" +
"<IncludeMultipleMTAlternatives>True</IncludeMultipleMTAlternatives>" +
"<ReservedFlags></ReservedFlags>" +
"<State>contact with each other</State>" +
"</TranslateOptions>";
translateConn.setRequestProperty("TranslationOptions", TranslationOptions);
IOUtils.write("", translateConn.getOutputStream(), "UTF-8");
String resp = IOUtils.toString(translateConn.getInputStream(), "UTF-8");
// System.out.println(resp+"\n\n");
String s=resp;
Pattern assign_op=Pattern.compile("(<TranslatedText>)"
+ "|(<\\/TranslatedText>)"
+ "|[()\\\\[\\\\]{};=#.,'\\\\^:#!$%&_`*-<>]"
+ "|[a-zA-Z0-9\\s]*"
+ "");
Matcher m = assign_op.matcher(s) ;
String actualTranslation="";
Boolean endOfTransTxt=false,startOfTransTxt=false,concat=false;
String foundRegexStr="",tempStr="";
while (m.find()) {
foundRegexStr=m.group();
if(m.group().matches("(<TranslatedText>)")) {
startOfTransTxt=true;
}
else if(m.group().matches("(<\\/TranslatedText>)")) {
endOfTransTxt=true;
concat=false;
}
else{
startOfTransTxt=false;
endOfTransTxt=false;
}
if(startOfTransTxt==true) {
concat=true;
}
else if(concat==true) {
tempStr=tempStr+""+m.group();
}
else {
}
}
// System.out.println("\nTranslated Text: "+tempStr);
translatedTextStr=tempStr;
} catch (Exception e) {
e.printStackTrace();
}
return translatedTextStr;
}
}
I think you did a wrong AsyncTask.
You can do it like this:
private class TransAsynTask extends AsyncTask<String, Void, String>{
#Override
protected String doInBackground(String... strings) {
String output = getTranslation(strings[0]);
return output;
}
#Override
protected void onPostExecute(String s) {
txtTranslatedText.setText("Output: " + s);
super.onPostExecute(s);
}
}
And in the onCreate you call:
String textOrig;
textOrig = txtOriginalText.getText().toString();
new TransAsynTask().execute(textOrig);
/*String output;
output = getTranslation(textOrig);
txtTranslatedText.setText("Translated Text: " + output);*/
We're using the goole-client-api library to verify google accounts from our backend. This implementation runs on Google Cloud Platform (GCP) using App Engine and DataStore.
What we're seeing so far is for the GoogleIdTokenVerifier to work but only returns email and uid along with token signature.
The token and uid used does return all profile infos when run against our servlet but does not through our App Engine Endpoint.
Here is the code used:
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdTokenVerifier;
import com.google.api.client.extensions.appengine.http.UrlFetchTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Collections;
import java.util.logging.Logger;
public class GoogleVerifier implements TokenVerifier {
final Logger logger = Logger.getLogger(GoogleVerifier.class.getName());
private static GoogleVerifier instance = null;
private String privAppId;
private UrlFetchTransport httpTransport; //library required to run on GCP
private JsonFactory jsonFactory;
private GoogleVerifier() {
}
private static GoogleVerifier getInstance() {
if (instance == null) {
instance = new GoogleVerifier();
}
return instance;
}
public static void setAppId(String appId) {
getInstance().setPrivAppId(appId);
getInstance().setHttpTransport(new UrlFetchTransport());
getInstance().setJsonFactory(new JacksonFactory());
}
public static String[] verify(String token, String uid) {
return getInstance().verifyPrivate(token, uid);
}
public String[] verifyPrivate(String token, String uid) {
#SuppressWarnings("unused")
GoogleCredential credential = new GoogleCredential().setAccessToken(token);
GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(httpTransport, jsonFactory)
.setAudience(Collections.singletonList(privAppId))
.build();
String[] payloadInfo = new String[5];
try {
GoogleIdToken idToken = verifier.verify(token);
if (idToken != null) {
GoogleIdToken.Payload payload = idToken.getPayload();
if (payload.getSubject().equals(uid)) {
logger.info("Matching google id: " + uid);
payloadInfo[0] = payload.getSubject();
payloadInfo[1] = payload.get("given_name").toString();
payloadInfo[2] = payload.get("family_name").toString();
payloadInfo[3] = payload.get("picture").toString();
payloadInfo[4] = payload.getEmail();
return payloadInfo;
} else {
logger.info("Mismatching google id: " + uid);
return payloadInfo;
}
}
}
catch (Exception e) {
e.printStackTrace();
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
logger.warning(sw.toString());
return payloadInfo;
}
return payloadInfo;
}
private void setPrivAppId(String appId) {
this.privAppId = appId;
}
private void setHttpTransport(UrlFetchTransport httpTransport) {
this.httpTransport = httpTransport;
}
private void setJsonFactory(JsonFactory jsonFactory) {
this.jsonFactory = jsonFactory;
}
}
Here is our App Engine Endpoint:
#ApiMethod(name = "loginSocial", path = "loginSocial", httpMethod = HttpMethod.PUT)
public Response loginSocial(#Named("token") String token,
#Named("uid") String uid,
#Named("socialWebSite") SOCIALWEBSITE socialWebSite,
HttpServletRequest request) throws DatabaseException, IOException {
Response response = new Response();
//make sure parameters and not null or empty
if (token != null && uid != null && socialWebSite != null &&
!token.trim().isEmpty() && !uid.trim().isEmpty()){
String [] userInfo = new String[5];
//validate token and retrieve info first
if (socialWebSite.equals(SOCIALWEBSITE.GOOGLE)){
GoogleVerifier.setAppId(APP_ID);
userInfo = GoogleVerifier.verify(token, uid);
}else if(socialWebSite.equals(APP_ID);
userInfo = FacebookVerifier.verify(token, uid);
}
}
}
Thanks!
I ended up using a different library which is much simpler and provided the same information.
https://stackoverflow.com/questions/22516693