JĘZYKI PROGRAMOWANIA Elementy programowania rozproszonego Serwer www / protokół HTTP Serwer www (język Java) Webservice typu REST(Restfull) Webservice typu SOAP/WSDL Zdalne wywoływanie procedur CORBA/RPC Serwer www / protokół HTTP OPTIONS GET HEAD POST PUT DELETE TRACE CONNECT sprawdzenie udostępnionych metod pobieranie danych pobieranie metadanych na temat zapytania wysyłanie danych dodanie zasobu skasowanie zasobu używane przy testowaniu połączenia stworzenie tunelu poprzez serwery proxy Protokół HTTP w wersji 1.1, opisany w dokumencie RFC 2068, jest faktycznym standardem przesyłu dokumentów WWW. Protokół ten jest ogólny i bezpaństwowy, i został zaprojektowany w taki sposób, aby był rozszerzalny do prawie każdego formatu dokumentu. Działa on poprzez połączenia protokołu TCP, zazwyczaj wykorzystując port 80, chociaż można określić inny port (na przykład 8080). Po ustanowieniu połączenia, klient transmituje do serwera komunikat żądania, a ten wysyła odpowiedź. Protokół HTTP jest zazwyczaj wykorzystywany przez takie aplikacje, jak przeglądarki. 1
Najprostszą funkcją, lub metodą, protokołu HTTP jest GET, która pobiera informacje zapamiętane w witrynie WWW, lub na serwerze WWW. Jednak systemy informacyjne o pełnych możliwościach wymagają szerszego zestawu funkcji, łącznie z wyszukiwaniem, uaktualnianiem oraz przypisami. Protokół HTTP zapewnia zestaw metod sygnalizujących cel danego żądania. Wykorzystuje on ujednolicony identyfikator zasobów (URI) albo jako ujednolicony lokalizator zasobów (URL), albo jako ujednoliconą nazwę zasobów (URN), aby wskazać zasób, wobec którego ma zostać zastosowana metoda. Komunikaty są przekazywane w formacie podobnym do tego, który wykorzystuje poczta internetowa, zdefiniowanym przez standard uniwersalnych rozszerzeń internetowej poczty elektronicznej (MIME) - (RFC od 2045 do 2049) GET pobiera informacje (w formie jednostki) zidentyfikowane przez URI, do którego zostało zgłoszone żądanie. Jeżeli URI zidentyfikuje proces wytwarzający dane, to wytworzone dane zostaną zwrócone jako jednostka. Warunkowy komunikat GET zawiera pole nagłówka If- Modified-Since, If-Unmodified-Since, If-Match, If-None-Match, lub If- Range. Pozwala to na odświeżanie buforowanych jednostek bez potrzeby wielokrotnego przesyłu, czy żądania danych, które są już w posiadaniu klienta. GET częściowy żąda, aby przesłana została tylko część jednostki, określona przez pole nagłówka Range. Przekazywanie danych do metody GET odbywa się poprzez QueryString. HEAD identyczny z GET, z tym że serwer nie zwraca w odpowiedzi treści komunikatu. Metoda ta uzyskuje informacje dotyczące jednostki nie przesyłając samej treści jednostki i jest wykorzystywana do testowania ważności, dostępności oraz niedawnych modyfikacji łączy hipertekstowych. POST żąda, aby serwery przyjmowały jednostkę załączoną w żądaniu, jako nowego podwładnego zasobu zidentyfikowanego przez URI, do którego zostało zgłoszone żądanie POST jest wykorzystywany do przypisywania zasobów, do wysyłania komunikatów, do przedkładania 2
danych formularzy oraz do rozszerzania bazy danych poprzez operację dołączania. Funkcja pełniona przez metodę POST określana jest przez serwer i zazwyczaj jest uzależniona od URI. Serwer www (język Java) package jp_w10; import java.io.bufferedinputstream; import java.io.file; import java.io.fileinputstream; import java.io.ioexception; import java.io.outputstream; import java.net.inetsocketaddress; import java.util.arraylist; import com.sun.net.httpserver.*; public class MessengerServer { ArrayList<VerySimpleMessage> messages; public static void main(string[] args) { MessengerServer ms=new MessengerServer(); ms.starserver(); //http://localhost:8001/send?sender=andrzej&receiver=stefan&message ="co słychać?" public void starserver(){ try { HttpServer server = HttpServer.create(new InetSocketAddress(8001), 0); server.createcontext("/send", new SendHandler()); server.createcontext("/receive", new ReceiveHandler()); server.setexecutor(null); // creates a default executor server.start(); messages=new ArrayList<VerySimpleMessage>(20); catch (IOException e) { // TODO Auto-generated catch block 3
e.printstacktrace(); class SendHandler implements HttpHandler { public void handle(httpexchange t) throws IOException { t.getresponseheaders().add("accept", "*/*"); VerySimpleMessage vsm=new VerySimpleMessage(); vsm.setsender(t.getrequesturi().getquery().split("&")[0].split("=") [1]); vsm.setreceiver(t.getrequesturi().getquery().split("&")[1].split("= ")[1]); vsm.setmessagebody(t.getrequesturi().getquery().split("&")[2].split ("=")[1]); messages.add(vsm); System.out.println(t.getRequestURI().getQuery()); String response = "<H1>Dziekuję za przesłanie wiadomości, będzie oczekiwać na odbiorcę</h1>"; t.sendresponseheaders(200, response.length()); OutputStream os = t.getresponsebody(); os.write(response.getbytes()); os.close(); class ReceiveHandler implements HttpHandler { public void handle(httpexchange t) throws IOException { t.getresponseheaders().add("accept", "*/*"); Headers h = t.getresponseheaders(); h.add("content-type", "text/html"); StringBuilder sb=new StringBuilder(); for(verysimplemessage vsm:messages){ if(messages.indexof(vsm)!=0){ sb.append(" "); sb.append(vsm); String response = sb.tostring(); t.sendresponseheaders(200, response.length()); OutputStream os = t.getresponsebody(); os.write(response.getbytes()); os.close(); 4
public class VerySimpleMessage { String sender; String receiver; String messagebody; @Override public String tostring() { return sender + "," + receiver + "," + messagebody ; public String getsender() { return sender; public void setsender(string sender) { this.sender = sender; public String getreceiver() { return receiver; public void setreceiver(string receiver) { this.receiver = receiver; public String getmessagebody() { return messagebody; public void setmessagebody(string messagebody) { this.messagebody = messagebody; Webservice typu REST(Restfull)... @Path("/convert") public class PrismaConverter { public static final String ON="on"; @POST @Path("/do") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) 5
@Produces("application/zip") public Response convert(@formparam("filename") String filename, @FormParam("eps") String eps, @FormParam("jpg") String jpg, @FormParam("pdf") String pdf, @FormParam("png") String png, @FormParam("tiff") String tiff, @FormParam("svg") String svg){ try { SVGDocument doc=null; String yourstring=svg; StringReader reader = new StringReader(svg); String uri = "file:/prisma.svg"; try { String parser = XMLResourceDescriptor.getXMLParserClassName(); SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser); doc = f.createsvgdocument(uri,reader); catch (Exception ex) { ex.printstacktrace(); finally { reader.close(); TranscoderInput input = new TranscoderInput(doc); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ZipOutputStream zos = new ZipOutputStream(baos); if(on.equals(pdf)){ ByteArrayOutputStream pdf_ostream = new ByteArrayOutputStream(); TranscoderOutput output_pdf = new TranscoderOutput(pdf_ostream); Transcoder transcoderpdf = new PDFTranscoder(); transcoderpdf.transcode(input, output_pdf); pdf_ostream.flush(); pdf_ostream.close(); ZipEntry entry = new ZipEntry(fileName+".pdf"); entry.setsize(pdf_ostream.size()); zos.putnextentry(entry); zos.write(pdf_ostream.tobytearray()); zos.closeentry(); 6
zos.close(); return Response.ok(baos.toByteArray()).type("application/zip").header("Content-Disposition", "attachment; filename=\""+filename+".zip\"").header("access-control-allow-origin", "*").header("access-control-allow-methods", "GET, POST, DELETE, PUT").allow("OPTIONS").build(); catch (Exception e) { e.printstacktrace(); return null; @GET @Produces(MediaType.TEXT_PLAIN) public String sayplaintexthello() { return "PRISMA SVG CONVERTER"; @GET @Produces(MediaType.TEXT_HTML) public String sayhtmlhello() { return "<html> " + "<title>" + "PRISMA SVG CONVERTER" + "</title>" + "<body><h1>" + "PRISMA SVG CONVERTER" + "</body></h1>" + "</html> "; 7
WEB.XML <?xml version="1.0" encoding="utf-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemalocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="webapp_id" version="3.1"> <display-name>prisma_converter_java</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>jersey REST Service for converting svg into particular formats using batik</servlet-name> <servlet-class>org.glassfish.jersey.servlet.servletcontainer</servlet-class> <!-- Register resources and providers under com.vogella.jersey.first package. -- > <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>com.evidenceprime.prisma</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jersey REST Service for converting svg into particular formats using batik</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app> Webservice typu SOAP/WSDL WSDL <?xml version="1.0" encoding="utf-8"?> <wsdl:definitions xmlns:s="http://www.w3.org/2001/xmlschema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://www.cma.com.pl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 8
xmlns:tm="http://microsoft.com/wsdl/mime/textmatching/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" targetnamespace="http://www.cma.com.pl/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> <s:element name="getlocationsex"> <s:complextype> <s:sequence> <s:element minoccurs="1" maxoccurs="1" name="lngsessionid" type="s:long" /> <s:element minoccurs="1" maxoccurs="1" name="boolforcefetchall" type="s:boolean" /> <s:element minoccurs="1" maxoccurs="1" name="dtadatefrom" type="s:datetime" /> <s:element minoccurs="1" maxoccurs="1" name="dtadateto" type="s:datetime" /> <s:element minoccurs="0" maxoccurs="1" name="arrselecteddevices" type="tns:arrayofint" /> </s:sequence> </s:complextype> </s:element> <s:complextype name="arrayofint"> <s:sequence> <s:element minoccurs="0" maxoccurs="unbounded" name="int" type="s:int" /> </s:sequence> </s:complextype> <s:element name="getlocationsexresponse"> <s:complextype> <s:sequence> <s:element minoccurs="0" maxoccurs="1" name="getlocationsexresult" type="tns:clslocationexportresult" /> </s:sequence> </s:complextype> </s:element> </wsdl:port> </wsdl:service> </wsdl:definitions> DEFINICJA METODY oper = new org.apache.axis.description.operationdesc(); oper.setname("getlocationsex"); param = new org.apache.axis.description.parameterdesc(new javax.xml.namespace.qname("http://www.cma.com.pl/", "lngsessionid"), 9
org.apache.axis.description.parameterdesc.in, new javax.xml.namespace.qname("http://www.w3.org/2001/xmlschema", "long"), long.class, false, false); oper.addparameter(param); param = new org.apache.axis.description.parameterdesc(new javax.xml.namespace.qname("http://www.cma.com.pl/", "boolforcefetchall"), org.apache.axis.description.parameterdesc.in, new javax.xml.namespace.qname("http://www.w3.org/2001/xmlschema", "boolean"), boolean.class, false, false); oper.addparameter(param); param = new org.apache.axis.description.parameterdesc(new javax.xml.namespace.qname("http://www.cma.com.pl/", "dtadatefrom"), org.apache.axis.description.parameterdesc.in, new javax.xml.namespace.qname("http://www.w3.org/2001/xmlschema", "datetime"), java.util.calendar.class, false, false); oper.addparameter(param); param = new org.apache.axis.description.parameterdesc(new javax.xml.namespace.qname("http://www.cma.com.pl/", "dtadateto"), org.apache.axis.description.parameterdesc.in, new javax.xml.namespace.qname("http://www.w3.org/2001/xmlschema", "datetime"), java.util.calendar.class, false, false); oper.addparameter(param); param = new org.apache.axis.description.parameterdesc(new javax.xml.namespace.qname("http://www.cma.com.pl/", "arrselecteddevices"), org.apache.axis.description.parameterdesc.in, new javax.xml.namespace.qname("http://www.cma.com.pl/", "ArrayOfInt"), int[].class, false, false); param.setitemqname(new javax.xml.namespace.qname("http://www.cma.com.pl/", "int")); param.setomittable(true); oper.addparameter(param); oper.setreturntype(new javax.xml.namespace.qname("http://www.cma.com.pl/", "clslocationexportresult")); oper.setreturnclass(pl.com.cma.www.clslocationexportresult.class); oper.setreturnqname(new javax.xml.namespace.qname("http://www.cma.com.pl/", "GetLocationsExResult")); oper.setstyle(org.apache.axis.constants.style.wrapped); oper.setuse(org.apache.axis.constants.use.literal); _operations[1] = oper; 10
DEFINICJA DANYCH /** * ClsLocationExportResult.java * * This file was auto-generated from WSDL * by the Apache Axis 1.4 Apr 22, 2006 (06:55:48 PDT) WSDL2Java emitter. */ package pl.com.cma.www; public class ClsLocationExportResult extends pl.com.cma.www.clsbaseresult implements java.io.serializable { private pl.com.cma.www.strulocation[] arrlocation; public ClsLocationExportResult() { public ClsLocationExportResult( java.lang.string strresult, pl.com.cma.www.strulocation[] arrlocation) { super( strresult); this.arrlocation = arrlocation; /** * Gets the arrlocation value for this ClsLocationExportResult. * * @return arrlocation */ public pl.com.cma.www.strulocation[] getarrlocation() { return arrlocation; /** * Sets the arrlocation value for this ClsLocationExportResult. * * @param arrlocation */ public void setarrlocation(pl.com.cma.www.strulocation[] arrlocation) { this.arrlocation = arrlocation; 11
private java.lang.object equalscalc = null; public synchronized boolean equals(java.lang.object obj) { if (!(obj instanceof ClsLocationExportResult)) return false; ClsLocationExportResult other = (ClsLocationExportResult) obj; if (obj == null) return false; if (this == obj) return true; if ( equalscalc!= null) { return ( equalscalc == obj); equalscalc = obj; boolean _equals; _equals = super.equals(obj) && ((this.arrlocation==null && other.getarrlocation()==null) (this.arrlocation!=null && java.util.arrays.equals(this.arrlocation, other.getarrlocation()))); equalscalc = null; return _equals; private boolean hashcodecalc = false; public synchronized int hashcode() { if ( hashcodecalc) { return 0; hashcodecalc = true; int _hashcode = super.hashcode(); if (getarrlocation()!= null) { for (int i=0; i<java.lang.reflect.array.getlength(getarrlocation()); i++) { java.lang.object obj = java.lang.reflect.array.get(getarrlocation(), i); if (obj!= null &&!obj.getclass().isarray()) { _hashcode += obj.hashcode(); hashcodecalc = false; return _hashcode; 12
// Type metadata private static org.apache.axis.description.typedesc typedesc = new org.apache.axis.description.typedesc(clslocationexportresult.class, true); static { typedesc.setxmltype(new javax.xml.namespace.qname("http://www.cma.com.pl/", "clslocationexportresult")); org.apache.axis.description.elementdesc elemfield = new org.apache.axis.description.elementdesc(); elemfield.setfieldname("arrlocation"); elemfield.setxmlname(new javax.xml.namespace.qname("http://www.cma.com.pl/", "arrlocation")); elemfield.setxmltype(new javax.xml.namespace.qname("http://www.cma.com.pl/", "strulocation")); elemfield.setminoccurs(0); elemfield.setnillable(false); elemfield.setitemqname(new javax.xml.namespace.qname("http://www.cma.com.pl/", "strulocation")); typedesc.addfielddesc(elemfield); /** * Return type metadata object */ public static org.apache.axis.description.typedesc gettypedesc() { return typedesc; /** * Get Custom Serializer */ public static org.apache.axis.encoding.serializer getserializer( java.lang.string mechtype, java.lang.class _javatype, javax.xml.namespace.qname _xmltype) { return new org.apache.axis.encoding.ser.beanserializer( _javatype, _xmltype, typedesc); /** * Get Custom Deserializer 13
*/ public static org.apache.axis.encoding.deserializer getdeserializer( java.lang.string mechtype, java.lang.class _javatype, javax.xml.namespace.qname _xmltype) { return new org.apache.axis.encoding.ser.beandeserializer( _javatype, _xmltype, typedesc); WYWOŁANIE ClsLocationExportResult objresultlocation=null; while(existsunread){ objresultlocation = stub.getlocationsex(session, true, cal1,cal2,null); StruLocation locations[]=objresultlocation.getarrlocation(); existsunread=(objresultlocation!=null && objresultlocation.getarrlocation()!=null && objresultlocation.getarrlocation().length!=0); if(existsunread){ ArrayList<Long> ids=new ArrayList<Long>(); for(strulocation location:locations){ ids.add(location.getlngidlocation()); VehicleGpsLocation VehicleGpsLocation(); vgpsl=new vgpsl.setdatetime(location.getdtaidtime().gettime()); vgpsl.setdescription(location.getstrdescription()); vgpsl.setdirection(location.getintdirection()+0l); vgpsl.setdirection(location.getlngidlocation()); vgpsl.setgpsmonitoringdeviceid(location.getintiddevice()+0l); 14
vgpsl.setheight(location.getintheight()+0l); vgpsl.setlat(location.getdbly()); vgpsl.setlng(location.getdblx()); vgpsl.setlocationid(location.getlngidlocation()); vgpsl.setodometer(location.getintodometer()+0l); vgpsl.setspeed(location.getintspeed()+0l); log.debug( "I'm saving GPS Location with id="+vgpsl.getlocationid()); sessionbeanlocal.saveentity(vgpsl, true); long [] idsarray=new long[ids.size()]; for(int i=0;i<ids.size();i++){ idsarray[i]=ids.get(i); ClsCommitResult commitresult = stub.commitlocations(session, idsarray); log.info( "GPS Locations import finished"); Zdalne wywoływaie procedur CORBA/RPC 15