are there any good examples on how to use Google App Engine from Silverlight, preferably without writing custom webservices?
Cheers
Nik
Here is my approach based heavily on Scott Seely's post
Simply passes XML around, .xap is also served by GAE. I only just got this working yesterday so it is still work in progress.
Google:
app.yaml
application: wowbosscards
version: 1
runtime: python
api_version: 1
handlers:
- url: /WowBossCards.html
static_files: WowBossCards.html
upload: WowBossCards.html
mime_type: text/html
- url: /clientaccesspolicy.xml
static_files: clientaccesspolicy.xml
upload: clientaccesspolicy.xml
mime_type: text/xml
- url: /WowBossCards.xap
static_files: WowBossCards.xap
upload: WowBossCards.xap
mime_type: application/x-silverlight-app
- url: .*
script: wowbosscards.py
wowbosscards.py
#!/usr/bin/env python
import cgi
import datetime
import wsgiref.handlers
import os
import logging
import string
import urllib
from google.appengine.ext import db
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
class Xml(db.Model):
xmlContent = db.TextProperty()
date = db.DateTimeProperty(auto_now_add=True)
class XmlCrud(webapp.RequestHandler):
def get(self, xmlType):
xmlKey = string.capitalize(xmlType)
xml = Xml.get_by_key_name(xmlKey)
self.response.headers['Content-Type'] = "application/xml"
self.response.out.write(xml.xmlContent)
def post(self, xmlType):
logging.debug("XmlCrud POST")
xmlKey = string.capitalize(xmlType)
xml = Xml.get_by_key_name(xmlKey)
if len(self.request.get('content')) > 0 :
xml.xmlContent = self.request.get('content')
else:
xml.xmlContent = self.request.body
xml.put()
self.redirect('/upload/' + xmlType)
def main():
Xml.get_or_insert("Bosses", xmlContent="<a>Bosses go here</a>")
Xml.get_or_insert("Dungeons", xmlContent="<a>Dungeons go here</a>")
application = webapp.WSGIApplication([
(r'/xml/(.*)', XmlCrud),
], debug=True)
wsgiref.handlers.CGIHandler().run(application)
if __name__ == '__main__':
main()
Silverlight:
private class RequestExtended
{
public VoidCall CallBack;
public WebRequest Request;
public Uri Host;
public RequestExtended(WebRequest request, VoidCall callBack, Uri host)
{
Request = request;
CallBack = callBack;
Host = host;
}
}
public static void ImportFromGoogle(Uri host, VoidCall callBack)
{
try
{
if (host.Host == "localhost")
{
host = new Uri(host.Scheme + #"://" + host.Host + ":8080");
}
var bossesCrud = new Uri(host, "/xml/bosses");
var bossesRequest = WebRequest.Create(bossesCrud);
bossesRequest.BeginGetResponse(BossesResponse, new RequestExtended(bossesRequest, callBack, host));
}
catch (Exception ex)
{
}
}
public static void BossesResponse(IAsyncResult result)
{
var requestExtended = result.AsyncState as RequestExtended;
try
{
WebResponse response = requestExtended.Request.EndGetResponse(result);
Stream responseStream = response.GetResponseStream();
byte[] bytes = new byte[response.ContentLength];
responseStream.Read(bytes, 0, bytes.Length);
responseStream.Close();
var enc = new System.Text.UTF8Encoding();
string xml = enc.GetString(bytes, 0, bytes.Length);
bosses = GetCollectionFromXmlString<BossCollection>(xml);
if (requestExtended.CallBack != null) requestExtended.CallBack();
}
catch (WebException we)
{
HttpWebResponse response = (HttpWebResponse)we.Response;
response.Close();
}
catch (Exception e)
{
}
}
public static void SaveBossesToGoogle(Uri host)
{
if (host.Host == "localhost")
{
host = new Uri(host.Scheme + #"://" + host.Host + ":8080");
}
var bossesCrud = new Uri(host, "/xml/bosses");
var request = WebRequest.Create(bossesCrud);
request.Method = "POST";
request.ContentType = "text/xml"; //"application/x-www-form-urlencoded";
request.BeginGetRequestStream(GetSaveBossesRequestStreamCallback, new RequestExtended(request, null, host));
}
static void GetSaveBossesRequestStreamCallback(IAsyncResult result)
{
var requestExtended = result.AsyncState as RequestExtended;
try
{
Stream stream = requestExtended.Request.EndGetRequestStream(result);
var xmlSerializer = new XmlSerializer(typeof(BossCollection));
var xmlText = new StringBuilder();
using (TextWriter textWriter = new StringWriter(xmlText))
{
xmlSerializer.Serialize(textWriter, Store.Bosses);
textWriter.Close();
}
var enc = new System.Text.UTF8Encoding();
var bytes = enc.GetBytes(xmlText.ToString());
stream.Write(bytes, 0, bytes.Length);
stream.Close();
requestExtended.Request.BeginGetResponse(SaveResponse, requestExtended);
}
catch (WebException we)
{
HttpWebResponse response = (HttpWebResponse)we.Response;
response.Close();
}
}
static void SaveResponse(IAsyncResult result)
{
var requestExtended = result.AsyncState as RequestExtended;
try
{
WebResponse response = requestExtended.Request.EndGetResponse(result);
if (requestExtended.CallBack != null) requestExtended.CallBack();
}
catch (WebException we)
{
HttpWebResponse response = (HttpWebResponse)we.Response;
response.Close();
}
}
I'm thinking about doing the same thing, but I've not come across anything yet.
I'm thinking about using JSON.net for the comms, so basically writing a REST service in GAE for the client to call, and maybe OAuth.NET for the authentication (unless I can find the .NET port of the google one, I've not looked yet)
Silverlight is basically just .NET, tho a lite version of it, so if you can find .NET code to do something, it should work, atleast somewhat, in SL :)
But thats as far as I've got - thinking about it. Sorry, can't be of more help yet!
I'm looking at this also. There are several REST projects for GAE, I haven't tried any of them out yet, but hope to in the next week or so.
http://code.google.com/p/app3/
http://code.google.com/p/gae-json-rest/
http://code.google.com/p/appengine-rest-server/
I couldn't find any examples getting Silverlight to work with google app's Java SDK, so here is my post.
Download the demo for Expression Blend. Check the included tutorial which shows how to create a gorgeous Silverlight interface in GUI mode and integrate it with the Bing search web service. Manipulating this example into a Google example should be trivial. Good luck! :)
Related
I already know how to save images in mongodb using angularjs and java to save it in my mongodb,
I need to get the saved image from mongodb and display it in an html page using AngularJS.
This is my controller for getting image
#GET
#Path("/{id}")
#Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response getById(#PathParam("id") String id) throws IOException
{
Response response = null;
MongoClient mongoClient = new MongoClient("localhost", 27017);
DB mongoDB = mongoClient.getDB("sampleDB");
DBCollection collection = mongoDB.getCollection("filestore");
BasicDBObject query = new BasicDBObject();
ObjectId oid = new ObjectId(id);
query.put("_id", oid);
GridFS fileStore = new GridFS(mongoDB, "filestore");
GridFSDBFile gridFile = fileStore.findOne(query);
InputStream in = gridFile.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
int data = in.read();
while (data >= 0)
{
out.write((char) data);
data = in.read();
}
out.flush();
ResponseBuilder builder = Response.ok(out.toByteArray());
builder.header("Content-Disposition", "attachment; filename=");
response = builder.build();
return response;
}
This is my angularjs for getting image
var userImagePromise = $http.get("../api/s3/" + $scope.user.profileImage[0].id);
userImagePromise.success(function(data, status, headers, config) {
$scope.imageData = data;
});
userImagePromise.error(function(data, status, headers, config) {
});
This is my html for displaying image
<img id="userProfileImg" height="150px" width="150px" ng-src="data:image/png;base64,{{imageData}}">
if I simply put the link to browser i got this output in octect-stream
�PNG .....
How to display image in html?Any error in my code wise?
Image for getting output
i think your base64 code is not converting images properly, so check my code it may help you.
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import javax.imageio.ImageIO;
BufferedImage buffimage = ImageIO.read(new File(imagePath));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(buffimage, "png", baos);
String img = Base64.encode(baos.toByteArray());
send this img variable to your angularjs code.
I am not good with Web API. Here is my problem. I send an Json serialized object from my Windows Form Application. The object is an Entity table. When I do a get response it returns a 500 server error. Basically I plan to have multiple post methods in one controller which I may not be doing right. So I need you guys to guide me on what I have been doing wrong.
Here is my Controller:
[ResponseType(typeof(HttpWebResponse)), HttpPost, ActionName("MerchandiseApi")]
public HttpResponseMessage PostMain(IList<IMF_Main> mainFromConsolidator)
{
if (!ModelState.IsValid)
return Request.CreateResponse(HttpStatusCode.BadRequest, 2);
using (var anthill = new AnthillConsolidatorEntities())
{
var main = new IMF_Main();
foreach (var item in mainFromConsolidator)
{
main.BrandID = item.BrandID;
main.ItemID = item.ItemID;
main.CategoryID = item.CategoryID;
main.SubCategoryID = item.SubCategoryID;
main.ClassID = item.ClassID;
main.GenderID = item.GenderID;
main.CoaID = item.CoaID;
main.SubCoaID = item.SubCoaID;
main.First_SRP = item.First_SRP;
main.Current_SRP = item.Current_SRP;
main.Previous_SRP = item.Previous_SRP;
main.isSenior = item.isSenior;
main.isActive = item.isActive;
main.DateCreated = item.DateCreated;
anthill.IMF_Main.Add(main);
anthill.SaveChanges();
}
}
return Request.CreateResponse(HttpStatusCode.OK, 1);
}
Here's my WebApiConfig:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "MerchandiseApi",
routeTemplate: "api/{controller}/{action}"
);
}
Here is where the Uri gets built: I have 2 more tables to send but I will start with this. This goes to my first Post method to the server
var jsonMain = JsonConvert.SerializeObject(consolidatorEntities.IMF_Main, Formatting.None);
HttpPost("http://localhost:50826/api/Merchandise/PostMain", jsonMain) == 1.ToString()
public string HttpPost(string uri, string json)
{
string content = "";
try
{
var request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.Accept = "application/json";
request.ContentType = "application/json";
byte[] bodyBytes = Encoding.UTF8.GetBytes(json);
request.GetRequestStream().Write(bodyBytes, 0, bodyBytes.Length);
request.GetRequestStream().Close();
var response = (HttpWebResponse)request.GetResponse();
var sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncod
ing("UTF-8"));
content = sr.ReadToEnd();
sr.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error sending data to Anthill \nException: " + ex, "Monytron - Consolidator", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return content;
}
Problem
The main problem is with your routing. Routes will check in order so when you post a request to http://localhost:50826/api/Merchandise/PostMain and you have these routes in order:
"api/{controller}/{id}"
"api/{controller}/{action}"
So the first route will match:
If your PostMain method is the only action with [HttpPost], then mainFromConsolidator will be null in your foreach loop you will receive a NullReferenceException that result in a 500 error.
If you have multiple method decorated with [HttpPost], then the call is ambiguous between those actions and you will receive an InvalidOperationExpception with "Multiple actions were found that match the request" message that result in a 500 error.
The other problem is you are using an ActionName("MerchandiseApi") but didn't post to that action.
Solution
You can use multiple solutions. As an option you can define only one route:
"api/{controller}/{action}/{id}"
This way you can create a controller that contains actions like these:
public class SomeController
{
// matches GET /api/some/action1
[HttpGet]
public HttpResponseMessage Action1()
// matches GET /api/some/action2/5
[HttpGet]
public HttpResponseMessage Action2(int id)
// matches POST /api/some/action3
[HttpPost]
public HttpResponseMessage Action3(SomeType someParameter)
// matches POST /api/some/action4
[HttpPost]
public HttpResponseMessage Action4(SomeType someParameter)
}
Anyway if you decide to define multiple routes, pay attention that routes will match in order and also if you used ActionName attribute, then use that name in url to call that action.
I am using Google App Engine (python) and have a data: url of a PNG image available on the server. The PNG image was never in a file, as it was generated from some canvas code using toDataUrl() and ajaxed to the server. I would like to allow the user to click a button and be able to select a filename and save the PNG image locally. The Save As dialog box would supply a default filename.png. The target browser is FireFox. I have supplied sample code that doesn't work. There are several questions on stackoverflow that are somewhat like this one, but each is a little different.
I am setting content-disposition as attachment with a suggested filename. I set the header content-type to application/octet-stream. But I don't get the SaveAs dialog. What am I missing?
The app.yaml file is the standard
application: saveas
version: 1
runtime: python
api_version: 1
handlers:
- url: /.*
script: main.py
The index.html is as follows:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<script>
function saveAsPng()
{
var alldata;
var httpRequest;
var response;
var myheaders;
httprequest = new XMLHttpRequest();
if ( !httprequest )
{
alert("In saveAsPng, XMLHttpRequest failed.");
return;
}
/* Make this a json string */
alldata = JSON.stringify("no data");
try
{
httprequest.open('POST', '/handlebitmap', true);
httprequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
httprequest.setRequestHeader("Content-length", alldata.length);
httprequest.setRequestHeader("Connection", "close");
httprequest.onreadystatechange = function()
{
if ( httprequest.readyState == 4 )
{
if (httprequest.status == 200)
{
/* a status of 200 is good. */
response = null;
try
{
response = JSON.parse(httprequest.responseText);
}
catch (e)
{
response = httprequest.responseText;
}
if ( response == "error" )
{
alert("In saveAsPng callback, response == error");
return false;
}
else
{
/* This is the successful exit. */
//alert("response = " +response);
window.location.href = response;
return true;
}
}
else
{
/* httprequest.status was not 200, so must be an error. */
alert("saveAsPNG callback, status = " +httprequest.status);
return false;
}
} /* End of if where readyState was 4. */
} /* End of the callback function */
/* Make the actual request */
httprequest.send(alldata);
}
catch(e)
{
alert("In saveAsPng, Can't connect to the server");
}
} /* End of the saveAsPng function */
The python code is as follows:
# !/usr/bin/env python
import os
import base64
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp import util
class MainPage(webapp.RequestHandler):
""" Renders the main template."""
def get(self):
template_values = { 'title':'Test Save As PNG', }
path = os.path.join(os.path.dirname(__file__), "index.html")
self.response.out.write(template.render(path, template_values))
class BitmapHandler(webapp.RequestHandler):
""" Shows the Save As with a default filename. """
def post(self):
origdata = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="
urldata = "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="
decodeddata = base64.b64decode(urldata)
self.response.headers['Content-Type'] = 'application/octet-stream'
self.response.headers['Content-Disposition'] = 'attachment; filename="untitled.png"'
self.response.out.write(decodeddata)
def main():
app = webapp.WSGIApplication([
('/', MainPage),
('/handlebitmap',BitmapHandler),
], debug=True)
util.run_wsgi_app(app)
if __name__ == '__main__':
main()
{{title}}
Perhaps you want to change the "def post" to a "def get"? I just tried your sample and it seemed to be working as expected after that change (only tried /handlebitmap)
I wish to have a file upload form that in addition to the file selection input , also has other input fields like textarea, dropdown etc. The problem is that I cannot access any post parameters other than the file in my blobstore upload handler. I am using the following function call to get the parameter name but it always returns an empty screen.
par = self.request.get("par")
I found another question with a similar problem Uploading a video to google app engine blobstore. The answer to that question suggests a workaround solution to set the filename to the parameter you wish to read which is not sufficient for my needs. Is there a way to access other form parameters in the post method of blobstore upload handler?
Did you find the solution?
In my experience, when using form/multipart request doesn't include the other parameters and they have to be dug out manually.
This is how I dig out parameters out of request that is used to send a file.
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import javax.servlet.http.HttpServletRequest;
// for reading form data when posted with multipart/form-data
import java.io.*;
import javax.servlet.ServletException;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.google.appengine.api.datastore.Blob;
// Fetch the attributes for a given model using rails conventions.
// We need to do this in Java because getParameterMap uses generics.
// We currently only support one lever: foo[bar] but not foo[bar][baz].
// We currently only pull the first value, so no support for checkboxes
public class ScopedParameterMap {
public static Map params(HttpServletRequest req, String model)
throws ServletException, IOException {
Map<String, Object> scoped = new HashMap<String, Object>();
if (req.getHeader("Content-Type").startsWith("multipart/form-data")) {
try {
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iterator = upload.getItemIterator(req); // this is used to get those params
while (iterator.hasNext()) {
FileItemStream item = iterator.next();
InputStream stream = item.openStream();
String attr = item.getFieldName();
if (attr.startsWith(model + "[") && attr.endsWith("]")) { // fetches all stuff like article[...], you can modify this to return only one value
int len = 0;
int offset = 0;
byte[] buffer = new byte[8192];
ByteArrayOutputStream file = new ByteArrayOutputStream();
while ((len = stream.read(buffer, 0, buffer.length)) != -1) {
offset += len;
file.write(buffer, 0, len);
}
String key = attr.split("\\[|\\]")[1];
if (item.isFormField()) {
scoped.put(key, file.toString());
} else {
if (file.size() > 0) {
scoped.put(key, file.toByteArray());
}
}
}
}
} catch (Exception ex) {
throw new ServletException(ex);
}
} else {
Map params = req.getParameterMap();
Iterator i = params.keySet().iterator();
while (i.hasNext()) {
String attr = (String) i.next();
if (attr.startsWith(model + "[") && attr.endsWith("]")) {
String key = attr.split("\\[|\\]")[1];
String val = ((String[]) params.get(attr))[0];
scoped.put(key, val);
// TODO: when multiple values, set a List instead
}
}
}
return scoped;
}
}
I hope this speedy answer helps, let me know if you have questions.
I am just trying to perform an http post on http://www.test.com/test.asp?test1=3. Here is the code I have been trying to use:
private void pif_test_conn()
{
Uri url = new Uri("http://www.test.com/test.asp?test1=3", UriKind.Absolute);
if (httpResult == true)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
request.BeginGetResponse(new AsyncCallback(ReadCallback), request);
}
return ;
}
private void ReadCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
using (StreamReader streamReader1 = new StreamReader(response.GetResponseStream()))
{
string resultString = streamReader1.ReadToEnd();
MessageBox.Show("Using HttpWebRequest: " + resultString, "Found", MessageBoxButton.OK);
}
}
When I execute this code my program triggers the Application_UnhandledException event. Not sure what I am doing wrong.
Are you trying to post to another host? That behavior could lead to XSS security problems, so that isnt available.
string responseValue = "";
AutoResetEvent syncRequest = new AutoResetEvent(false);
Uri address = new Uri(HtmlPage.Document.DocumentUri, "/sample.aspx");
WebRequest request = WebRequest.Create(address);
request.Method = "POST";
request.BeginGetRequestStream(getRequestResult =>
{
// Send packet data
using (Stream post = request.EndGetRequestStream(getRequestResult))
{
post.Write(buffer, 0, buffer.Length);
post.Close();
}
// wait for server response
request.BeginGetResponse(getResponseResult =>
{
WebResponse response = request.EndGetResponse(getResponseResult);
responseValue=new StreamReader(response.GetResponseStream()).ReadToEnd();
syncRequest.Set();
}, null);
}, null);
syncRequest.WaitOne();
MessageBox.Show(
"Using WebRequest: " + responseValue,
"Found", MessageBoxButton.OK);
HTH
You can only send HTTP requests to the domain that your app comes from.
This restriction prevents XSS attacks.
With regard to Rubens' answer,
If you leave in the SyncRequest.WaitOne() call, the call deadlocks, at least in Silverlight 4.0.
In order to send an HTTP POST, you need to write the POST data to the request by calling the BeginGetRequestStream method.
This is probably why you're getting an exception; please tell us what exception you're geting for a more specific answer.