View Javadoc
1   package org.apache.maven.search.backend.smo.internal;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.util.Properties;
25  
26  import org.apache.maven.search.SearchRequest;
27  
28  /**
29   * A trivial "transport abstraction" to make possible pluggable implementations.
30   */
31  public abstract class SmoSearchTransportSupport
32  {
33      private final String clientVersion;
34  
35      public SmoSearchTransportSupport()
36      {
37          this.clientVersion = discoverVersion();
38      }
39  
40      private String discoverVersion()
41      {
42          Properties properties = new Properties();
43          InputStream inputStream = getClass().getClassLoader().getResourceAsStream( "smo-version.properties" );
44          if ( inputStream != null )
45          {
46              try ( InputStream is = inputStream )
47              {
48                  properties.load( is );
49              }
50              catch ( IOException e )
51              {
52                  // fall through
53              }
54          }
55          return properties.getProperty( "version", "unknown" );
56      }
57  
58      /**
59       * Exposes this backend version, for example to be used in HTTP {@code User-Agent} string, never {@code null}.
60       */
61      protected String getClientVersion()
62      {
63          return clientVersion;
64      }
65  
66      /**
67       * Exposes full HTTP {@code User-Agent} string ready to be used by HTTP clients, never {@code null}.
68       */
69      protected String getUserAgent()
70      {
71          return "Apache Search SMO/" + getClientVersion();
72      }
73  
74      /**
75       * This method should issue a HTTP GET requests using {@code serviceUri} and return body payload as {@link String}
76       * ONLY if the response was HTTP 200 Ok and there was a payload returned by service. In any other case, it should
77       * throw, never return {@code null}. The payload is expected to be {@code application/json}, so client may add
78       * headers to request. Also, the payload is expected to be "relatively small".
79       */
80      public abstract String fetch( SearchRequest searchRequest, String serviceUri ) throws IOException;
81  }