-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from cwisniew/client-server-merge
- Loading branch information
Showing
45 changed files
with
2,045 additions
and
377 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/main/java/net/rptools/clientserver/ActivityListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* This software Copyright by the RPTools.net development team, and | ||
* licensed under the Affero GPL Version 3 or, at your option, any later | ||
* version. | ||
* | ||
* MapTool Source Code 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. | ||
* | ||
* You should have received a copy of the GNU Affero General Public | ||
* License * along with this source Code. If not, please visit | ||
* <http://www.gnu.org/licenses/> and specifically the Affero license | ||
* text at <http://www.gnu.org/licenses/agpl.html>. | ||
*/ | ||
package net.rptools.clientserver; | ||
|
||
/** | ||
* @author drice | ||
* <p>TODO To change the template for this generated type comment go to Window - Preferences - | ||
* Java - Code Style - Code Templates | ||
*/ | ||
public interface ActivityListener { | ||
public static enum Direction { | ||
Inbound, | ||
Outbound | ||
}; | ||
|
||
public static enum State { | ||
Start, | ||
Progress, | ||
Complete | ||
}; | ||
|
||
public static final int CHUNK_SIZE = 4 * 1024; | ||
|
||
public void notify( | ||
Direction direction, State state, int totalTransferSize, int currentTransferSize); | ||
} |
57 changes: 57 additions & 0 deletions
57
src/main/java/net/rptools/clientserver/hessian/AbstractMethodHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* This software Copyright by the RPTools.net development team, and | ||
* licensed under the Affero GPL Version 3 or, at your option, any later | ||
* version. | ||
* | ||
* MapTool Source Code 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. | ||
* | ||
* You should have received a copy of the GNU Affero General Public | ||
* License * along with this source Code. If not, please visit | ||
* <http://www.gnu.org/licenses/> and specifically the Affero license | ||
* text at <http://www.gnu.org/licenses/agpl.html>. | ||
*/ | ||
package net.rptools.clientserver.hessian; | ||
|
||
import com.caucho.hessian.io.HessianInput; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.zip.GZIPInputStream; | ||
|
||
/** | ||
* @author drice | ||
* <p>TODO To change the template for this generated type comment go to Window - Preferences - | ||
* Java - Code Style - Code Templates | ||
*/ | ||
public abstract class AbstractMethodHandler implements MethodHandler { | ||
|
||
/* (non-Javadoc) | ||
* @see clientserver.simple.MessageHandler#handleMessage(java.lang.String, byte[]) | ||
*/ | ||
public void handleMessage(String id, byte[] message) { | ||
try { | ||
|
||
HessianInput in = null; | ||
try { | ||
in = | ||
HessianUtils.createSafeHessianInput( | ||
new GZIPInputStream(new ByteArrayInputStream(message))); | ||
} catch (IOException ioe) { | ||
in = HessianUtils.createSafeHessianInput(new ByteArrayInputStream(message)); | ||
} | ||
in.startCall(); | ||
List<Object> arguments = new ArrayList<Object>(); | ||
while (!in.isEnd()) { | ||
arguments.add(in.readObject()); | ||
} | ||
in.completeCall(); | ||
|
||
handleMethod(id, in.getMethod(), arguments.toArray()); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
src/main/java/net/rptools/clientserver/hessian/HessianSecurity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* This software Copyright by the RPTools.net development team, and | ||
* licensed under the Affero GPL Version 3 or, at your option, any later | ||
* version. | ||
* | ||
* MapTool Source Code 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. | ||
* | ||
* You should have received a copy of the GNU Affero General Public | ||
* License * along with this source Code. If not, please visit | ||
* <http://www.gnu.org/licenses/> and specifically the Affero license | ||
* text at <http://www.gnu.org/licenses/agpl.html>. | ||
*/ | ||
package net.rptools.clientserver.hessian; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class HessianSecurity { | ||
private final Set<String> allowed; | ||
private final Set<String> denied; | ||
|
||
public HessianSecurity() { | ||
|
||
Set<String> allow = new HashSet<>(); | ||
// Safe java.lang stuff | ||
allow.add("java.lang.Boolean"); | ||
allow.add("java.lang.Byte"); | ||
allow.add("java.lang.Character"); | ||
allow.add("java.lang.Double"); | ||
allow.add("java.lang.Float"); | ||
allow.add("java.lang.Long"); | ||
allow.add("java.lang.Short"); | ||
allow.add("java.lang.String"); | ||
|
||
allow.add("java.awt.geom.*"); | ||
allow.add("sun.awt.geom.*"); | ||
allow.add("java.awt.BasicStroke"); | ||
|
||
allow.add("net.rptools.maptool.client.walker.*"); | ||
allow.add("net.rptools.maptool.common.*"); | ||
allow.add("net.rptools.maptool.model.*"); | ||
|
||
allow.add("net.rptools.lib.MD5Key"); | ||
|
||
Set<String> deny = new HashSet<>(); | ||
deny.add("*"); | ||
|
||
allowed = Collections.unmodifiableSet(allow); | ||
|
||
denied = Collections.unmodifiableSet(deny); | ||
} | ||
|
||
public Collection<String> getAllowed() { | ||
return allowed; | ||
} | ||
|
||
public Collection<String> getDenied() { | ||
return denied; | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/main/java/net/rptools/clientserver/hessian/HessianUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* This software Copyright by the RPTools.net development team, and | ||
* licensed under the Affero GPL Version 3 or, at your option, any later | ||
* version. | ||
* | ||
* MapTool Source Code 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. | ||
* | ||
* You should have received a copy of the GNU Affero General Public | ||
* License * along with this source Code. If not, please visit | ||
* <http://www.gnu.org/licenses/> and specifically the Affero license | ||
* text at <http://www.gnu.org/licenses/agpl.html>. | ||
*/ | ||
package net.rptools.clientserver.hessian; | ||
|
||
import com.caucho.hessian.io.HessianFactory; | ||
import com.caucho.hessian.io.HessianInput; | ||
import com.caucho.hessian.io.HessianOutput; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.zip.GZIPOutputStream; | ||
|
||
/** @author drice */ | ||
public class HessianUtils { | ||
|
||
public static HessianInput createSafeHessianInput(InputStream is) { | ||
HessianFactory hessianFactory = new HessianFactory(); | ||
HessianSecurity hessianSecurity = new HessianSecurity(); | ||
HessianInput in = hessianFactory.createHessianInput(is); | ||
in.getSerializerFactory().setAllowNonSerializable(true); | ||
in.getSerializerFactory().getClassFactory().setWhitelist(true); | ||
hessianSecurity.getAllowed().forEach(a -> in.getSerializerFactory().getClassFactory().allow(a)); | ||
hessianSecurity.getDenied().forEach(d -> in.getSerializerFactory().getClassFactory().allow(d)); | ||
return in; | ||
} | ||
|
||
public static final byte[] methodToBytes(String method, Object... parameters) { | ||
ByteArrayOutputStream bout = new ByteArrayOutputStream(); | ||
|
||
HessianOutput hout = new HessianOutput(bout); | ||
hout.getSerializerFactory().setAllowNonSerializable(true); | ||
|
||
try { | ||
hout.call(method, parameters); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return bout.toByteArray(); | ||
} | ||
|
||
public static final byte[] methodToBytesGZ(String method, Object... parameters) { | ||
|
||
ByteArrayOutputStream bout = new ByteArrayOutputStream(); | ||
|
||
try { | ||
|
||
ByteArrayOutputStream hessianBytes = new ByteArrayOutputStream(); | ||
HessianOutput hout = new HessianOutput(hessianBytes); | ||
hout.getSerializerFactory().setAllowNonSerializable(true); | ||
hout.call(method, parameters); | ||
|
||
GZIPOutputStream gzip = new GZIPOutputStream(bout); | ||
gzip.write(hessianBytes.toByteArray()); | ||
gzip.close(); | ||
|
||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return bout.toByteArray(); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/net/rptools/clientserver/hessian/MethodHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* This software Copyright by the RPTools.net development team, and | ||
* licensed under the Affero GPL Version 3 or, at your option, any later | ||
* version. | ||
* | ||
* MapTool Source Code 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. | ||
* | ||
* You should have received a copy of the GNU Affero General Public | ||
* License * along with this source Code. If not, please visit | ||
* <http://www.gnu.org/licenses/> and specifically the Affero license | ||
* text at <http://www.gnu.org/licenses/agpl.html>. | ||
*/ | ||
package net.rptools.clientserver.hessian; | ||
|
||
import net.rptools.clientserver.simple.MessageHandler; | ||
|
||
/** | ||
* @author drice | ||
* <p>TODO To change the template for this generated type comment go to Window - Preferences - | ||
* Java - Code Style - Code Templates | ||
*/ | ||
public interface MethodHandler extends MessageHandler { | ||
|
||
public void handleMethod(String id, String method, Object... parameters); | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/net/rptools/clientserver/hessian/client/ClientConnection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* This software Copyright by the RPTools.net development team, and | ||
* licensed under the Affero GPL Version 3 or, at your option, any later | ||
* version. | ||
* | ||
* MapTool Source Code 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. | ||
* | ||
* You should have received a copy of the GNU Affero General Public | ||
* License * along with this source Code. If not, please visit | ||
* <http://www.gnu.org/licenses/> and specifically the Affero license | ||
* text at <http://www.gnu.org/licenses/agpl.html>. | ||
*/ | ||
package net.rptools.clientserver.hessian.client; | ||
|
||
import java.io.IOException; | ||
import java.net.Socket; | ||
import java.net.UnknownHostException; | ||
import net.rptools.clientserver.hessian.HessianUtils; | ||
|
||
/** @author drice */ | ||
public class ClientConnection extends net.rptools.clientserver.simple.client.ClientConnection { | ||
|
||
public ClientConnection(String host, int port, String id) | ||
throws UnknownHostException, IOException { | ||
super(host, port, id); | ||
} | ||
|
||
public ClientConnection(Socket socket, String id) throws IOException { | ||
super(socket, id); | ||
} | ||
|
||
public void callMethod(String method, Object... parameters) { | ||
|
||
byte[] message = HessianUtils.methodToBytesGZ(method, parameters); | ||
sendMessage(message); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/net/rptools/clientserver/hessian/server/ServerConnection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* This software Copyright by the RPTools.net development team, and | ||
* licensed under the Affero GPL Version 3 or, at your option, any later | ||
* version. | ||
* | ||
* MapTool Source Code 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. | ||
* | ||
* You should have received a copy of the GNU Affero General Public | ||
* License * along with this source Code. If not, please visit | ||
* <http://www.gnu.org/licenses/> and specifically the Affero license | ||
* text at <http://www.gnu.org/licenses/agpl.html>. | ||
*/ | ||
package net.rptools.clientserver.hessian.server; | ||
|
||
import java.io.IOException; | ||
import net.rptools.clientserver.hessian.HessianUtils; | ||
|
||
/** @author drice */ | ||
public class ServerConnection extends net.rptools.clientserver.simple.server.ServerConnection { | ||
public ServerConnection(int port) throws IOException { | ||
super(port); | ||
} | ||
|
||
public void broadcastCallMethod(String method, Object... parameters) { | ||
broadcastMessage(HessianUtils.methodToBytesGZ(method, parameters)); | ||
} | ||
|
||
public void broadcastCallMethod(String[] exclude, String method, Object... parameters) { | ||
byte[] data = HessianUtils.methodToBytesGZ(method, parameters); | ||
broadcastMessage(exclude, data); | ||
} | ||
|
||
public void callMethod(String id, String method, Object... parameters) { | ||
byte[] data = HessianUtils.methodToBytesGZ(method, parameters); | ||
sendMessage(id, null, data); | ||
} | ||
|
||
public void callMethod(String id, Object channel, String method, Object... parameters) { | ||
byte[] data = HessianUtils.methodToBytesGZ(method, parameters); | ||
sendMessage(id, channel, data); | ||
} | ||
} |
Oops, something went wrong.