IBaseWeb.java

  1. /*
  2.  * @cond LICENSE
  3.  * ######################################################################################
  4.  * # LGPL License                                                                       #
  5.  * #                                                                                    #
  6.  * # This file is part of the LightJason AgentSpeak(L++)                                #
  7.  * # Copyright (c) 2015-19, LightJason (info@lightjason.org)                            #
  8.  * # This program is free software: you can redistribute it and/or modify               #
  9.  * # it under the terms of the GNU Lesser General Public License as                     #
  10.  * # published by the Free Software Foundation, either version 3 of the                 #
  11.  * # License, or (at your option) any later version.                                    #
  12.  * #                                                                                    #
  13.  * # This program is distributed in the hope that it will be useful,                    #
  14.  * # but WITHOUT ANY WARRANTY; without even the implied warranty of                     #
  15.  * # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                      #
  16.  * # GNU Lesser General Public License for more details.                                #
  17.  * #                                                                                    #
  18.  * # You should have received a copy of the GNU Lesser General Public License           #
  19.  * # along with this program. If not, see http://www.gnu.org/licenses/                  #
  20.  * ######################################################################################
  21.  * @endcond
  22.  */

  23. package org.lightjason.agentspeak.action.builtin.web;

  24. import org.apache.http.client.methods.HttpGet;
  25. import org.apache.http.client.methods.HttpPost;
  26. import org.apache.http.client.methods.HttpRequestBase;
  27. import org.apache.http.impl.client.HttpClientBuilder;
  28. import org.apache.http.util.EntityUtils;
  29. import org.lightjason.agentspeak.action.builtin.IBuiltinAction;
  30. import org.lightjason.agentspeak.action.builtin.web.rest.IBaseRest;
  31. import org.lightjason.agentspeak.common.CCommon;
  32. import org.lightjason.agentspeak.language.CLiteral;
  33. import org.lightjason.agentspeak.language.CRawTerm;
  34. import org.lightjason.agentspeak.language.ILiteral;
  35. import org.lightjason.agentspeak.language.ITerm;

  36. import javax.annotation.Nonnull;
  37. import javax.annotation.Nullable;
  38. import java.io.IOException;
  39. import java.util.Collection;
  40. import java.util.Map;
  41. import java.util.Objects;
  42. import java.util.Stack;
  43. import java.util.stream.Collectors;
  44. import java.util.stream.Stream;


  45. /**
  46.  * web base action class
  47.  */
  48. public abstract class IBaseWeb extends IBuiltinAction
  49. {
  50.     /**
  51.      * serial id
  52.      */
  53.     private static final long serialVersionUID = 4839156213009145751L;

  54.     /**
  55.      * ctor
  56.      *
  57.      * @param p_length length
  58.      */
  59.     protected IBaseWeb( final int p_length )
  60.     {
  61.         super( p_length );
  62.     }

  63.     /**
  64.      * returns a http-post connection
  65.      *
  66.      * @param p_url url
  67.      * @return request
  68.      */
  69.     protected static HttpPost httppost( @Nonnull final String p_url )
  70.     {
  71.         return header( new HttpPost( p_url ) );
  72.     }

  73.     /**
  74.      * returns a http-get connection
  75.      *
  76.      * @param p_url url
  77.      * @return request
  78.      */
  79.     protected static HttpGet httpget( @Nonnull final String p_url )
  80.     {
  81.         return header( new HttpGet( p_url ) );
  82.     }

  83.     /**
  84.      * sets the default header definition
  85.      *
  86.      * @param p_request request
  87.      * @tparam T request type
  88.      * @return input request
  89.      */
  90.     private static <T extends HttpRequestBase> T header( @Nonnull final T p_request )
  91.     {
  92.         p_request.setHeader(
  93.             "User-Agent",
  94.             ( Objects.isNull( System.getProperty( "http.agent" ) ) ) || ( System.getProperty( "http.agent" ).isEmpty() )
  95.             ? CCommon.PACKAGEROOT + CCommon.configuration().getString( "version" )
  96.             : System.getProperty( "http.agent" )
  97.         );

  98.         return p_request;
  99.     }


  100.     /**
  101.      * execute http-get request
  102.      *
  103.      * @param p_url url
  104.      * @return url data
  105.      *
  106.      * @throws IOException is thrown on connection errors
  107.      */
  108.     @Nonnull
  109.     protected static String httpgetexecute( @Nonnull final String p_url ) throws IOException
  110.     {
  111.         return EntityUtils.toString( HttpClientBuilder.create().build().execute( httpget( p_url ) ).getEntity() );
  112.     }

  113.     /**
  114.      * execute http-get request
  115.      *
  116.      * @param p_get get request
  117.      * @return output data as string
  118.      * @throws IOException is thrown on connection errors
  119.      */
  120.     protected static String httpgetexecute( @Nonnull final HttpGet p_get ) throws IOException
  121.     {
  122.         return EntityUtils.toString( HttpClientBuilder.create().build().execute( p_get ).getEntity() );
  123.     }

  124.     /**
  125.      * execute http-post request
  126.      *
  127.      * @param p_post post request
  128.      * @return output data as string
  129.      * @throws IOException is thrown on connection errors
  130.      */
  131.     protected static String httppostexecute( @Nonnull final HttpPost p_post ) throws IOException
  132.     {
  133.         return EntityUtils.toString( HttpClientBuilder.create().build().execute( p_post ).getEntity() );
  134.     }

  135.     /**
  136.      * creates a literal structure from a stream of string elements,
  137.      * the string stream will be build in a tree structure
  138.      *
  139.      * @param p_functor stream with functor strings
  140.      * @param p_values value stream
  141.      * @return term
  142.      */
  143.     @Nonnull
  144.     protected static ITerm baseliteral( @Nonnull final Stream<String> p_functor, @Nonnull final Stream<ITerm> p_values )
  145.     {
  146.         final Stack<String> l_tree = p_functor.collect( Collectors.toCollection( Stack::new ) );

  147.         ILiteral l_literal = CLiteral.from( l_tree.pop(), p_values );
  148.         while ( !l_tree.isEmpty() )
  149.             l_literal = CLiteral.from( l_tree.pop(), l_literal );

  150.         return l_literal;
  151.     }

  152.     /**
  153.      * converts an object into a term stream
  154.      *
  155.      * @param p_object object
  156.      * @return term stream
  157.      */
  158.     @Nonnull
  159.     @SuppressWarnings( "unchecked" )
  160.     protected static Stream<ITerm> flatterm( @Nullable final Object p_object )
  161.     {
  162.         if ( ( Objects.isNull( p_object ) ) || ( ( p_object instanceof Map ) && ( ( (Map<String, ?>) p_object ).isEmpty() ) ) )
  163.             return Stream.empty();

  164.         return p_object instanceof Map
  165.                ? flatmap( (Map<String, ?>) p_object )
  166.                : p_object instanceof Collection
  167.                  ? flatcollection( (Collection) p_object )
  168.                  : Stream.of( CRawTerm.from( p_object ) );
  169.     }



  170.     /**
  171.      * transformas a map into a literal
  172.      *
  173.      * @param p_map input map
  174.      * @return term stream
  175.      */
  176.     @Nonnull
  177.     private static Stream<ITerm> flatmap( @Nonnull final Map<String, ?> p_map )
  178.     {
  179.         return p_map.entrySet()
  180.                     .stream()
  181.                     .map( i ->
  182.                               CLiteral.from(
  183.                                   i.getKey().toLowerCase().replaceAll( "[^([a-z][0-9]\\-/_)]]", "_" ),
  184.                                   flatterm( i.getValue() )
  185.                               )
  186.                     );
  187.     }

  188.     /**
  189.      * transforms a collection into a term stream
  190.      *
  191.      * @param p_collection collection
  192.      * @return term stream
  193.      */
  194.     @Nonnull
  195.     private static Stream<ITerm> flatcollection( @Nonnull final Collection<?> p_collection )
  196.     {
  197.         return p_collection.stream().flatMap( IBaseRest::flatterm );
  198.     }

  199. }