Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 21-25 of 25 total

An XML-RPC Servlet

// This depends upon the Apache XML-RPC library.

   1  
   2  import java.io.IOException;
   3  import java.io.OutputStream;
   4  
   5  import javax.servlet.ServletConfig;
   6  import javax.servlet.ServletException;
   7  import javax.servlet.http.HttpServlet;
   8  import javax.servlet.http.HttpServletRequest;
   9  import javax.servlet.http.HttpServletResponse;
  10  
  11  import org.apache.commons.logging.Log;
  12  import org.apache.commons.logging.LogFactory;
  13  import org.apache.xmlrpc.XmlRpcServer;
  14  
  15  public class XmlRpcServlet extends HttpServlet {
  16      public class EchoHandler {
  17          public String echo(String input) {
  18              return input;
  19          }
  20      }
  21  	
  22      private XmlRpcServer server = new XmlRpcServer();
  23  
  24      private static Log log = LogFactory.getLog(XmlRpcServlet.class);
  25  
  26  	@Override
  27  	public void init(ServletConfig config) throws ServletException {
  28          server.addHandler("echo", new EchoHandler());
  29  	}
  30  
  31  	@Override
  32  	protected void doGet(HttpServletRequest request, 
  33  			HttpServletResponse response) throws ServletException, IOException {
  34          doPost(request, response);
  35  	}
  36  
  37  	@Override
  38  	protected void doPost(HttpServletRequest request, 
  39  			HttpServletResponse response) throws ServletException, IOException {
  40          byte[] result = server.execute(request.getInputStream());
  41          
  42          response.setContentType("text/xml");
  43          response.setContentLength(result.length);
  44          
  45          OutputStream output = response.getOutputStream();
  46          output.write(result);
  47          output.flush();
  48      }
  49  }


// The following needs to be added to your web.xml file to
// expose the servlet so it may be called remotely.
   1  
   2    <servlet>
   3      <servlet-name>XmlRpcServlet</servlet-name>
   4      <servlet-class>XmlRpcServlet</servlet-class>
   5    </servlet>
   6  
   7    <servlet-mapping>
   8      <servlet-name>XmlRpcServlet</servlet-name>
   9      <url-pattern>/remoteapi</url-pattern>        
  10    </servlet-mapping>    

get-version-ex API

   1  
   2  REBOL [
   3      Title:   "Get Windows Version"
   4      File:    %get-version-ex.r
   5      Author:  "Gregg Irwin"
   6      Email:   greggirwin@acm.org
   7      Version: 0.0.1
   8      Date:    23-sep-2003
   9      Purpose: {
  10          Shows how to call Windows GetVersonEx function.
  11      }
  12      library: [
  13          level:    'intermediate
  14          platform: 'windows
  15          type:     [function how-to]
  16          domain:   [external-library win-api]
  17          tested-under: [view/pro 1.2.8.3.1 on W2K]
  18          support:  none
  19          license:  none
  20          see-also: none
  21      ]
  22  ]
  23  
  24  
  25  ; The credit for this technique of dealing with fixed char arrays
  26  ; in structs belongs to Pekr and Cyphre. I just modded the idea a
  27  ; bit for my own uses.
  28  make-elements: func [name count type /local result][
  29      if not word? type [type: type?/word type]
  30      result: copy "^/"
  31      repeat i count [
  32          append result join name [i " [" type "]" newline]
  33      ]
  34      to block! result
  35  ]
  36  
  37  kernel.dll: load/library %kernel32.dll
  38  
  39  
  40  OSVERSIONINFOEXA: make struct! OSVERSIONINFOEXA-def: compose/deep [
  41      dwOSVersionInfoSize [integer!]  ; DWORD
  42      dwMajorVersion      [integer!]  ; DWORD
  43      dwMinorVersion      [integer!]  ; DWORD
  44      dwBuildNumber       [integer!]  ; DWORD
  45      dwPlatformId        [integer!]  ; DWORD
  46      (make-elements 'szCSDVersion 128 #"@")  ; TCHAR
  47      wServicePackMajor   [short]     ; WORD
  48      wServicePackMinor   [short]     ; WORD
  49      wSuiteMask          [short]     ; WORD
  50      wProductType        [char!]     ; BYTE
  51      wReserved           [char!]     ; BYTE
  52  ] none
  53  OSVERSIONINFOEXA/dwOSVersionInfoSize: length? third OSVERSIONINFOEXA
  54  
  55  GetLastError: make routine! [return: [integer!]] kernel.dll "GetLastError"
  56  
  57  GetVersionEx: make routine! compose/deep/only [
  58      lpVersionInformation    [struct! (OSVERSIONINFOEXA-def)] ;LPOSVERSIONINFO
  59      return:     [integer!]  ;BOOL
  60  ] kernel.dll "GetVersionExA"
  61  
  62  get-version: has [res] [
  63      res: GetVersionEx OSVERSIONINFOEXA
  64      either 0 = res [none][OSVERSIONINFOEXA]
  65  ]
  66  
  67  ; test call
  68  print either res: get-version [
  69      [
  70          "Major:" res/dwMajorVersion newline
  71          "Minor:" res/dwMinorVersion newline
  72          "Build:" res/dwBuildNumber  newline
  73          "SP.Major:" res/wServicePackMajor   newline
  74          "SP.Minor:" res/wServicePackMinor   newline
  75          "Suite"   mold res/wSuiteMask    newline
  76          "Product" mold res/wProductType  newline
  77          "Version:" to-string copy/part at third OSVERSIONINFOEXA 21 128
  78      ]
  79  ][
  80      ["Call failed: " GetLastError]
  81  ]
  82  
  83  free kernel.dll
  84  
  85  halt

Win API drive info functions

   1  
   2  REBOL []
   3  
   4  win-drive: context [
   5      win-lib:  load/library %kernel32.dll
   6  
   7      null-buff: func [
   8          {Returns a null-filled string buffer of the specified length.}
   9          len [integer!]
  10      ][
  11          head insert/dup make string! len #"^@" len
  12      ]
  13  
  14      int-struct: make struct! [
  15          value   [integer!]
  16      ] none
  17  
  18      ; If all the requested information is retrieved,
  19      ; the return value is nonzero.
  20      GetVolumeInformation: make routine! compose/deep [
  21          RootPathName [string!]      ;LPCTSTR  address of root directory of the
  22                                      ;         file system
  23          VolumeNameBuffer [string!]  ;LPTSTR address of name of the volume
  24          VolumeNameSize   [integer!] ;DWORD  length of lpVolumeNameBuffer
  25          VolumeSerialNumber [struct! [(first int-struct)]]  ;LPDWORD// address of volume serial number
  26          MaximumComponentLength [struct! [(first int-struct)]] ;LPDWORD
  27                                         ;// address of system's maximum
  28                                         ;// filename length
  29          FileSystemFlags [struct! [(first int-struct)]]     ; LPDWORD// address of file system flags
  30          FileSystemNameBuffer [string!] ;LPTSTR // address of name of file system
  31          FileSystemNameSize [integer!]  ;DWORD  // length of FileSystemNameBuffer
  32          return: [integer!]
  33      ] win-lib "GetVolumeInformationA"
  34  
  35  
  36      get-volume-info: func [
  37          {Returns a block with volume-name, serial number, max. filename
  38          length, flags, and file system name if successful; otherwise NONE.}
  39          /with root
  40          /local vol-name file-sys-name ser-num max-filename-length flags res
  41      ][
  42          vol-name: null-buff 260
  43          file-sys-name: null-buff 260
  44          ser-num: make struct! int-struct [0]
  45          max-filename-length: make struct! int-struct [0]
  46          flags: make struct! int-struct [0]
  47          root: copy/part to-local-file either with [root][what-dir] 3
  48          res: GetVolumeInformation root
  49              vol-name length? vol-name
  50              ser-num max-filename-length flags
  51              file-sys-name length? file-sys-name
  52  
  53          either res <> 0 [
  54              reduce [trim vol-name ser-num/value max-filename-length/value flags/value trim file-sys-name]
  55          ][
  56              none
  57          ]
  58      ]
  59  
  60  ]
  61  
  62  print mold win-drive/get-volume-info
  63  print mold win-drive/get-volume-info/with %/d/
  64  print mold win-drive/get-volume-info/with %/e/
  65  print mold win-drive/get-volume-info/with %/f/
  66  ;print mold win-drive/get-volume-info/with %/a/
  67  
  68  halt
  69  

Del.icio.us API with python

Taken from del.icio.us-py usage here.
   1  
   2  >>> import delicious
   3  >>> # add a post, user and passwd must be valid values for del.icio.us
   4  >>> delicious.add('user', 'passwd', 'url', 'description', 'tag1 tag2', 
   5                    'extended', '2004-12-31T13:59:59Z')
   6  True
   7  >>> posts = delicious.get('user', 'passwd', count = 1)
   8  >>> # posts is a list containing the post objects, these are dicts
   9  >>> print posts
  10  [{'url': u'url', 'dt': u''2004-12-31T13:59:59Z'', 'extended': u'extended', 
  11    'description': u'description', 'tags': 'tag1 tag2'}]
  12  >>> posts[0]["tags"]
  13  'tag1 tag2'
  14  >>> delicious.delete('user', 'passwd', 'url')
  15  True
  16  >>>
  17  
  18  >>> d = delicious.DeliciousAPI("user", "passwd")
  19  >>> # DeliciousAPI offers all features of the API, see the docs for more
  20  >>> posts = d.posts_all()                 # get all posts by user
  21  >>> posts = d.posts_recent(tag = "tag1", count = 12)
  22  >>> len(posts), type(posts)
  23  (12,  <type 'dict'>)
  24  >>> 
  25  
  26  >>> d = delicious.DeliciousNOTAPI()
  27  >>> # DeliciousNOTAPI offers access to features of delicious, that are not 
  28        accessible via its api
  29  >>> posts = d.get_posts_by_user("username)
  30  >>> posts = d.get_posts_by_tag("tag")
  31  >>>

wsdl the google API (search google with ruby)

sign up to get a key first

   1  
   2  require 'soap/wsdlDriver'
   3  $KCODE = "UTF8"
   4  key = 'LVJnAm5QFHblahblahblah your key here'
   5  
   6  #create driver
   7  wsdl = "http://api.google.com/GoogleSearch.wsdl"
   8  driver = SOAP::WSDLDriverFactory.new(wsdl).create_rpc_driver
   9  query = "your query string here"
  10  start = 0
  11  max = 10
  12    
  13  # see http://dev.ctor.org/soap4r/browser/trunk/sample/wsdl/googleSearch/wsdlDriver.rb
  14  @results = driver.doGoogleSearch( key, query, start, max, true, "", true, 'lang_en', '','')
  15  snippets = @results.resultElements.collect { |r| r.snippet } # you can get all kinds'a' info here
  16  self.update_attribute(:html, snippets.join("\n")) # or whatever

« Newer Snippets
Older Snippets »
Showing 21-25 of 25 total