I made java script, it save/load player inventory when he leave/join, but someting goes wrong(GET request dont send)
here is classes.
InventorySync:
import com.google.gson.Gson;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.IOException;
import java.util.Arrays;
import java.util.Base64;
import java.util.UUID;
import InventoryGet.*;
public class InventorySync extends JavaPlugin implements Listener {
String saveInv = "*correct url*";
String loadInv = "*correct url*";
Gson gson = new Gson();
#EventHandler
public void onJoin(PlayerJoinEvent event) throws IOException {
Player player = event.getPlayer();
UUID uuid = player.getUniqueId();
//InventoryGet.main(loadInv + "?username=" + uuid.toString());
}
#EventHandler
public void onQuit(PlayerQuitEvent event) throws IOException {
Player player = event.getPlayer();
UUID uuid = player.getUniqueId();
Object[] contents = Arrays.stream(player.getInventory().getContents()).toArray();
String inv = Base64.getEncoder().encodeToString( gson.toJson(contents).getBytes() );
InventoryGet.main(saveInv + "?username=" + uuid.toString() + "&inventory=" + inv);
}
}
InventoryGet:
package InventoryGet;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
public class InventoryGet {
public static String main(String urlString) throws IOException {
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
Scanner scanner = new Scanner(connection.getInputStream());
String response = scanner.useDelimiter("\\A").next();
scanner.close();
return response;
} else {
return "GET request failed with response code " + responseCode;
}
} catch (Exception e) {
return "Exception occurred: " + e.getMessage();
}
}
}
The URL is correct.
For the PHP script:
Update inventory:
<?php
//Get player nickname
//check username for exist
if (!isset($_GET['username']))
{
die();
} else { //username exist:
$username = $_GET['username'];
}
//get player inventory(if exist)
if (!isset($_GET['inventory'])) {
die();
} else { //decode inventory from base64:
$inventory = base64_decode($_GET['inventory']);
} //save inventory to file
file_put_contents('inventory/' . $username . '.inv', $inventory);
Get inventory:
<?php
header("content-type: application/json");
//Get player nickname
//check username for exist
if (!isset($_GET['username'])) {
die('[]');
} else { // username exist
$username = $_GET['username'];
}
if (file_exists('inventory/' . $username . '.inv')) { //find player's inventory if exists
echo file_get_contents('inventory/' . $username . '.inv');
} else { //player file not found:
echo '[]';
}
When player disconnect from server, server save his inventory, but server not do a GET request
You said it's full code, so there is multiple issues :
You are in the JavaPlugin class, and there isn't onEnable() method that register listener. So, you should add:
#Override
public void onEnable() {
getServer().getPluginManager().registerEvents(this, this);
}
I suggest you to create a new class for listeners, to have (I omitted import):
public class InventorySync extends JavaPlugin {
#Override
public void onEnable() {
getServer().getPluginManager().registerEvents(new ConnectionListener(), this);
}
}
public class ConnectionListener implements Listener {
String saveInv = "*correct url*";
String loadInv = "*correct url*";
Gson gson = new Gson();
#EventHandler
public void onJoin(PlayerJoinEvent event) throws IOException {
Player player = event.getPlayer();
UUID uuid = player.getUniqueId();
//InventoryGet.main(loadInv + "?username=" + uuid.toString());
}
#EventHandler
public void onQuit(PlayerQuitEvent event) throws IOException {
Player player = event.getPlayer();
UUID uuid = player.getUniqueId();
Object[] contents = Arrays.stream(player.getInventory().getContents()).toArray();
String inv = Base64.getEncoder().encodeToString( gson.toJson(contents).getBytes() );
InventoryGet.main(saveInv + "?username=" + uuid.toString() + "&inventory=" + inv);
}
}
You should not use GET request to update datas. The method POST is here.
You are wrongly using gson according to Spigot API. You should use ItemStack#serialize and ItemStack#deserialize, like that:
#EventHandler
public void onJoin(PlayerJoinEvent event) throws IOException {
Player player = event.getPlayer();
UUID uuid = player.getUniqueId();
String inv = main(loadInv + "?username=" + uuid.toString());
List<Map<String, Object>> list = gson.fromJson(inv, List.class);
list.stream().map(ItemStack::deserialize).forEach(it -> {
// here you have items
});
}
#EventHandler
public void onQuit(PlayerQuitEvent event) throws IOException {
Player player = event.getPlayer();
UUID uuid = player.getUniqueId();
List<Map<String, Object>> contents = Arrays.stream(player.getInventory().getContents()).filter(Objects::nonNull).map(ItemStack::serialize).collect(Collectors.toList());
String inv = Base64.getEncoder().encodeToString(gson.toJson(contents).getBytes());
main(saveInv + "?username=" + uuid.toString() + "&inventory=" + inv);
}
I added filter(Objects::nonNull) to prevent empty slot
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 know there are other questions like this but none that I could find are fully helpful to this case.
So basically on the line that says
getCommand("minealchemy").setExecutor(new Commands());
I get this error:
The constructor Commands() is undefined
Any help? When i put "null" in the () for Commands ... Commands(null)... then i get a NullPointerException error...
Here are all of my classes:
Main Class:
package me.zachbears27;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import me.zachbears27.utils.Commands;
public class Main extends JavaPlugin implements Listener {
#Override
public void onDisable() {
}
#Override
public void onEnable() {
getServer().getPluginManager().registerEvents(this, this);
registerConfig();
getCommand("minealchemy").setExecutor(new Commands());
}
public void registerConfig() {
saveDefaultConfig();
}
}
Commands Class:
package me.zachbears27.utils;
import java.io.File;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import me.zachbears27.utils.Methods;
import net.md_5.bungee.api.ChatColor;
public class Commands implements CommandExecutor {
private final Methods methods;
public Commands(Methods methods) {
this.methods = methods;
}
public boolean onCommand(CommandSender sender, Command label, String cmd, String[] args) {
Player p = (Player) sender;
if (cmd.equalsIgnoreCase("minealchemy")) {
if(args.length > 0) {
//Get File
File file = methods.getPlayerFile(p.getUniqueId());
FileConfiguration fileSettings = YamlConfiguration.loadConfiguration(file);
//Make Sure It's False
fileSettings.set("Enabled", false);
//Save Inventory
methods.savePlayerInv(p.getUniqueId(), p.getInventory().getContents());
//Clear It
p.getInventory().clear();
//Put In God Mode
p.setInvulnerable(true);
//Open Inventory
p.openWorkbench(p.getLocation(), true);
} else {
p.sendMessage(ChatColor.RED + "Incorrect Arguments... Please use \"start\" or \"list\".");
}
}
return true;
}
}
Methods Class:
package me.zachbears27.utils;
import java.io.File;
import java.util.ArrayList;
import java.util.UUID;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;
import me.zachbears27.Main;
import net.md_5.bungee.api.ChatColor;
public class Methods {
private final Main plugin;
public Methods(Main plugin) {
this.plugin = plugin;
}
public ArrayList<String> getAllItems() {
ArrayList<String> allItems = new ArrayList<String>();
//Basics
String Air = ChatColor.GREEN + "Air" + ChatColor.RESET;
String Earth = ChatColor.GREEN + "Earth" + ChatColor.RESET;
String Fire = ChatColor.GREEN + "Fire" + ChatColor.RESET;
String Metal = ChatColor.GREEN + "Metal" + ChatColor.RESET;
String Water = ChatColor.GREEN + "Water" + ChatColor.RESET;
String Motion = ChatColor.GREEN + "Motion" + ChatColor.RESET;
String Big = ChatColor.GREEN + "Big" + ChatColor.RESET;
String Time = ChatColor.GREEN + "Time" + ChatColor.RESET;
String Small = ChatColor.GREEN + "Small" + ChatColor.RESET;
allItems.add(Air);
allItems.add(Earth);
allItems.add(Fire);
allItems.add(Metal);
allItems.add(Water);
allItems.add(Motion);
allItems.add(Big);
allItems.add(Time);
allItems.add(Small);
//Animals
String Cat = ChatColor.GOLD + "Cat" + ChatColor.RESET;
allItems.add(Cat);
return allItems;
}
public String addItems(String item1, String item2) {
String returnStatement = ChatColor.RED + "X";
if(item1.equals("Air") && item2.equals("Fire")) {
returnStatement = "Mist";
}
return returnStatement;
}
public File getPlayerFile(UUID playerUUID) {
//Creates The Player File
File playerFile = new File (plugin.getDataFolder() + File.separator + "Player Data", playerUUID + ".yml");
return playerFile;
}
public void savePlayerInv(UUID playerUUID, ItemStack[] inv) {
File playerFile = getPlayerFile(playerUUID);
FileConfiguration playerData = YamlConfiguration.loadConfiguration(playerFile);
playerData.set("SavedInventory", inv);
}
}
Change the .setExecutor(new Commands()) in the onEnable() function to
.setExecutor(new Commands(new Methods(this)))
I've a program, which has only Driver and Mapper class. I am not using Reducer class.
In driver class, I'm reading a file in S3bucket and in the Mapper class. I'm writing a file in S3bucket through normal java code like(aws java sdk) and not by context.write.
I have 1000 json files. When I ran the program, the driver class gets the file and mapper class is writing each file in the s3 bucket. For me it takes a maximum 2 seconds to write a single file but I want to write minimum 100 files within 2 seconds.
How can I achieve this? Please suggest me some solutions.
Service call class:
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.methods.GetMethod;
import org.elasticsearch.client.Client;
public class ClcConstants {
public static Client client = null;
// Variable Declarations for mapping the field in input file
public static String ContactId = "contactid";
public static String PostalCode = "PostalCode";
public static String Email = "Email";
public static String DateFormat = "dd/MM/yyyy HH:mm";
public static String Date = "dd/MM/yyyy";
// WebService MethodExecution
public static String executePost(String url) {
System.out.println("Started to connect webservices...");
HttpClient httpClient = new HttpClient();
/*
* Credentials credentials = new NTCredentials("myusername",
* "mypassword", "myhost", "mydomain");
* httpClient.getState().setCredentials(AuthScope.ANY, credentials);
*/
System.out.println("Accessing webservices...");
HttpMethodBase method = new GetMethod(url);
try {
int returnCode = httpClient.executeMethod(method);
System.out.println("returnCode: " + returnCode);
String response = method.getResponseBodyAsString();
// response
// ="{\"GetAllSourceWeightageResult\":[{\"ClientKey\":\"L4CTRsto\",\"Weightages\":[{\"Source\":\"DI\",\"Weight\":1},{\"Source\":\"ER\",\"Weight\":2},{\"Source\":\"IM\",\"Weight\":3},{\"Source\":\"CU\",\"Weight\":4}]}]}";
System.out.println("Response: " + response);
return response;
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
}
Driver Class:
import java.util.Date;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputFormat;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.NullOutputFormat;
import org.json.JSONArray;
import org.json.JSONObject;
public class ClcDriver {
// Globally initialized in order to access outside the method
public static String client_key;
public static String folder_name;
public static void main(String[] args) throws Exception {
// Arguments to be passed dynamically
String dbType = args[0];
String serviceUrl = args[1];
String input_path = args[2];
client_key = args[3];
folder_name = args[4];
Date date = new Date();
String jobstarttime = String.format("Current Date/Time : %tc", date);
System.out.println(jobstarttime);
String url = serviceUrl + "/GetCLCConfiguration/?clientKey="
+ client_key;
System.out.println("GetCLCConfiguration from Service");
String responseText = ClcConstants.executePost(url);
System.out.println("End GetCLCConfiguration from Service");
// Convert the accessed string to JsonObject.
JSONObject json_data = new JSONObject(responseText);
// Converting into JsonArray in order to process in the loop
JSONArray array = (JSONArray) json_data
.get("GetCLCConfigurationResult");
// If the argument passed as "amazonS3", the below method gets executed
if (dbType.equals("amazonS3")) {
amazonS3(array, input_path);
}
}
// Passing the GetCLCConfigurationResults and input path of S3 Bucket
public static void amazonS3(JSONArray array, String input_path)
throws Exception {
for (int i = 0; i < array.length(); i++) {
System.out.println("***********");
JSONObject innerObj = (JSONObject) array.get(i);
String clientName = innerObj.get("ClientKey").toString();
String data = innerObj.get("Configurations").toString();
System.out.println("Setting Configuration...");
Configuration conf = new Configuration();
System.out.println("Configuration done");
System.out.println("Accessing s3bucket...");
conf.set("Configurations", data);
conf.set("ClientKey", clientName);
conf.set("fs.s3n.awsAccessKeyId", "myaccesskey");
conf.set("fs.s3n.awsSecretAccessKey",
"mysecret access key");
System.out.println("Accessed.");
System.out.println("Setting Job...");
Job job = Job.getInstance(conf, "JobName");
System.out.println("Job assigned");
job.setJarByClass(ClcDriver.class);
job.setMapperClass(ClcMapper.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path(input_path + client_key
+ "/" + folder_name + "/"));
FileOutputFormat.setOutputPath(job, new Path(input_path + client_key
+ "/" + folder_name + "/" + "output" + "/"));
if (!job.waitForCompletion(true))
return;
// Calculating Job Completed Time
Date date = new Date();
String jobcompletedtime = String
.format("Current Date/Time : %tc", date);
System.out.println(jobcompletedtime);
}
}
}
Mapper Class:
public class ClcMapper extends Mapper<Object, Text, Text, Text> {
private static String bucketName = "BucketName";
private static String keyName = ClcDriver.client_key + "/"
+ ClcDriver.folder_name + "/";
public static AmazonS3 s3client;
public String jsonobjectconvertstring;
public InputStream writecontentins3;
int val;
// Called once at the beginning of the task
protected void setup(Context context) throws IOException,
InterruptedException {
System.out.println("Mapper Started");
System.out.println("Accessing s3bucket once again...");
s3client = new AmazonS3Client(new BasicAWSCredentials(
"Myaccesskey",
"mysecretaccesskey"));
System.out.println("Accessed.");
System.out.println("Setting Region...");
Region region = Region.getRegion(Regions.US_WEST_2);
s3client.setRegion(region);
s3client.setEndpoint("s3-us-west-2.amazonaws.com");
System.out.println("Region was successfully set.");
// GetCLCConfiguration results from Driver class
//Configuration conf = context.getConfiguration();
//String data = conf.get("Configurations");
}
// Processing Mapper for each contact...
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
boolean MemberOfAnyContactGroup = true;
String line = value.toString();
try {
JSONObject contacts = new JSONObject(line);
// Updating the CLC field
System.out.println(contacts.put("CLC", val).toString());
context.write(new Text(contacts.toString()),new Text());
} catch (Exception e) {
System.out.println(e);
}
}
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?