Skip to content

Commit

Permalink
Add application ID in the JSON logs
Browse files Browse the repository at this point in the history
Remove checks style that should be down by a code formatter
  • Loading branch information
sbrunner committed May 17, 2023
1 parent c3e7c31 commit c836fdd
Show file tree
Hide file tree
Showing 101 changed files with 266 additions and 178 deletions.
10 changes: 0 additions & 10 deletions checkstyle_checks.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,12 @@
<property name="checkFormat" value="$1"/>
</module>
</module>
<module name="LineLength">
<property name="max" value="110"/>
</module>
<module name="NewlineAtEndOfFile"/>
<module name="JavadocPackage"/>
<module name="Translation"/>
<module name="FileLength"/>
<module name="FileTabCharacter">
<metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
</module>
<module name="RegexpSingleline">
<property name="severity" value="ignore"/>
<property name="format" value="\s+$"/>
<property name="message" value="Line has trailing spaces."/>
<metadata name="net.sf.eclipsecs.core.lastEnabledSeverity" value="inherit"/>
</module>
<module name="SuppressionFilter">
<property name="file" value="${basedir}/checkstyle_suppressions.xml"/>
</module>
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/org/mapfish/print/MapPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ public final OutputFormat getOutputFormat(final PJsonObject specJson) {
* @param out the stream to write to.
*/
public final Processor.ExecutionContext print(
final String jobId, final PJsonObject specJson, final OutputStream out)
final String applicationId, final String jobId, final PJsonObject specJson, final OutputStream out)
throws Exception {
final OutputFormat format = getOutputFormat(specJson);
final File taskDirectory = this.workingDirectories.getTaskDirectory();

try {
return format.print(jobId, specJson, getConfiguration(), this.configFile.getParentFile(),
return format.print(applicationId, jobId, specJson, getConfiguration(), this.configFile.getParentFile(),
taskDirectory, out);
} finally {
this.workingDirectories.removeDirectory(taskDirectory);
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/org/mapfish/print/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ private void run(final CliDefinition cli) throws Exception {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Request Data: \n{}", jsonSpec.getInternalObj().toString(2));
}
this.mapPrinter.print("main", jsonSpec, outFile);
this.mapPrinter.print(null, "main", jsonSpec, outFile);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public final class ConfigFileResolvingHttpRequestFactory implements MfClientHttp
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigFileResolvingHttpRequestFactory.class);
private final Configuration config;
private final String jobId;
private final String applicationId;
private final MfClientHttpRequestFactoryImpl httpRequestFactory;
private final List<RequestConfigurator> callbacks = new CopyOnWriteArrayList<>();

Expand All @@ -50,9 +51,10 @@ public final class ConfigFileResolvingHttpRequestFactory implements MfClientHttp
*/
public ConfigFileResolvingHttpRequestFactory(
final MfClientHttpRequestFactoryImpl httpRequestFactory,
final Configuration config, final String jobId) {
final Configuration config, final String applicationId, final String jobId) {
this.httpRequestFactory = httpRequestFactory;
this.config = config;
this.applicationId = applicationId;
this.jobId = jobId;
}

Expand Down Expand Up @@ -105,9 +107,11 @@ private synchronized ClientHttpRequest createRequestFromWrapped(final HttpHeader
protected synchronized ClientHttpResponse executeInternal(final HttpHeaders headers)
throws IOException {
final String prev = MDC.get(Processor.MDC_JOB_ID_KEY);
final String prevApplication = MDC.get(Processor.MDC_APPLICATION_ID_KEY);
boolean mdcChanged = prev == null || jobId.equals(prev);
if (mdcChanged) {
MDC.put(Processor.MDC_JOB_ID_KEY, ConfigFileResolvingHttpRequestFactory.this.jobId);
MDC.put(Processor.MDC_APPLICATION_ID_KEY, ConfigFileResolvingHttpRequestFactory.this.applicationId);
}
try {
if (this.request != null) {
Expand Down Expand Up @@ -150,8 +154,10 @@ protected synchronized ClientHttpResponse executeInternal(final HttpHeaders head
if (mdcChanged) {
if (prev != null) {
MDC.put(Processor.MDC_JOB_ID_KEY, prev);
MDC.put(Processor.MDC_APPLICATION_ID_KEY, prevApplication);
} else {
MDC.remove(Processor.MDC_JOB_ID_KEY);
MDC.remove(Processor.MDC_APPLICATION_ID_KEY);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ protected abstract void doExport(OutputStream outputStream, Print print)

@Override
public final Processor.ExecutionContext print(
final String jobId, final PJsonObject requestData, final Configuration config,
final String applicationId, final String jobId, final PJsonObject requestData, final Configuration config,
final File configDir, final File taskDirectory, final OutputStream outputStream)
throws Exception {
final Print print = getJasperPrint(jobId, requestData, config, configDir, taskDirectory);
final Print print = getJasperPrint(applicationId, jobId, requestData, config, configDir, taskDirectory);

if (Thread.currentThread().isInterrupted()) {
throw new CancellationException();
Expand Down Expand Up @@ -125,7 +125,7 @@ private JasperFillManager getJasperFillManager(
*/
@VisibleForTesting
public final Print getJasperPrint(
final String jobId, final PJsonObject requestData,
final String applicationId, final String jobId, final PJsonObject requestData,
final Configuration config, final File configDir, final File taskDirectory)
throws JRException, SQLException, ExecutionException {
final String templateName = requestData.getString(Constants.JSON_LAYOUT_KEY);
Expand All @@ -136,7 +136,7 @@ public final Print getJasperPrint(
config, jasperTemplateFile, JasperReportBuilder.JASPER_REPORT_COMPILED_FILE_EXT,
LOGGER);

final Values values = new Values(jobId, requestData, template, taskDirectory,
final Values values = new Values(applicationId, jobId, requestData, template, taskDirectory,
this.httpRequestFactory, jasperTemplateBuild.getParentFile());

double maxDpi = maxDpi(values);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,15 @@ private String getMapSubReportVariable(final Template template) {

@Override
public final Processor.ExecutionContext print(
final String jobId, final PJsonObject spec,
final String applicationId, final String jobId, final PJsonObject spec,
final Configuration config, final File configDir,
final File taskDirectory,
final OutputStream outputStream) throws Exception {
final String templateName = spec.getString(Constants.JSON_LAYOUT_KEY);

final Template template = config.getTemplate(templateName);

final Values values = new Values(jobId, spec, template, taskDirectory, this.httpRequestFactory, null,
final Values values = new Values(applicationId, jobId, spec, template, taskDirectory, this.httpRequestFactory, null,
this.fileSuffix);

final ProcessorDependencyGraph.ProcessorGraphForkJoinTask task =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public interface OutputFormat {
/**
* Performs the print and writes to the report in the correct format to the outputStream.
*
* @param applicationId the application ID
* @param jobId the job ID
* @param spec the data from the client, required for writing.
* @param config the configuration object representing the server side configuration.
Expand All @@ -35,7 +36,7 @@ public interface OutputFormat {
* @param outputStream the stream to write the result to
*/
Processor.ExecutionContext print(
String jobId, PJsonObject spec, Configuration config, File configDir, File taskDirectory,
String applicationId, String jobId, PJsonObject spec, Configuration config, File configDir, File taskDirectory,
OutputStream outputStream) throws Exception;

}
14 changes: 12 additions & 2 deletions core/src/main/java/org/mapfish/print/output/Values.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public final class Values {
* The key for the reference ID.
*/
public static final String JOB_ID_KEY = "jobId";
/**
* The key for the reference ID.
*/
public static final String APPLICATION_ID_KEY = "applicationId";
/**
* The key for the values object of it self.
*/
Expand Down Expand Up @@ -94,6 +98,7 @@ public Values() {
/**
* Construct from the json request body and the associated template.
*
* @param applicationId the application ID
* @param jobId the job ID
* @param requestData the json request data
* @param template the template
Expand All @@ -102,18 +107,20 @@ public Values() {
* @param jasperTemplateBuild the directory where the jasper templates are compiled to
*/
public Values(
final String applicationId,
final String jobId,
final PJsonObject requestData,
final Template template,
final File taskDirectory,
final MfClientHttpRequestFactoryImpl httpRequestFactory,
final File jasperTemplateBuild) {
this(jobId, requestData, template, taskDirectory, httpRequestFactory, jasperTemplateBuild, null);
this(applicationId, jobId, requestData, template, taskDirectory, httpRequestFactory, jasperTemplateBuild, null);
}

/**
* Construct from the json request body and the associated template.
*
* @param applicationId the application ID
* @param jobId the job ID
* @param requestData the json request data
* @param template the template
Expand All @@ -124,6 +131,7 @@ public Values(
*/
//CHECKSTYLE:OFF
public Values(
final String applicationId,
final String jobId,
final PJsonObject requestData,
final Template template,
Expand All @@ -138,7 +146,7 @@ public Values(
this.values.put(TASK_DIRECTORY_KEY, taskDirectory);
this.values.put(CLIENT_HTTP_REQUEST_FACTORY_KEY,
new MfClientHttpRequestFactoryProvider(new ConfigFileResolvingHttpRequestFactory(
httpRequestFactory, template.getConfiguration(), jobId)));
httpRequestFactory, template.getConfiguration(), applicationId, jobId)));
this.values.put(TEMPLATE_KEY, template);
this.values.put(PDF_CONFIG_KEY, template.getPdfConfig());
if (jasperTemplateBuild != null) {
Expand All @@ -154,6 +162,7 @@ public Values(
populateFromAttributes(template, attributes, jsonAttributes);

this.values.put(JOB_ID_KEY, jobId);
this.values.put(APPLICATION_ID_KEY, applicationId == null ? "" : applicationId);

this.values.put(VALUES_KEY, this);

Expand Down Expand Up @@ -268,6 +277,7 @@ public void addRequiredValues(@Nonnull final Values sourceValues) {
this.values.put(SUBREPORT_DIR_KEY, subReportDir);
this.values.put(VALUES_KEY, this);
this.values.put(JOB_ID_KEY, sourceValues.getString(JOB_ID_KEY));
this.values.put(APPLICATION_ID_KEY, sourceValues.getString(APPLICATION_ID_KEY));
this.values.put(LOCALE_KEY, sourceValues.getObject(LOCALE_KEY, Locale.class));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,15 @@ public String toString() {
*/
public static final class Context implements ExecutionContext {
private final String jobId;
private final String applicationId;
private volatile boolean canceled = false;
private ExecutionStats stats = new ExecutionStats();

/**
* @param jobId The job ID.
*/
public Context(final String jobId) {
public Context(final String applicationId, final String jobId) {
this.applicationId = applicationId;
this.jobId = jobId;
}

Expand Down Expand Up @@ -240,18 +242,22 @@ public String getJobId() {
public <T> T mdcContext(final Supplier<T> action) {
this.stopIfCanceled();
final String prev = MDC.get(MDC_JOB_ID_KEY);
final String prevApplication = MDC.get(MDC_APPLICATION_ID_KEY);
boolean changed = prev == null || (jobId != null && jobId.equals(prev));
if (changed) {
MDC.put(MDC_JOB_ID_KEY, this.jobId);
MDC.put(MDC_APPLICATION_ID_KEY, this.applicationId);
}
try {
return action.get();
} finally {
if (changed) {
if (prev != null) {
MDC.put(MDC_JOB_ID_KEY, prev);
MDC.put(MDC_APPLICATION_ID_KEY, prevApplication);
} else {
MDC.remove(MDC_JOB_ID_KEY);
MDC.remove(MDC_APPLICATION_ID_KEY);
}
}
}
Expand All @@ -261,18 +267,22 @@ public <T> T mdcContext(final Supplier<T> action) {
public <T> T mdcContextEx(final Callable<T> action) throws Exception {
this.stopIfCanceled();
final String prev = MDC.get(MDC_JOB_ID_KEY);
final String prevApplication = MDC.get(MDC_APPLICATION_ID_KEY);
boolean mdcChanged = prev == null || jobId.equals(prev);
if (mdcChanged) {
MDC.put(MDC_JOB_ID_KEY, this.jobId);
MDC.put(MDC_APPLICATION_ID_KEY, this.applicationId);
}
try {
return action.call();
} finally {
if (mdcChanged) {
if (prev != null) {
MDC.put(MDC_JOB_ID_KEY, prev);
MDC.put(MDC_APPLICATION_ID_KEY, prevApplication);
} else {
MDC.remove(MDC_JOB_ID_KEY);
MDC.remove(MDC_APPLICATION_ID_KEY);
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/java/org/mapfish/print/processor/Processor.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public interface Processor<IN, OUT> extends ConfigurationObject {
*/
String MDC_JOB_ID_KEY = "job_id";

/**
* MDC key for the application ID.
*/
String MDC_APPLICATION_ID_KEY = "application_id";

/**
* Get the class of the output type. This is used when determining the outputs this processor produces.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ public ProcessorDependencyGraph build(
outputTypes.put(Values.PDF_CONFIG_KEY, PDFConfig.class);
outputTypes.put(Values.SUBREPORT_DIR_KEY, String.class);
outputTypes.put(Values.OUTPUT_FORMAT_KEY, String.class);
outputTypes.put(Values.APPLICATION_ID_KEY, String.class);
outputTypes.put(Values.JOB_ID_KEY, String.class);
outputTypes.put(MapPrinterServlet.JSON_REQUEST_HEADERS, HttpRequestHeadersAttribute.Value.class);
outputTypes.put(Values.LOCALE_KEY, Locale.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public final class ProcessorExecutionContext {
*/
public ProcessorExecutionContext(final Values values) {
this.values = values;
this.context = new Context(values.getString(Values.JOB_ID_KEY));
this.context = new Context(values.getString(Values.APPLICATION_ID_KEY), values.getString(Values.JOB_ID_KEY));
}

public Values getValues() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public static String getInputValueName(
final String[] defaultValues = {
Values.TASK_DIRECTORY_KEY, Values.CLIENT_HTTP_REQUEST_FACTORY_KEY,
Values.TEMPLATE_KEY, Values.PDF_CONFIG_KEY, Values.SUBREPORT_DIR_KEY,
Values.OUTPUT_FORMAT_KEY, Values.JOB_ID_KEY
Values.OUTPUT_FORMAT_KEY, Values.APPLICATION_ID_KEY, Values.JOB_ID_KEY
};
if (inputPrefix == null || Arrays.asList(defaultValues).contains(field)) {
name = field;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ public Collection<String> getDependencies() {
result.add(Values.PDF_CONFIG_KEY);
result.add(Values.SUBREPORT_DIR_KEY);
result.add(Values.VALUES_KEY);
result.add(Values.APPLICATION_ID_KEY);
result.add(Values.JOB_ID_KEY);
return result;
}
Expand Down
Loading

0 comments on commit c836fdd

Please sign in to comment.