1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package fr.cnes.doi.resource.citation;
20
21 import fr.cnes.doi.application.AbstractApplication;
22 import fr.cnes.doi.exception.ClientCrossCiteException;
23 import fr.cnes.doi.utils.spec.Requirement;
24 import java.util.Arrays;
25 import org.apache.logging.log4j.Level;
26 import org.restlet.data.MediaType;
27 import org.restlet.data.Method;
28 import org.restlet.data.Status;
29 import org.restlet.ext.wadl.MethodInfo;
30 import org.restlet.ext.wadl.ParameterStyle;
31 import org.restlet.resource.Get;
32 import org.restlet.resource.ResourceException;
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public class FormatCitationResource extends BaseCitationResource {
47
48
49
50
51 private volatile String doiName;
52
53
54
55
56 private volatile String style;
57
58
59
60
61 private volatile String language;
62
63
64
65
66
67
68 @Override
69 protected void doInit() throws ResourceException {
70 super.doInit();
71 LOG.traceEntry();
72 this.doiName = getQueryValue(DOI_PARAMETER);
73 this.language = getQueryValue(LANG_PARAMETER);
74 this.style = getQueryValue(STYLE_PARAMETER);
75
76 LOG.debug("DOI Parameter : " + this.doiName);
77 LOG.debug("LANGUAGE Parameter : " + this.language);
78 LOG.debug("STYLE Parameter : " + this.language);
79
80 final StringBuilder description = new StringBuilder();
81 description.append("CrossRef, DataCite and mEDRA support formatted "
82 + "citations via the text/bibliography content type. These are "
83 + "the output of the Citation Style Language processor, "
84 + "citeproc-js. The content type can take two additional "
85 + "parameters to customise its response format.");
86 description.append("\"style\" can be chosen from the list of style names"
87 + " found in the CSL style repository. Many styles are supported,"
88 + " including common styles such as apa and harvard3");
89 setDescription(description.toString());
90 LOG.traceExit();
91 }
92
93
94
95
96
97
98
99
100
101 @Requirement(reqId = Requirement.DOI_SRV_120, reqName = Requirement.DOI_SRV_120_NAME)
102 @Requirement(reqId = Requirement.DOI_MONIT_020, reqName = Requirement.DOI_MONIT_020_NAME)
103 @Get
104 public String getFormat() throws ResourceException {
105 LOG.traceEntry();
106 try {
107 checkInputs();
108 final String result = this.getApp().getClient().getFormat(
109 this.doiName, this.style, this.language
110 );
111 return LOG.traceExit(result);
112 } catch (ClientCrossCiteException ex) {
113 ((AbstractApplication) getApplication()).sendAlertWhenDataCiteFailed(ex);
114 throw LOG.throwing(Level.ERROR, new ResourceException(ex.getStatus(), ex.
115 getDetailMessage(), ex));
116 }
117 }
118
119
120
121
122
123
124
125 private void checkInputs() throws ResourceException {
126 LOG.traceEntry();
127 final StringBuilder errorMsg = new StringBuilder();
128 if (this.doiName == null || this.doiName.isEmpty()) {
129 errorMsg.append(DOI_PARAMETER).append(" value is not set.");
130 }
131 if (this.language == null || this.language.isEmpty()) {
132 errorMsg.append(LANG_PARAMETER).append(" value is not set.");
133 }
134 if (this.style == null || this.style.isEmpty()) {
135 errorMsg.append(STYLE_PARAMETER).append(" value is not set.");
136 }
137 if (errorMsg.length() == 0) {
138 LOG.debug("The parameters are valid");
139 } else {
140 throw LOG.throwing(Level.ERROR, new ResourceException(
141 Status.CLIENT_ERROR_BAD_REQUEST, errorMsg.toString()));
142 }
143 LOG.traceExit();
144 }
145
146
147
148
149
150
151 @Requirement(reqId = Requirement.DOI_DOC_010, reqName = Requirement.DOI_DOC_010_NAME)
152 @Override
153 protected final void describeGet(final MethodInfo info) {
154 info.setName(Method.GET);
155 info.setDocumentation("Select Formatting Style");
156 addRequestDocToMethod(info, Arrays.asList(
157 createQueryParamDoc(DOI_PARAMETER, ParameterStyle.QUERY,
158 "doi to format for the citation", true, "xs:string"),
159 createQueryParamDoc(LANG_PARAMETER, ParameterStyle.QUERY,
160 "language for the citation formating", true, "xs:string"),
161 createQueryParamDoc(STYLE_PARAMETER, ParameterStyle.QUERY,
162 "style fot the citation formating", true, "xs:string")
163 ));
164 addResponseDocToMethod(info, createResponseDoc(
165 Status.SUCCESS_OK,
166 "Operation successful",
167 listRepresentation("Format representation", MediaType.TEXT_PLAIN,
168 "The formatted citation"))
169 );
170 addResponseDocToMethod(info, createResponseDoc(
171 Status.CLIENT_ERROR_NOT_FOUND,
172 "DOI not found",
173 listRepresentation("Error representation",
174 MediaType.TEXT_HTML, "Error"))
175 );
176 addResponseDocToMethod(info, createResponseDoc(
177 Status.CLIENT_ERROR_BAD_REQUEST,
178 "Wrong input parameters",
179 listRepresentation("Error representation",
180 MediaType.TEXT_HTML, "Error"))
181 );
182 }
183
184 }