send xml data indoPost by coverting it to byte stream

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

send xml data indoPost by coverting it to byte stream

Postby suhabb » Thu Jan 07, 2010 2:45 pm

/*server side code*/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/



protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @return void
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException, NullPointerException {

SyncAdaptor synAdaptor = new SyncAdaptor();
ServletOutputStream outputStream = null;
ServletInputStream servletInputStream = null;
BufferedReader in = null;
try {

/*
* This is used to get stream from HttpRequest. Convert into burrerreader
*/
logger.info("Servlet starts here");
servletInputStream = request.getInputStream();
in = new BufferedReader(new InputStreamReader(servletInputStream));
StringBuffer inputFromClient = new StringBuffer();
String syncInput;
String outSyncML;
while ((syncInput = in.readLine()) != null) {
inputFromClient.append(syncInput);
}
GarbageCollector.runGarbageCollector();
//inputLine = inputFromClient.toString();
syncInput = inputFromClient.toString();
logger.info("Request From Client --> " + syncInput);
GarbageCollector.runGarbageCollector();
if (!validateRequest(syncInput)) {
// handle the error part
logger.error("validation fail in servlet");
outSyncML = SyncMLGenerator.generateResponseForInvalidRequest();
logger.info("After Creating Error Response for Bad Request");
GarbageCollector.runGarbageCollector();
} else {
StringReader syncReader = new StringReader(syncInput);

logger.info("STRING READER" + syncReader);
outSyncML = synAdaptor.processSyncML(syncReader);
GarbageCollector.runGarbageCollector();
}
outputStream = response.getOutputStream();
outputStream.write(outSyncML.getBytes());
GarbageCollector.runGarbageCollector();
} catch (Exception e) {
logger.error("Exception in ActionServlet " + e);
String erroroutSyncML;
erroroutSyncML = SyncMLGenerator.generateResponseForInvalidRequest();
outputStream = response.getOutputStream();
outputStream.write(erroroutSyncML.getBytes());
} finally {
if(in!=null)
in.close();
if(servletInputStream!=null)
servletInputStream.close();
if(outputStream!=null)
outputStream.close();
GarbageCollector.runGarbageCollector();
}
}//end of processRequest

/**
* This method used for validate given input is not null or its having proper
* stating and ending tags.
* @param inputLine
* @return boolean
* @throws
*/
private boolean validateRequest(String inputLine) {
boolean returnType = false;
try {
if (inputLine != null) {
if (inputLine.indexOf("<SyncML>") == 0 && inputLine.indexOf("</SyncML>") == (inputLine.length() - 9)) {
returnType = true;
} else {
returnType = false;
}
} else {
returnType = false;
}
} catch (NullPointerException ne) {
logger.error("Nullpointer Exception validateRequest() in ActionServlet " + ne);
} catch (Exception e) {
logger.error("Exception in validateRequest() in ActionServlet " + e);
}
return returnType;
}//end of validateRequest
}//end of Action Servlet





/*Client side need to send request to this servlet */ i need help here


public void postData() throws Exception{


// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://<URL>/ActionServlet");
URLEncoder tmp = null;
InputStream in;


String s="<SyncML></SyncML/>" ;



in=getBytes(s);

System.out.println(in);
try {
// tmp = new URLEncoder(in);
} catch (UnsupportedEncodingException e) {
System.out.println("HTTPHelp : UnsupportedEncodingException : "+e);
}
httppost.getEntity().getContent // neeed help here ni to send stream to the server encoding with url
(setEntity(new URLEncoder(in);


try{
// Execute HTTP Post Request
System.out.println("before post");
HttpResponse response = httpclient.execute(httppost);
System.out.println("After post");
InputStream is = response.getEntity().getContent();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(20);

int current = 0;
while((current = bis.read()) != -1){
baf.append((byte)current);
}

/* Convert the Bytes read to a String. */
text = new String(baf.toByteArray());
tv.setText(text);

} catch (ClientProtocolException e) {
System.out.println("protocol exception");
// TODO Auto-generated catch block
} catch (IOException e) {
System.out.println("IO this exception");
// TODO Auto-generated catch block
}
}

private InputStream getBytes(String s) {
// TODO Auto-generated method stub
return null;
}
}
suhabb
Freshman
Freshman
 
Posts: 2
Joined: Wed Dec 23, 2009 10:56 am

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 8 guests