📄 serveruploader.java
字号:
/*
* MC2 -- j2me spreadsheet
*
* Copyright (c) 2004-2006 Michael Zemljanukha (mixaz@mail.ru)
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.wapindustrial.calc;
import javax.lang.*;
import javax.io.*;
//#ifndef NOGUI
import javax.microedition.io.*;
//#endif
/**
* @author mzemlyan
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ServerUploader extends FunctionModule {
public static final ServerUploader SERVERUPLOADER = new ServerUploader();
/** Creates a new instance of ServerUploader */
public ServerUploader() {
}
static final byte[] _namesArray = {
'\11','h','t','t','p','-','l','o','a','d',
'\13','h','t','t','p','-','u','p','l','o','a','d'
};
public static final int NAME_HTTP_LOAD = 0;
public static final int NAME_HTTP_UPLOAD = 10;
public static final int INDEX_HTTP_LOAD = 0;
public static final int INDEX_HTTP_UPLOAD = 1;
/* (non-Javadoc)
* @see com.wapindustrial.calc.FunctionModule#initializeNames()
*/
public void initializeNames() {
namesArray = _namesArray;
namesArrayCount = namesArray.length;
table = new ModuleName[] {
new ModuleName(NAME_HTTP_LOAD),
new ModuleName(NAME_HTTP_UPLOAD)
};
namesCount = table.length;
}
/* (non-Javadoc)
* @see com.wapindustrial.calc.FunctionModule#getModuleClassName()
*/
protected String getModuleClassName() {
return "ServerUploader";
}
/* (non-Javadoc)
* @see com.wapindustrial.calc.FunctionModule#evaluate(com.wapindustrial.calc.FunctionModule.ModuleName, com.wapindustrial.calc.FunctorList)
*/
//#ifndef NOGUI
public LispObject evaluate(ModuleName modulename, FunctorList sexp) throws EvaluateException {
int size = sexp.listSize();
int optype = modulename.offset;
try {
switch( optype ) {
/** (http-load url:string) -> string */
case NAME_HTTP_LOAD:
return new StringAtom( processGetRequest(sexp.getString1()) );
case NAME_HTTP_UPLOAD:
try {
/** (http-upload url:string login:string password:string server-side-code:string/sexp) -> string */
LispObject code = sexp.evaluateArgN(4);
String codeText;
if(code.typeNumber() == TYPE_STRING)
codeText = ((StringAtomBase)code).getValue();
else
codeText = code.toString();
return LispObject.parseSExp(
processPostRequest(
sexp.getString1(),
sexp.getString2(),
sexp.getString3(),
codeText)
).evaluateSExp();
}
catch (ParseException e) {
throw new EvaluateException("Can't parse server reply: "+e.getMessage());
}
}
}
catch (IOException e) {
throw new EvaluateException("Error processing HTTP request: "+e.getMessage());
}
return NIL;
}
private static void setConnectionProperties(HttpConnection c, int length) throws IOException {
CanvasHandler1 canvasHandler = CanvasHandler1.canvasHandler;
c.setRequestProperty("User-Agent",canvasHandler.getAppProperty("MIDlet-Name") + ' ' + canvasHandler.getAppProperty("MIDlet-Version") + " (" +
//#ifndef NO_PROPERTY
System.getProperty("microedition.platform")
//#else
//# "#applet#"
//#endif
+ ")" );
c.setRequestProperty("Content-Language", "en-US");
c.setRequestProperty("Connection", "close");
}
private static String processPostRequest( String url, String login, String password, String code ) throws IOException {
HttpConnection c = null;
InputStream is = null;
OutputStream os = null;
StringBuffer sb = new StringBuffer();
debug("code to server: "+code);
try {
sb.append( "USR=" );
appendUrlEncoded( sb, login );
sb.append( "&PWD=" );
appendUrlEncoded( sb, password );
sb.append( "&CODE=" );
appendUrlEncoded( sb, code );
c = (HttpConnection)Connector.open(url);
// Set the request method and headers
c.setRequestMethod(HttpConnection.POST);
setConnectionProperties(c, sb.length());
c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
c.setRequestProperty("Content-Length",String.valueOf(sb.length()));
os = c.openOutputStream();
os.write( sb.toString().getBytes() ); // to do - encode UTF-8 here (or in appendUrlEncoded?)
// Opening the InputStream will open the connection
// and read the HTTP headers. They are stored until
// requested.
os.close(); // to avoid a bug in Nokia MIDP implementation? (from forum.nokia.com posts)
os = null;
is = c.openInputStream();
sb.setLength( 0 ); // clear the buffer
int len = (int)c.getLength();
/* if (len > 0) {
byte[] buf = new byte[len];
int actual = is.read(buf);
sb.append( new String(buf) );
} else {*/
int ch;
while ((ch = is.read()) != -1)
// if( canceled )
// break;
// else
sb.append( (char) ch );
} finally {
try {
if (is != null) is.close();
if (os != null) os.close();
if (c != null) c.close();
}
catch( IOException ex ) {
ex.printStackTrace();
}
}
debug("server reply: "+sb.toString());
return sb.toString();
}
private static String processGetRequest( String url ) throws IOException {
HttpConnection c = null;
InputStream is = null;
CanvasHandler1 canvasHandler = CanvasHandler1.canvasHandler;
StringBuffer sb = new StringBuffer();
try {
c = (HttpConnection)Connector.open(url);
// Set the request method and headers
c.setRequestMethod(HttpConnection.GET);
setConnectionProperties(c, sb.length());
is = c.openInputStream();
int len = (int)c.getLength();
/* if (len > 0) {
byte[] buf = new byte[len];
int actual = is.read(buf);
sb.append( new String(buf) );
} else {*/
int ch;
while ((ch = is.read()) != -1)
// if( canceled )
// break;
// else
sb.append( (char) ch );
} finally {
try {
if (is != null) is.close();
if (c != null) c.close();
}
catch( IOException ex ) {
ex.printStackTrace();
}
}
return sb.toString();
}
//#endif
private static void appendUrlEncoded( StringBuffer sb, String ss ) {
int n = ss.length();
for( int i=0; i<n; i++ ) {
char cc = ss.charAt(i);
switch( cc ) {
case ' ':
case '\n':
case '\r':
case '&':
case '=':
case '%':
case '+':
case '\t':
sb.append( '%' );
if( cc < 0x10 ) sb.append( '0' );
sb.append( Integer.toHexString( (int) cc ) );
break;
default:
sb.append( cc );
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -