Accept code improvement here [2904905] - Bad code in MSequence.java

https://sourceforge.net/tracker/?func=detail&atid=879332&aid=2904905&group_id=176962
thanks to Gabriel and Zoli
This commit is contained in:
Carlos Ruiz 2009-12-08 16:09:58 +00:00
parent bc3add14a7
commit 5df3846db8
1 changed files with 8 additions and 23 deletions

View File

@ -17,9 +17,7 @@
package org.compiere.model; package org.compiere.model;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStreamWriter; import java.net.HttpURLConnection;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.URL; import java.net.URL;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.sql.CallableStatement; import java.sql.CallableStatement;
@ -1476,7 +1474,7 @@ public class MSequence extends X_AD_Sequence
String website, String prm_USER, String prm_PASSWORD, String website, String prm_USER, String prm_PASSWORD,
String prm_TABLE, String prm_ALTKEY, String prm_COMMENT, String prm_TABLE, String prm_ALTKEY, String prm_COMMENT,
String prm_PROJECT) { String prm_PROJECT) {
StringBuffer read = new StringBuffer(""); StringBuffer read = new StringBuffer();
int retValue = -1; int retValue = -1;
try { try {
String completeUrl = website + "?" + "USER=" String completeUrl = website + "?" + "USER="
@ -1493,34 +1491,21 @@ public class MSequence extends X_AD_Sequence
String protocol = url.getProtocol(); String protocol = url.getProtocol();
if (!protocol.equals("http")) if (!protocol.equals("http"))
throw new IllegalArgumentException("URL must use 'http:' protocol"); throw new IllegalArgumentException("URL must use 'http:' protocol");
String host = url.getHost(); HttpURLConnection conn = (HttpURLConnection)url.openConnection();
int port = url.getPort(); conn.setRequestMethod("GET");
if (port == -1) port = 80; // if no port, use the default HTTP port conn.setAllowUserInteraction(false);
String filename = url.getFile(); InputStream is = conn.getInputStream();
// Open a network socket connection to the specified host and port
Socket socket = new Socket(host, port);
// Get input and output streams for the socket
InputStream from_server = socket.getInputStream();
PrintWriter to_server =
new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
// Send the HTTP GET command to the Web server, specifying the file.
// This uses an old and very simple version of the HTTP protocol
to_server.println("GET " + filename);
to_server.flush(); // Send it right now!
// Now read the server's response, and write it to the file // Now read the server's response, and write it to the file
byte[] buffer = new byte[4096]; byte[] buffer = new byte[4096];
int bytes_read; int bytes_read;
while((bytes_read = from_server.read(buffer)) != -1) { while((bytes_read = is.read(buffer)) != -1) {
for (int i = 0; i < bytes_read; i++) { for (int i = 0; i < bytes_read; i++) {
if (buffer[i] != 10) if (buffer[i] != 10)
read.append((char) buffer[i]); read.append((char) buffer[i]);
} }
} }
conn.disconnect();
// When the server closes the connection, we close our stuff
socket.close();
retValue = Integer.parseInt(read.toString()); retValue = Integer.parseInt(read.toString());
if (retValue <= 0) if (retValue <= 0)
retValue = -1; retValue = -1;