Skip to main content
summaryrefslogtreecommitdiffstats
blob: 1c08abdac013edccb59b72b95f3fab3431c62896 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/

package org.eclipse.osee.framework.core.client.server;

import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.logging.Level;
import org.eclipse.osee.framework.core.client.CoreClientConstants;
import org.eclipse.osee.framework.core.client.OseeClientProperties;
import org.eclipse.osee.framework.core.client.internal.CoreClientActivator;
import org.eclipse.osee.framework.core.exception.OseeExceptions;
import org.eclipse.osee.framework.database.core.OseeInfo;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.jdk.core.type.OseeStateException;
import org.eclipse.osee.framework.jdk.core.util.HttpUrlBuilder;
import org.eclipse.osee.framework.jdk.core.util.Strings;
import org.eclipse.osee.framework.logging.OseeLog;

/**
 * @author Roberto E. Escobar
 */
public final class HttpUrlBuilderClient {

   public static final String USE_CONNECTED_SERVER_URL_FOR_PERM_LINKS =
      CoreClientConstants.getBundleId() + "osee.use.connected.server.url.for.perm.links";

   private static final String urlPrefixFormat = "http://%s:%s/";
   private static final HttpUrlBuilderClient instance = new HttpUrlBuilderClient();

   private HttpUrlBuilderClient() {
      // Singleton
   }

   public static HttpUrlBuilderClient getInstance() {
      return instance;
   }

   public String getUrlForLocalSkynetHttpServer(String context, Map<String, String> parameters) throws OseeStateException {
      try {
         return HttpUrlBuilder.createURL(getHttpLocalServerPrefix(), context, parameters);
      } catch (UnsupportedEncodingException ex) {
         OseeLog.log(CoreClientActivator.class, Level.SEVERE, ex);
      }
      return null;
   }

   public String getHttpLocalServerPrefix() throws OseeStateException {
      int port = HttpServer.getDefaultServicePort();
      if (port == -1) {
         throw new OseeStateException("Http Server was not launched by this workbench - Ensure port was set correctly");
      }
      return String.format(urlPrefixFormat, HttpServer.getLocalServerAddress(), port);
   }

   public String getApplicationServerPrefix() throws OseeCoreException {
      String address = OseeClientProperties.getOseeApplicationServer();
      return normalize(address);
   }

   public String getPermanentBaseUrl() throws OseeCoreException {
      String address = OseeInfo.getValue("osee.permanent.base.url");
      return normalize(address);
   }

   public String getSelectedPermanenrLinkUrl() throws OseeCoreException {
      boolean isUseConnectedServerUrl =
         CoreClientActivator.getInstance().getPluginPreferences().getBoolean(USE_CONNECTED_SERVER_URL_FOR_PERM_LINKS);
      String address = null;
      if (isUseConnectedServerUrl) {
         address = getApplicationServerPrefix();
      } else {
         try {
            address = getPermanentBaseUrl();
         } catch (Exception ex) {
            OseeLog.log(CoreClientActivator.class, Level.WARNING, ex);
         }
         if (!Strings.isValid(address)) {
            address = getApplicationServerPrefix();
         }
      }
      return normalize(address);
   }

   private static String normalize(String address) {
      String toReturn = address;
      if (Strings.isValid(toReturn) && !toReturn.endsWith("/")) {
         toReturn += "/";
      }
      return toReturn;
   }

   public String getOsgiServletServiceUrl(String context, Map<String, String> parameters) throws OseeCoreException {
      String url = null;
      try {
         String applicationServerPrefix = getApplicationServerPrefix();
         if (Strings.isValid(applicationServerPrefix)) {
            url = HttpUrlBuilder.createURL(applicationServerPrefix, context, parameters);
         }
      } catch (UnsupportedEncodingException ex) {
         OseeExceptions.wrapAndThrow(ex);
         return null; // unreachable since wrapAndThrow() always throws an exception
      }
      return url;
   }

   public String getPermanentLinkBaseUrl(String context, Map<String, String> parameters) throws OseeCoreException {
      try {
         return HttpUrlBuilder.createURL(getSelectedPermanenrLinkUrl(), context, parameters);
      } catch (UnsupportedEncodingException ex) {
         OseeExceptions.wrapAndThrow(ex);
         return null; // unreachable since wrapAndThrow() always throws an exception
      }
   }
}

Back to the top