Saturday, December 11, 2010

SAP: cURLing a BSP through GuiXT

Following on from my post about cURLing a google static map then I did follow up with a yahoo based method.

The reasons to try another way to get a map onto a SAP screen was to test out BSP, REST based Yahoo service and to learn more about SAP.

Why Yahoo and not Google? In my opnion Google does have better maps particularly Japan, however reading "terms of use" and at one time it appeared that Yahoo would allow free use. I have not studied the terms of use in detail recently however its not as free as I would like! However I completed the Yahoo based way.

cURLing through a proxy
cURL does allow you to follow redirection (-L option) however I hit a proxy issue where the local SAP server was accessible with a direct connection but the yahoo service required a proxy connection. This is were I failed to get cURL to work.

So I found my work around

Setup the BSP to cache the yahoo image in SAP, to allow direct cURL download.

BSP to Use Yahoo's Static Map API

I created a BSP in se80 called ZJZIP and eventually ended up with these page attributes for the main map.htm page.



Error Handling

The google static map example I posted earlier does not allow much error checking for the image. As this example is a BSP I checked the Yahoo API xml for any errors. The simple transformation used, which appears later, returns either the URL for the image or an error code. Therefore I added an error.gif to the MIME section of the BSP.







The layout of the BSP would appear in se80 as follows.


















So onto the Event Handler OnInitilization code

navigation->set_parameter( 'postcode' ).
navigation->set_parameter( 'height' ).
navigation->set_parameter( 'width' ).
navigation->set_parameter( 'zoom' ).
navigation->set_parameter( 'out' ).
if postcode is initial.
  postcode = 'b904su'.
endif.
if height is initial.
  height = '200'.
endif.
if width is initial.
  width = '200'.
endif.
if zoom is initial.
  zoom = '5'.
endif.
if out is initial.
  out = 'gif'.
endif.
DATA: pict_url type string,
      error_msg type string.
DATA err_url type string.
DATA: urlget TYPE STRING,
      urlstub TYPE STRING,
      urlstub1 type string,
      urlstub2 type string,
      urlstub3 type string,
      urlstub4 type string,
      urlstub5 type string,
      http_client TYPE REF TO IF_HTTP_CLIENT,
      return_code TYPE I,
       p_filnam type string,
      content TYPE STRING.
urlstub = 'http://local.yahooapis.com/MapsService/V1/mapImage?appid=ENTER-YOUR-OWN-YAHOO-APP-ID'.
urlstub1 = '&image_type='.
urlstub2 = '&image_height='.
urlstub3 = '&image_width='.
urlstub4 = '&zoom='.
urlstub5 = '&zip='.
concatenate urlstub urlstub1 out urlstub2 height urlstub3 width urlstub4 zoom urlstub5 postcode into urlget.
cl_http_client=>create_by_url(
  EXPORTING url = urlget
  proxy_host = 'PROXY.SERVER.ADDRESS'
  proxy_service = 'PROXY_PORT'
  IMPORTING client = http_client ).
http_client->send( ).
http_client->receive( ).
http_client->response->get_status( IMPORTING code = return_code ).
content = http_client->response->get_cdata( ).
http_client->close( ).
data: xml_string type xstring.
DATA: gs_rif_ex     TYPE REF TO cx_root,
      gs_var_text   TYPE string.
TRY.
    CALL TRANSFORMATION  ZJ_YAH_geopict
        SOURCE XML  content
        RESULT ROOT = pict_url
              EROOT = error_msg.
  CATCH cx_root INTO gs_rif_ex.
    gs_var_text = gs_rif_ex->get_text( ).
    MESSAGE gs_var_text TYPE 'E'.
ENDTRY.
if proxy <> 'y'.
if pict_url is initial.
  d_pc = 'ERROR: could not process address'.
err_url = 'http://F.Q.D.N.HERE:8000/sap/bc/bsp/zjzip/error.gif'.
  navigation->goto_page( err_url ).
else.
  DATA URL1 type string.
  url1 =   pict_url.
  d_pc = pict_url.
data:   file_length    TYPE  STRING,
  file_mime_type TYPE  STRING,
  file_name      TYPE  STRING,
  file_content   TYPE  XSTRING,
  display_type   TYPE  STRING.
cl_http_client=>create_by_url(
  EXPORTING url = d_pc
  proxy_host = 'PROXY.SERVER.ADDRESS'
  proxy_service = 'PROXY_PORT'
  IMPORTING client = http_client ).
http_client->send( ).
http_client->receive( ).
http_client->response->get_status( IMPORTING code = return_code ).
bit = http_client->response->get_data( ).
http_client->close( ).
postcode = XSTRLEN( bit ).
IF  XSTRLEN( bit ) > 0.
  DATA: cached_response TYPE REF TO if_http_response.
  CREATE OBJECT cached_response TYPE CL_HTTP_RESPONSE EXPORTING add_c_msg = 1.
  cached_response->set_data( bit ).
   cached_response->set_header_field( name  = if_http_header_fields=>content_type
                                       value = 'image/gif' ).
    cached_response->set_status( code = 200 reason = 'OK' ).
    cached_response->server_cache_expire_rel( expires_rel = 180 ).
    DATA: guid TYPE guid_32.
    CALL FUNCTION 'GUID_CREATE' IMPORTING ev_guid_32 = guid.
    CONCATENATE runtime->application_url '/' guid '.gif' INTO display_url.
    cl_http_server=>server_cache_upload( url      = display_url
                                         response = cached_response ).
response->redirect( display_url ).
else.
display_url = runtime->application_url.
  ENDIF.
endif.
endif.


Use transaction xlst_tool for the...(not so)
Simple Transformations

<?sap.transform simple?>
<tt:transform xmlns:tt="http://www.sap.com/transformation-templates" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
  <tt:root name="ROOT"/>
  <tt:root name="EROOT"/>
  <tt:template>
    <tt:d-cond>
      <Result>
        <tt:value ref="ROOT"/>
      </Result>
    </tt:d-cond>
    <tt:d-cond>
      <Error>
          <Message><tt:value ref="EROOT"/></Message>
      </Error>
    </tt:d-cond>
  </tt:template>
</tt:transform>



Testing it.....

Refer to the post cURLing a google static map

The following changes would be required for the BSP controlled Yahoo image.

In STEP THREE
Change the jpg format to gif


In STEP FIVE
Add the option -L to allow redirection and the F.Q.D.N is the SAP server fully qualified domain name. The PORT needs to be changed to the appropriate value.

c:\guixt\curlbin\curl -L http://F.Q.D.N.:PORT/sap/bc/bsp/sap/zjzip/map.htm? -d "postcode=%1,%2"  -d width=200 -d height=200 -d zoom=5   -G >c:\GuiXT\img\%1.gif

The END Result.











More to come on the geocode side of things.....

No comments:

Post a Comment

Google +