18
18
*/
19
19
20
20
/*
21
- * Copyright (c) 2016, 2020 , Oracle and/or its affiliates. All rights reserved.
21
+ * Copyright (c) 2016, 2024 , Oracle and/or its affiliates. All rights reserved.
22
22
*/
23
23
package opengrok .auth .plugin .configuration ;
24
24
25
- import java .beans .XMLDecoder ;
26
- import java .beans .XMLEncoder ;
27
- import java .io .BufferedInputStream ;
28
- import java .io .BufferedOutputStream ;
29
- import java .io .ByteArrayInputStream ;
30
25
import java .io .ByteArrayOutputStream ;
31
26
import java .io .File ;
32
27
import java .io .FileInputStream ;
35
30
import java .io .OutputStream ;
36
31
import java .io .Serializable ;
37
32
import java .util .ArrayList ;
33
+ import java .util .Arrays ;
38
34
import java .util .List ;
35
+
36
+ import com .fasterxml .jackson .annotation .JsonAutoDetect ;
37
+ import com .fasterxml .jackson .annotation .JsonProperty ;
38
+ import com .fasterxml .jackson .databind .ObjectMapper ;
39
+ import com .fasterxml .jackson .dataformat .yaml .YAMLFactory ;
40
+ import com .fasterxml .jackson .dataformat .yaml .YAMLGenerator ;
39
41
import opengrok .auth .plugin .ldap .LdapServer ;
40
42
import opengrok .auth .plugin .util .WebHooks ;
41
43
42
44
/**
43
45
* Encapsulates configuration for LDAP plugins.
44
46
*/
47
+ @ JsonAutoDetect (
48
+ fieldVisibility = JsonAutoDetect .Visibility .NONE ,
49
+ setterVisibility = JsonAutoDetect .Visibility .NONE ,
50
+ getterVisibility = JsonAutoDetect .Visibility .NONE ,
51
+ isGetterVisibility = JsonAutoDetect .Visibility .NONE ,
52
+ creatorVisibility = JsonAutoDetect .Visibility .NONE
53
+ )
45
54
public class Configuration implements Serializable {
46
55
47
56
private static final long serialVersionUID = -1 ;
48
57
58
+ @ JsonProperty
49
59
private List <LdapServer > servers = new ArrayList <>();
60
+ @ JsonProperty
50
61
private int interval ;
62
+ @ JsonProperty
51
63
private String searchBase ;
64
+ @ JsonProperty
52
65
private WebHooks webHooks ;
66
+ @ JsonProperty
53
67
private int searchTimeout ;
68
+ @ JsonProperty
54
69
private int connectTimeout ;
70
+ @ JsonProperty
55
71
private int readTimeout ;
72
+ @ JsonProperty
56
73
private int countLimit ;
57
74
58
75
public void setServers (List <LdapServer > servers ) {
@@ -119,20 +136,19 @@ public void setSearchBase(String base) {
119
136
this .searchBase = base ;
120
137
}
121
138
122
- public String getXMLRepresentationAsString () {
139
+ String getObjectRepresentationAsString () throws IOException {
123
140
ByteArrayOutputStream bos = new ByteArrayOutputStream ();
124
141
this .encodeObject (bos );
125
142
return bos .toString ();
126
143
}
127
144
128
- private void encodeObject (OutputStream out ) {
129
- try (XMLEncoder e = new XMLEncoder (new BufferedOutputStream (out ))) {
130
- e .writeObject (this );
131
- }
145
+ void encodeObject (OutputStream out ) throws IOException {
146
+ var mapper = new ObjectMapper (new YAMLFactory ().disable (YAMLGenerator .Feature .WRITE_DOC_START_MARKER ));
147
+ mapper .writeValue (out , this );
132
148
}
133
149
134
150
/**
135
- * Read a configuration from a file in XML format .
151
+ * Read a configuration from a file.
136
152
*
137
153
* @param file input file
138
154
* @return the new configuration object
@@ -144,32 +160,8 @@ public static Configuration read(File file) throws IOException {
144
160
}
145
161
}
146
162
147
- /**
148
- * Read a configuration from a string in xml format.
149
- *
150
- * @param xmlconfig input string
151
- * @return the new configuration object
152
- * @throws IOException if any error occurs
153
- */
154
- public static Configuration makeXMLStringAsConfiguration (String xmlconfig ) throws IOException {
155
- final Configuration ret ;
156
- final ByteArrayInputStream in = new ByteArrayInputStream (xmlconfig .getBytes ());
157
- ret = decodeObject (in );
158
- return ret ;
159
- }
160
-
161
- private static Configuration decodeObject (InputStream in ) throws IOException {
162
- final Object ret ;
163
-
164
- try (XMLDecoder d = new XMLDecoder (new BufferedInputStream (in ), null , null ,
165
- new PluginConfigurationClassLoader ())) {
166
- ret = d .readObject ();
167
- }
168
-
169
- if (!(ret instanceof Configuration )) {
170
- throw new IOException ("Not a valid configuration file" );
171
- }
172
-
173
- return (Configuration ) ret ;
163
+ static Configuration decodeObject (InputStream in ) throws IOException {
164
+ var mapper = new ObjectMapper (new YAMLFactory ().disable (YAMLGenerator .Feature .WRITE_DOC_START_MARKER ));
165
+ return mapper .readValue (in , Configuration .class );
174
166
}
175
167
}
0 commit comments