We're using RESTlet to do a small little REST server for a project we have. We set up a bunch of routes in a class inheriting from Application:
public static void createRestServer(ApplicationContext appCtx, String propertiesPath) throws Exception {
// Create a component
Component component = new Component();
component.getServers().add(Protocol.HTTP, 8081);
component.getClients().add(Protocol.FILE);
component.getClients().add(Protocol.CLAP);
Context context = component.getContext().createChildContext();
RestServer application = new RestServer(context);
application.getContext().getParameters().add("useForwardedForHeader", "true");
application.getContext().getAttributes().put("appCtx", appCtx);
application.getContext().getAttributes().put("file", propertiesPath);
// Attach the application to the component and start it
component.getDefaultHost().attach(application);
component.start();
}
private RestServer(Context context) {
super(context);
}
public synchronized Restlet createInboundRoot() {
Router router = new Router(getContext());
// we then have a bunch of these
router.attach("/accounts/{accountId}", AccountFetcher.class); //LIST Account level
// blah blah blah
// finally some stuff for static files:
//
Directory directory = new Directory(getContext(),
LocalReference.createClapReference(LocalReference.CLAP_CLASS, "/"));
directory.setIndexName("index.html");
router.attach("/", directory);
return router;
}
The problem: If I request a .js file in the JAR via Ajax from a web page (also loaded via CLAP from this JAR), it'll only return the first 7737 bytes of that file and then hang. I can't get it to return the rest of the file. It always hangs after exactly the same number of bytes. 1 in 50 times it works.
Any ideas why it's hanging? Can I just turn off chunked encoding for CLAP and static files (all ours are quite small).
This is driving us nuts.
I don't know which server connector you use for your application but it seems that it's the default one.
Restlet is pluggable and extensible at different levels. I recommend you to use the Jetty one. To do that simply add the JAR file for the Jetty extension (org.restlet.ext.jetty.jar) within your classpath. The connector will be automatically registered and use instead of the default one.
I also recommend you to upgrade to the latest version (2.3).
To see which connectors are registered in the Restlet engine, you can use the following code:
List<ConnectorHelper<Server>> serverConnectors
= Engine.getInstance().getRegisteredServers();
for (ConnectorHelper<Server> connectorHelper : serverConnectors) {
System.out.println("Server connector: "+connectorHelper);
}
You shouldn't have such problems after doing this.
Hope it helps you,
Thierry
Related
I'm using Restlet 2.3.2.
I'm building an Android Activity running a Servlet Server. I would like to package some files (the keystore and the static web files directory) with the resulting APK and make them accessible to Restlet.
Right now, I put those files in /storage/sdcard0. Here is the code that actually works with the files located at that place :
Server server = component.getServers().add(org.restlet.data.Protocol.HTTPS, port);
component.getClients().add(org.restlet.data.Protocol.FILE);
Series<Parameter> parameters = server.getContext().getParameters();
parameters.add("sslContextFactory", "org.restlet.engine.ssl.DefaultSslContextFactory");
parameters.add("keyStorePath", "/storage/sdcard0/ssl/keystore");
parameters.add("keyStorePassword", "password");
parameters.add("keyPassword", "password");
parameters.add("keyStoreType", "BKS");
parameters.add("keyManagerAlgorithm", KeyManagerFactory.getDefaultAlgorithm());
// Create an application
Application application = new Application() {
#Override
public Restlet createInboundRoot() {
return new Directory(getContext(), "/storage/sdcard0/www/");
}
};
component.getDefaultHost().attach("/static", application);
I'd like to package these files by putting them in res/rawor assets in my Android project, but I don't know how to make them accessible to my web server once moved.
you can change the Directory root url and use the clap protocol (that leverages the classloader):
component.getClients().add(org.restlet.data.Protocol.CLAP);
// Create an application
Application application = new Application() {
#Override
public Restlet createInboundRoot() {
return new Directory(getContext(), "clap://class/assets/");
}
};
I've not tested exactly your code, I hope it will work for you.
I search for some help creating a web Flex application using BlazeDS and Java server with dynamic BlazeDS endpoint configuration.
First, I will try to explain my current situation.
I have a Flex 3.2 application that provides GUI of the application. From the ActionScript I call Java methods using BlazeDS. To access the BlazeDS I use a Config class that provides the endpoint as shown below (it is a constructor):
public function Config(): void {
if (_serviceUrl == null) {
try {
var browser: IBrowserManager = BrowserManager.getInstance();
browser.init();
var url: String = browser.url;
var host: String = mx.utils.URLUtil.getServerName(url);
var port: uint = mx.utils.URLUtil.getPort(url);
var parts: Array = url.split('/');
if (parts[2] == '') {
url = DEFAULT_URL;
Alert.show("Unable to determine server location, using default URL: " + DEFAULT_URL, "Connection error");
}
else {
url = parts[0] + '//' + parts[2] + '/' + parts[3] + '/messagebroker/amf';
}
_serviceUrl = url;
} catch (e: Error) {
Alert.show("Exception while trying to determine server location, using default URL: " + DEFAULT_URL, "Connection exception");
_serviceUrl = DEFAULT_URL;
}
}
}
The idea of the class is to determine the endpoint from the request URL. I use a Delegate class to call the remote methods using BlazeDS like the following:
{
import com.adobe.cairngorm.business.ServiceLocator;
import mx.rpc.IResponder;
import mx.rpc.remoting.RemoteObject;
public class AbstractRemoteDelegate
{
public function AbstractRemoteDelegate(responder:IResponder,serviceName:String)
{
_responder=responder;
_locator=ServiceLocator.getInstance();
_service=_locator.getRemoteObject(serviceName);
_service.showBusyCursor=true;
_service.endpoint = Config.instance.serviceUrl;
}
private var _responder:IResponder;
private var _locator:ServiceLocator;
private var _service:RemoteObject;
protected function send(operationName:String,... args:Array) : void {
_service.getOperation(operationName).send.apply(_service.getOperation(operationName),args).addResponder(_responder);
}
}
}
This approach actually works fine. However, I got across a situation where I can't use dynamically determined URL. In such a situation, I need a hard-coded URL in the Config.as file. And this is the problem. When trying to deploy the application to another server, I always need to rebuild the application with a new URL configuration in the ActionScript class Config.
Therefore I search for a way to define a static configuration for the Flex application to connect to a BlazeDS server. And the way to change such configuration without rebuilding the application so I can give the customer his own way to reconfigure and move the Flex application.
I thought about using a configuration file, but Flex runs on the client side and there is no configuration file!
I thought about using database configuration, but I don't have any database on the client side!
To sum up, I am looking for a way, how to get BlazeDS URL from a configuration to be able to change it without rebuilding the whole app.
Thanks for any useful suggestions.
EDIT: Revised the question to be more actual. I improved the way to determine the URL dynamically from the request URL, so it works now even for proxy server. However, my curiosity persists for the configuration of flex without rebuilding.
Here is an old example Blaze DS Service of mine which does basically the same as you did. It's just the string which needs to be created correctly. If the endpoint address is wrong, catch the error accordingly.
My project may currently not build because of Flexmojos ... I'm not able to test that yet.
Since it did not read you question properly, I misunderstood you: You can put a configuration file next to the SWF and load it via URLLoader or pass it via FlashVars. That should give you the freedom to pass the endpoint dynamically.
I am using eclipse with the Google Toolkit and I have created a widget with a listbox, vertical split panel and a couple of buttons. What I am trying to do is have a list of files in a local directory listed in the listbox and I want to be able to click on a file and have it displayed in the top part of the split panel. I found out the hard way about browsers and file IO and not being able to use java.io.File.
What are my options? Can I put the data files inside a jar or something and have the widget read it in that way? I need to do this as a test run, to implement an new feature with working with the data. It's not going to be any kind of final server hosted application, I am not concerned about how the actual files will be loaded in the future.
Any suggestions would be greatly appreciated.
Respectfully,
DemiSheep
If you just need a hard-coded list of values to visually test your widget, you can simply put these values in a String array and load it from there. Or you can http GET the strings from a server using RequestBuilder. You can keep a simple file (CSV, XML, JSON etc.) in your war directory and load this file using Request builder.
Example code from GWT developer guide:
import com.google.gwt.http.client.*;
...
String url = "http://www.myserver.com/getData?type=3";
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(url));
try {
Request request = builder.sendRequest(null, new RequestCallback() {
public void onError(Request request, Throwable exception) {
// Couldn't connect to server (could be timeout, SOP violation, etc.)
}
public void onResponseReceived(Request request, Response response) {
if (200 == response.getStatusCode()) {
// Process the response in response.getText()
} else {
// Handle the error. Can get the status text from response.getStatusText()
}
}
});
} catch (RequestException e) {
// Couldn't connect to server
}
Make sure you inherit HTTP module:
<inherits name="com.google.gwt.http.HTTP" />
Create testcases with JUnit!
This is the official Google site describing Testing with JUnit and varios test methods: Google Web Toolkit: Testing. You definitly find a solution here^^
As it comes to GWT, there is no such thing sent to a browser as a .jar-file.
The easiest thing to fetch the file would be to
put the files on a server
fetch them via a http-call
Remember the same-origin-policy that applies to GWT as it is underlying all javascript-Restrictions
We have an MTOM-enabled web service that is published with Grails and the Metro 1.0.2 plugin:
#MTOM
#WebService(targetNamespace="http://com.domain")
class TestService {
#WebMethod
int uploadFile(#XmlMimeType("application/octet-stream")DataHandler data) {
data.dataSource.inputStream.eachLine {
println "reading: -> ${it}"
}
return 0
}
}
Following this tutorial, we set up a Java test-client that looks like this
public class Client {
public static void main(String[] argv) {
MTOMFeature feat = new MTOMFeature();
TestService service = new TestServiceService().getTestServicePort(feat);
Map<String, Object> ctxt = ((BindingProvider)service).getRequestContext();
ctxt.put(JAXWSProperties.HTTP_CLIENT_STREAMING_CHUNK_SIZE, 8192);
service.uploadFile(new DataHandler(new FileDataSource("c:/file.xml")));
}
}
When I run the client, I get the following error message:
Couldn't create SOAP message due to
exception:
org.jvnet.mimepull.MIMEParsingException:
Missing start boundary
However, when I don't add the MTOMFeature, and just do
TestService service = new TestServiceService().getTestServicePort(); the files gets uploaded ok. But as I understand it if MTOM is not enabled on both server and client side, the entire file will be kept in memory (and not streamed). So, my questions are
Why do we get that error?
If I don't add the MTOMFeature, will the file still be MTOM-transmitted?
I would be very grateful for any help/tips!
After some research and testing, the answers are:
The error is because grails adds its own filtering, including services. So, by excluding the services from being filtered like this static excludes = ["/services/*"] in UrlMappings.groovy, it works.
No. Without the MTOMFeature the file will just be treated as any other data in the request. That means being stored in the memory, thus causing problems for big files.
I'm developing a test WS with JAX-WS and the demo works fine but if I try to move it in production there are a lot of occurences of the URL of my test enviroment on my code. For example:
com.mycompany.testserver.ws.writer.WriterInterface service = new com.mycompany.testserver.ws.writer.WriterInterface();
QName portQName = new QName("http://testserver.mycompany.com/ws/writer.php", "WriterInterfacePort");
String req = "SOME_XML_HERE";
try { // Call Web Service Operation
Dispatch<Source> sourceDispatch = null;
sourceDispatch = service.createDispatch(portQName, Source.class, Service.Mode.PAYLOAD);
Source result = sourceDispatch.invoke(new StreamSource(new StringReader(req)));
} catch (Exception ex) [
//do stuff here
}
What is the best practice for moving such an app?
Read the connection URL from a configuration file. You might also want to have a strongly-typed configuration object that will cache the value so that it only needs to be read once. Then when you move your app from test to production, you only need to update the configuration file.
The most common practice is to store the URL's in config files rather than hardcoded, and have separate configurations for test and production.
Introducing an integration broker or service registry tends to be overkill.
You should really avoid hard coding URL's into code. There are a variety of ways this could be avoided. Even if you had no config file functionality available you should at the very least be storing this data in a string at the top of each class where its declared.
No need to tell you (But I guess I am) you are now encountering the reason as to why you never hard code URL's / configuration data.