forked from jbosstools/jbosstools-base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EclipseUserAgent.java
132 lines (109 loc) · 3.69 KB
/
EclipseUserAgent.java
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
124
125
126
127
128
129
130
131
132
/*******************************************************************************
* Copyright (c)
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is 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:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package org.jboss.tools.usage.googleanalytics.eclipse;
import java.text.MessageFormat;
import org.eclipse.core.runtime.IProduct;
import org.eclipse.core.runtime.Platform;
import org.osgi.framework.Bundle;
/**
* Emulates a Firefox Browser User-Agent String but reports eclipse related
* informations.
*
* @author Andre Dietisheim
*/
public class EclipseUserAgent implements IEclipseUserAgent {
public static final char BROWSER_LOCALE_DELIMITER = '-';
public static final char JAVA_LOCALE_DELIMITER = '_';
private static final String ECLIPSE_RUNTIME_BULDEID = "org.eclipse.core.runtime"; //$NON-NLS-1$
private static final String USERAGENT_WIN = "{0}/{1} (Windows; U; Windows NT {2}; {3})"; //$NON-NLS-1$
private static final String USERAGENT_MAC = "{0}/{1} (Macintosh; U; Intel Mac OS X {2}; {3})"; //$NON-NLS-1$
private static final String USERAGENT_LINUX = "{0}/{1} (X11; U; Linux {2}; {3})"; //$NON-NLS-1$
public static final char VERSION_DELIMITER = '.'; //$NON-NLS-1$
private static final String PROP_OS_VERSION = "os.version"; //$NON-NLS-1$
private String browserLanguage;
private String createBrowserLanguage() {
String nl = getNL();
if (nl == null) {
return ""; //$NON-NLS-1$
}
int indexOf = nl.indexOf(JAVA_LOCALE_DELIMITER); //$NON-NLS-1$
if (indexOf <= 0) {
return nl;
}
StringBuilder builder = new StringBuilder();
builder.append(nl.substring(0, indexOf));
builder.append(BROWSER_LOCALE_DELIMITER);
builder.append(nl.substring(indexOf + 1));
return builder.toString();
}
protected String getNL() {
return Platform.getNL();
}
public String getBrowserLanguage() {
if (browserLanguage == null) {
browserLanguage = createBrowserLanguage();
}
return browserLanguage;
}
public String toString() {
String productId = getApplicationName();
String productVersion = getApplicationVersion();
return MessageFormat.format(
getUserAgentPattern(getOS())
, productId
, productVersion
, getOSVersion()
, getBrowserLanguage()
);
}
public String getOS() {
return Platform.getOS();
}
public String getOSVersion() {
return System.getProperty(PROP_OS_VERSION);
}
private String getUserAgentPattern(String os) {
String userAgentPattern = ""; //$NON-NLS-1$
if (Platform.OS_LINUX.equals(os)) {
return USERAGENT_LINUX;
} else if (Platform.OS_MACOSX.equals(os)) {
return USERAGENT_MAC;
} else if (Platform.OS_WIN32.equals(os)) {
return USERAGENT_WIN;
}
return userAgentPattern;
}
public String getApplicationName() {
return getApplicationBundle().getSymbolicName();
}
public String getApplicationVersion() {
String fullVersion = getApplicationBundle().getVersion().toString();
int productVersionStart = fullVersion.lastIndexOf(VERSION_DELIMITER);
if (productVersionStart > 0) {
return fullVersion.substring(0, productVersionStart);
} else {
return fullVersion;
}
}
/**
* Returns the bundle that launched the application that this class runs in.
*
* @return the defining bundle
*/
private Bundle getApplicationBundle() {
IProduct product = Platform.getProduct();
if (product != null) {
return product.getDefiningBundle();
} else {
return Platform.getBundle(ECLIPSE_RUNTIME_BULDEID);
}
}
}