LightJason - AgentSpeak(L++)
IBaseWeb.java
Go to the documentation of this file.
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 
24 package org.lightjason.agentspeak.action.builtin.web;
25 
26 import org.apache.http.client.methods.HttpGet;
27 import org.apache.http.client.methods.HttpPost;
28 import org.apache.http.client.methods.HttpRequestBase;
29 import org.apache.http.impl.client.HttpClientBuilder;
30 import org.apache.http.util.EntityUtils;
38 
39 import javax.annotation.Nonnull;
40 import javax.annotation.Nullable;
41 import java.io.IOException;
42 import java.util.Collection;
43 import java.util.Map;
44 import java.util.Objects;
45 import java.util.Stack;
46 import java.util.stream.Collectors;
47 import java.util.stream.Stream;
48 
49 
53 public abstract class IBaseWeb extends IBuiltinAction
54 {
58  private static final long serialVersionUID = 4839156213009145751L;
59 
65  protected IBaseWeb( final int p_length )
66  {
67  super( p_length );
68  }
69 
76  protected static HttpPost httppost( @Nonnull final String p_url )
77  {
78  return header( new HttpPost( p_url ) );
79  }
80 
87  protected static HttpGet httpget( @Nonnull final String p_url )
88  {
89  return header( new HttpGet( p_url ) );
90  }
91 
99  private static <T extends HttpRequestBase> T header( @Nonnull final T p_request )
100  {
101  p_request.setHeader(
102  "User-Agent",
103  ( Objects.isNull( System.getProperty( "http.agent" ) ) ) || ( System.getProperty( "http.agent" ).isEmpty() )
104  ? CCommon.PACKAGEROOT + CCommon.configuration().getString( "version" )
105  : System.getProperty( "http.agent" )
106  );
107 
108  return p_request;
109  }
110 
111 
120  @Nonnull
121  protected static String httpgetexecute( @Nonnull final String p_url ) throws IOException
122  {
123  return EntityUtils.toString( HttpClientBuilder.create().build().execute( httpget( p_url ) ).getEntity() );
124  }
125 
133  protected static String httpgetexecute( @Nonnull final HttpGet p_get ) throws IOException
134  {
135  return EntityUtils.toString( HttpClientBuilder.create().build().execute( p_get ).getEntity() );
136  }
137 
145  protected static String httppostexecute( @Nonnull final HttpPost p_post ) throws IOException
146  {
147  return EntityUtils.toString( HttpClientBuilder.create().build().execute( p_post ).getEntity() );
148  }
149 
158  @Nonnull
159  protected static ITerm baseliteral( @Nonnull final Stream<String> p_functor, @Nonnull final Stream<ITerm> p_values )
160  {
161  final Stack<String> l_tree = p_functor.collect( Collectors.toCollection( Stack::new ) );
162 
163  ILiteral l_literal = CLiteral.from( l_tree.pop(), p_values );
164  while ( !l_tree.isEmpty() )
165  l_literal = CLiteral.from( l_tree.pop(), l_literal );
166 
167  return l_literal;
168  }
169 
176  @Nonnull
177  @SuppressWarnings( "unchecked" )
178  protected static Stream<ITerm> flatterm( @Nullable final Object p_object )
179  {
180  if ( ( Objects.isNull( p_object ) ) || ( ( p_object instanceof Map ) && ( ( (Map<String, ?>) p_object ).isEmpty() ) ) )
181  return Stream.empty();
182 
183  return p_object instanceof Map
184  ? flatmap( (Map<String, ?>) p_object )
185  : p_object instanceof Collection
186  ? flatcollection( (Collection) p_object )
187  : Stream.of( CRawTerm.from( p_object ) );
188  }
189 
190 
191 
198  @Nonnull
199  private static Stream<ITerm> flatmap( @Nonnull final Map<String, ?> p_map )
200  {
201  return p_map.entrySet()
202  .stream()
203  .map( i ->
204  CLiteral.from(
205  i.getKey().toLowerCase().replaceAll( "[^([a-z][0-9]\\-/_)]]", "_" ),
206  flatterm( i.getValue() )
207  )
208  );
209  }
210 
217  @Nonnull
218  private static Stream<ITerm> flatcollection( @Nonnull final Collection<?> p_collection )
219  {
220  return p_collection.stream().flatMap( IBaseRest::flatterm );
221  }
222 
223 }
static ResourceBundle configuration()
returns the property data of the package
static Stream< ITerm > flatcollection( @Nonnull final Collection<?> p_collection)
transforms a collection into a term stream
Definition: IBaseWeb.java:218
base class of build-in actions for setting name by package/classname (without prefix character) ...
static HttpGet httpget( @Nonnull final String p_url)
returns a http-get connection
Definition: IBaseWeb.java:87
static Stream< ITerm > flatterm( @Nullable final Object p_object)
converts an object into a term stream
Definition: IBaseWeb.java:178
static String httppostexecute( @Nonnull final HttpPost p_post)
execute http-post request
Definition: IBaseWeb.java:145
static ITerm baseliteral( @Nonnull final Stream< String > p_functor, @Nonnull final Stream< ITerm > p_values)
creates a literal structure from a stream of string elements, the string stream will be build in a tr...
Definition: IBaseWeb.java:159
static String httpgetexecute( @Nonnull final HttpGet p_get)
execute http-get request
Definition: IBaseWeb.java:133
static String httpgetexecute( @Nonnull final String p_url)
execute http-get request
Definition: IBaseWeb.java:121
static HttpPost httppost( @Nonnull final String p_url)
returns a http-post connection
Definition: IBaseWeb.java:76
static Stream< ITerm > flatmap( @Nonnull final Map< String, ?> p_map)
transformas a map into a literal
Definition: IBaseWeb.java:199
default generic literal class for agent beliefs a literal consists of a functor, an optional list of ...
Definition: CLiteral.java:64
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
static ILiteral from( @Nonnull final String p_functor, @Nullable final ITerm... p_values)
factory
Definition: CLiteral.java:161
static final String PACKAGEROOT
package name
base class to read data from the restful service
Definition: IBaseRest.java:42
static< T extends HttpRequestBase > T header( @Nonnull final T p_request)
sets the default header definition
Definition: IBaseWeb.java:99
term structure for simple datatypes
Definition: CRawTerm.java:45