forked from eclipse-sirius/sirius-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelectOptionIconURLDataFetcher.java
53 lines (49 loc) · 2.31 KB
/
SelectOptionIconURLDataFetcher.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
/*******************************************************************************
* Copyright (c) 2023 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.components.forms.graphql.datafetchers.form;
import java.util.regex.Pattern;
import org.eclipse.sirius.components.annotations.spring.graphql.QueryDataFetcher;
import org.eclipse.sirius.components.forms.SelectOption;
import org.eclipse.sirius.components.graphql.api.IDataFetcherWithFieldCoordinates;
import org.eclipse.sirius.components.graphql.api.URLConstants;
import graphql.schema.DataFetchingEnvironment;
/**
* Data fetcher for SelectOption.iconURL, to rewrite the relative path of the image into an absolute path on the server.
* <p>
* If the <code>SelectOption.iconURL</code> is of the form <code>path/to/image.svg</code>, the rewritten value which will
* be seen by the frontend will be <code>/api/images/path/to/image.svg</code>.
*
* If the <code>SelectOption.iconURL</code> is of the form <code>path/to/UUID.svg</code>, the rewritten value which will
* be seen by the frontend will be <code>/api/images/custom/path/to/image.svg</code>.
*
* @author mcharfadi
*/
@QueryDataFetcher(type = "SelectOption", field = "iconURL")
public class SelectOptionIconURLDataFetcher implements IDataFetcherWithFieldCoordinates<String> {
private static final Pattern UUID_REGEX =
Pattern.compile("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$");
@Override
public String get(DataFetchingEnvironment environment) throws Exception {
SelectOption option = environment.getSource();
String result = option.getIconURL();
if (result != null && !result.isBlank()) {
if (UUID_REGEX.matcher(result).matches()) {
result = URLConstants.IMAGE_BASE_PATH + "/custom/" + result;
}
else {
result = URLConstants.IMAGE_BASE_PATH + result;
}
}
return result;
}
}