Skip to content

Commit

Permalink
Merge pull request #131 from czcorpus/v2_fixes
Browse files Browse the repository at this point in the history
Fix misc. problems reported by Clarin validator
  • Loading branch information
tomachalek authored May 31, 2024
2 parents 0779bdf + 71d4d80 commit 0b9a304
Show file tree
Hide file tree
Showing 12 changed files with 221 additions and 80 deletions.
33 changes: 28 additions & 5 deletions handler/v12/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ func (a *FCSSubHandlerV12) produceXMLResponse(ctx *gin.Context, code int, xslt s
ctx.Writer.Header().Set("Content-Type", "application/xml")
}

func (a *FCSSubHandlerV12) produceErrorResponse(ctx *gin.Context, code int, xslt string, fcsErrors []general.FCSError) {
func (a *FCSSubHandlerV12) produceExplainErrorResponse(
ctx *gin.Context, code int, xslt string, fcsErrors []general.FCSError) {
ans := schema.XMLExplainResponse{
XMLNSSRU: "http://www.loc.gov/zing/srw/",
Version: "1.2",
Expand All @@ -69,18 +70,32 @@ func (a *FCSSubHandlerV12) produceErrorResponse(ctx *gin.Context, code int, xslt
a.produceXMLResponse(ctx, code, xslt, ans)
}

func (a *FCSSubHandlerV12) produceSRErrorResponse(
ctx *gin.Context, code int, xslt string, fcsErrors []general.FCSError) {
ans := schema.XMLSRResponse{
XMLNSSRUResponse: "http://www.loc.gov/zing/srw/",
Version: "1.2",
Diagnostics: schema.NewXMLDiagnostics(),
}
for _, fcsErr := range fcsErrors {
ans.Diagnostics.AddDiagnostic(fcsErr.Code, fcsErr.Type, fcsErr.Ident, fcsErr.Message)
}
a.produceXMLResponse(ctx, code, xslt, ans)
}

func (a *FCSSubHandlerV12) Handle(
ctx *gin.Context,
fcsGeneralRequest general.FCSGeneralRequest,
xslt map[string]string,
) {
fcsResponse := &FCSRequest{
General: fcsGeneralRequest,
General: &fcsGeneralRequest,
RecordPacking: RecordPackingXML,
Operation: OperationExplain,
}
if fcsResponse.General.HasFatalError() {
a.produceErrorResponse(ctx, general.ConformantStatusBadRequest, fcsGeneralRequest.XSLT, fcsGeneralRequest.Errors)
a.produceExplainErrorResponse(
ctx, general.ConformantStatusBadRequest, fcsGeneralRequest.XSLT, fcsGeneralRequest.Errors)
return
}

Expand All @@ -100,7 +115,8 @@ func (a *FCSSubHandlerV12) Handle(
Ident: "operation",
Message: fmt.Sprintf("Unsupported operation: %s", operation),
})
a.produceErrorResponse(ctx, general.ConformantStatusBadRequest, fcsGeneralRequest.XSLT, fcsGeneralRequest.Errors)
a.produceExplainErrorResponse(
ctx, general.ConformantStatusBadRequest, fcsGeneralRequest.XSLT, fcsGeneralRequest.Errors)
return
}
fcsResponse.Operation = operation
Expand All @@ -114,7 +130,14 @@ func (a *FCSSubHandlerV12) Handle(
Ident: "recordPacking",
Message: err.Error(),
})
a.produceErrorResponse(ctx, general.ConformantStatusBadRequest, fcsGeneralRequest.XSLT, fcsGeneralRequest.Errors)
if operation == OperationSearchRetrive {
a.produceSRErrorResponse(
ctx, general.ConformantStatusBadRequest, fcsGeneralRequest.XSLT, fcsGeneralRequest.Errors)

} else {
a.produceExplainErrorResponse(
ctx, general.ConformantStatusBadRequest, fcsGeneralRequest.XSLT, fcsGeneralRequest.Errors)
}
return
}
fcsResponse.RecordPacking = recordPacking
Expand Down
2 changes: 1 addition & 1 deletion handler/v12/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
)

type FCSRequest struct {
General general.FCSGeneralRequest
General *general.FCSGeneralRequest
RecordPacking RecordPacking
Operation Operation
}
12 changes: 8 additions & 4 deletions handler/v12/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,30 @@ func (a *FCSSubHandlerV12) scan(ctx *gin.Context, fcsResponse *FCSRequest) (sche
_, err := strconv.Atoi(xMaxTerms)
if err != nil {
ans.Diagnostics = schema.NewXMLDiagnostics()
ans.Diagnostics.AddDiagnostic(general.DCUnsupportedParameter, 0, ScanArgMaximumTerms.String(), general.DCUnsupportedParameterValue.AsMessage())
ans.Diagnostics.AddDfltMsgDiagnostic(
general.DCUnsupportedParameterValue, 0, ScanArgMaximumTerms.String())
return ans, general.ConformantUnprocessableEntity
}

xResponsePos := ctx.DefaultQuery(ScanArgResponsePosition.String(), "1")
_, err = strconv.Atoi(xResponsePos)
if err != nil {
ans.Diagnostics = schema.NewXMLDiagnostics()
ans.Diagnostics.AddDiagnostic(general.DCUnsupportedParameterValue, 0, ScanArgResponsePosition.String(), general.DCUnsupportedParameterValue.AsMessage())
ans.Diagnostics.AddDfltMsgDiagnostic(
general.DCUnsupportedParameterValue, 0, ScanArgResponsePosition.String())
return ans, general.ConformantUnprocessableEntity
}

scanClause := ctx.Query(ScanArgScanClause.String())
if scanClause == "" {
ans.Diagnostics = schema.NewXMLDiagnostics()
ans.Diagnostics.AddDiagnostic(general.DCMandatoryParameterNotSupplied, 0, ScanArgScanClause.String(), general.DCMandatoryParameterNotSupplied.AsMessage())
ans.Diagnostics.AddDfltMsgDiagnostic(
general.DCMandatoryParameterNotSupplied, 0, ScanArgScanClause.String())
return ans, general.ConformantUnprocessableEntity
}

ans.Diagnostics = schema.NewXMLDiagnostics()
ans.Diagnostics.AddDiagnostic(general.DCUnsupportedIndex, 0, ScanArgScanClause.String(), general.DCUnsupportedIndex.AsMessage())
ans.Diagnostics.AddDfltMsgDiagnostic(
general.DCUnsupportedIndex, 0, ScanArgScanClause.String())
return ans, general.ConformantUnprocessableEntity
}
18 changes: 17 additions & 1 deletion handler/v12/schema/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,23 @@ type XMLDiagnostics struct {
Diagnostics []XMLDiagnostic `xml:"diag:diagnostic"`
}

func (d *XMLDiagnostics) AddDiagnostic(code general.DiagnosticCode, typ general.DiagnosticType, ident string, message string) {
// AddDfltMsgDiagnostics adds a diagnostics code along with
// its attached default message. For custom message,
// use AddDiagnostic.
func (d *XMLDiagnostics) AddDfltMsgDiagnostic(
code general.DiagnosticCode,
typ general.DiagnosticType,
ident string,
) {
d.AddDiagnostic(code, typ, ident, code.AsMessage())
}

func (d *XMLDiagnostics) AddDiagnostic(
code general.DiagnosticCode,
typ general.DiagnosticType,
ident string,
message string,
) {
uri := []string{}
if code > 0 {
uri = append(uri, fmt.Sprintf("info:srw/diagnostic/1/%d", code))
Expand Down
12 changes: 8 additions & 4 deletions handler/v12/schema/searchRetrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ type XMLSRResponse struct {
XMLNSSRUResponse string `xml:"xmlns:sru,attr"`
Version string `xml:"sru:version"`

NumberOfRecords int `xml:"sru:numberOfRecords"`
Records *[]XMLSRRecord `xml:"sru:records>sru:record,omitempty"`
EchoedRequest XMLSREchoedRequest `xml:"sru:echoedSearchRetrieveRequest"`
Diagnostics *XMLDiagnostics `xml:"sru:diagnostics,omitempty"`
NumberOfRecords int `xml:"sru:numberOfRecords"`

// Records
// note: we need a pointer here to allow the marshaler skip the 'records' parent
// in case there are no 'record' children
Records *[]XMLSRRecord `xml:"sru:records>sru:record,omitempty"`
EchoedRequest XMLSREchoedRequest `xml:"sru:echoedSearchRetrieveRequest"`
Diagnostics *XMLDiagnostics `xml:"sru:diagnostics,omitempty"`
}

func NewXMLSRResponse() XMLSRResponse {
Expand Down
60 changes: 40 additions & 20 deletions handler/v12/searchrt.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ func (a *FCSSubHandlerV12) searchRetrieve(ctx *gin.Context, fcsResponse *FCSRequ
fcsQuery := ctx.Query(SearchRetrArgQuery.String())
if len(fcsQuery) == 0 {
ans.Diagnostics = schema.NewXMLDiagnostics()
ans.Diagnostics.AddDiagnostic(general.DCMandatoryParameterNotSupplied, 0, "fcs_query", general.DCMandatoryParameterNotSupplied.AsMessage())
ans.Diagnostics.AddDfltMsgDiagnostic(
general.DCMandatoryParameterNotSupplied, 0, "fcs_query")
return ans, general.ConformantStatusBadRequest
}
ans.EchoedRequest.Query = fcsQuery
Expand All @@ -101,12 +102,14 @@ func (a *FCSSubHandlerV12) searchRetrieve(ctx *gin.Context, fcsResponse *FCSRequ
startRecord, err := strconv.Atoi(xStartRecord)
if err != nil {
ans.Diagnostics = schema.NewXMLDiagnostics()
ans.Diagnostics.AddDiagnostic(general.DCUnsupportedParameterValue, 0, SearchRetrStartRecord.String(), general.DCUnsupportedParameterValue.AsMessage())
ans.Diagnostics.AddDfltMsgDiagnostic(
general.DCUnsupportedParameterValue, 0, SearchRetrStartRecord.String())
return ans, general.ConformantUnprocessableEntity
}
if startRecord < 1 {
ans.Diagnostics = schema.NewXMLDiagnostics()
ans.Diagnostics.AddDiagnostic(general.DCUnsupportedParameterValue, 0, SearchRetrStartRecord.String(), general.DCUnsupportedParameterValue.AsMessage())
ans.Diagnostics.AddDfltMsgDiagnostic(
general.DCUnsupportedParameterValue, 0, SearchRetrStartRecord.String())
return ans, general.ConformantUnprocessableEntity
}
ans.EchoedRequest.StartRecord = startRecord
Expand All @@ -116,7 +119,8 @@ func (a *FCSSubHandlerV12) searchRetrieve(ctx *gin.Context, fcsResponse *FCSRequ
recordSchema := ctx.DefaultQuery(SearchRetrArgRecordSchema.String(), general.RecordSchema)
if recordSchema != general.RecordSchema {
ans.Diagnostics = schema.NewXMLDiagnostics()
ans.Diagnostics.AddDiagnostic(general.DCUnknownSchemaForRetrieval, 0, SearchMaximumRecords.String(), general.DCUnknownSchemaForRetrieval.AsMessage())
ans.Diagnostics.AddDfltMsgDiagnostic(
general.DCUnknownSchemaForRetrieval, 0, SearchMaximumRecords.String())
return ans, general.ConformantUnprocessableEntity
}

Expand All @@ -126,13 +130,15 @@ func (a *FCSSubHandlerV12) searchRetrieve(ctx *gin.Context, fcsResponse *FCSRequ
maximumRecords, err = strconv.Atoi(xMaximumRecords)
if err != nil {
ans.Diagnostics = schema.NewXMLDiagnostics()
ans.Diagnostics.AddDiagnostic(general.DCUnsupportedParameterValue, 0, SearchMaximumRecords.String(), general.DCUnsupportedParameterValue.AsMessage())
ans.Diagnostics.AddDfltMsgDiagnostic(
general.DCUnsupportedParameterValue, 0, SearchMaximumRecords.String())
return ans, general.ConformantUnprocessableEntity
}
}
if maximumRecords < 1 {
ans.Diagnostics = schema.NewXMLDiagnostics()
ans.Diagnostics.AddDiagnostic(general.DCUnsupportedParameterValue, 0, SearchMaximumRecords.String(), general.DCUnsupportedParameterValue.AsMessage())
ans.Diagnostics.AddDfltMsgDiagnostic(
general.DCUnsupportedParameterValue, 0, SearchMaximumRecords.String())
return ans, general.ConformantUnprocessableEntity

}
Expand All @@ -141,7 +147,8 @@ func (a *FCSSubHandlerV12) searchRetrieve(ctx *gin.Context, fcsResponse *FCSRequ
// as the actual result can be very small. But we still
// have to limit max. number of records...
ans.Diagnostics = schema.NewXMLDiagnostics()
ans.Diagnostics.AddDiagnostic(general.DCTooManyMatchingRecords, 0, fmt.Sprintf("%d", mango.MaxRecordsInternalLimit), general.DCTooManyMatchingRecords.AsMessage())
ans.Diagnostics.AddDfltMsgDiagnostic(
general.DCTooManyMatchingRecords, 0, fmt.Sprintf("%d", mango.MaxRecordsInternalLimit))
return ans, general.ConformantUnprocessableEntity
}
logArgs[SearchMaximumRecords.String()] = maximumRecords
Expand All @@ -153,7 +160,7 @@ func (a *FCSSubHandlerV12) searchRetrieve(ctx *gin.Context, fcsResponse *FCSRequ
for _, pid := range corporaPids {
res, err := a.corporaConf.Resources.GetResourceByPID(pid)
if err == corpus.ErrResourceNotFound {
ans.Records = &[]schema.XMLSRRecord{}
ans.Records = nil
return ans, http.StatusOK
}
corpora = append(corpora, res.ID)
Expand All @@ -166,13 +173,15 @@ func (a *FCSSubHandlerV12) searchRetrieve(ctx *gin.Context, fcsResponse *FCSRequ
// get searchable corpora and attrs
if len(corpora) == 0 {
ans.Diagnostics = schema.NewXMLDiagnostics()
ans.Diagnostics.AddDiagnostic(general.DCUnsupportedContextSet, 0, SearchRetrArgFCSContext.String(), general.DCUnsupportedContextSet.AsMessage())
ans.Diagnostics.AddDfltMsgDiagnostic(
general.DCUnsupportedContextSet, 0, SearchRetrArgFCSContext.String())
return ans, general.ConformantStatusBadRequest
}
retrieveAttrs, err := a.corporaConf.Resources.GetCommonPosAttrNames(corpora...)
if err != nil {
ans.Diagnostics = schema.NewXMLDiagnostics()
ans.Diagnostics.AddDiagnostic(general.DCGeneralSystemError, 0, err.Error(), general.DCGeneralSystemError.AsMessage())
ans.Diagnostics.AddDfltMsgDiagnostic(
general.DCGeneralSystemError, 0, err.Error())
return ans, http.StatusInternalServerError
}

Expand All @@ -198,13 +207,15 @@ func (a *FCSSubHandlerV12) searchRetrieve(ctx *gin.Context, fcsResponse *FCSRequ
query := ast.Generate()
if len(ast.Errors()) > 0 {
ans.Diagnostics = schema.NewXMLDiagnostics()
ans.Diagnostics.AddDiagnostic(general.DCQueryCannotProcess, 0, SearchRetrArgQuery.String(), ast.Errors()[0].Error())
ans.Diagnostics.AddDiagnostic(
general.DCQueryCannotProcess, 0, SearchRetrArgQuery.String(), ast.Errors()[0].Error())
return ans, general.ConformantUnprocessableEntity
}
rscConf, err := a.corporaConf.Resources.GetResource(rng.Rsc)
if err != nil {
ans.Diagnostics = schema.NewXMLDiagnostics()
ans.Diagnostics.AddDiagnostic(general.DCGeneralSystemError, 0, err.Error(), general.DCGeneralSystemError.AsMessage())
ans.Diagnostics.AddDfltMsgDiagnostic(
general.DCGeneralSystemError, 0, err.Error())
return ans, general.ConformandGeneralServerError
}
args, err := sonic.Marshal(rdb.ConcExampleArgs{
Expand All @@ -218,7 +229,8 @@ func (a *FCSSubHandlerV12) searchRetrieve(ctx *gin.Context, fcsResponse *FCSRequ
})
if err != nil {
ans.Diagnostics = schema.NewXMLDiagnostics()
ans.Diagnostics.AddDiagnostic(general.DCGeneralSystemError, 0, err.Error(), general.DCGeneralSystemError.AsMessage())
ans.Diagnostics.AddDfltMsgDiagnostic(
general.DCGeneralSystemError, 0, err.Error())
return ans, http.StatusInternalServerError
}
wait, err := a.radapter.PublishQuery(rdb.Query{
Expand All @@ -227,7 +239,8 @@ func (a *FCSSubHandlerV12) searchRetrieve(ctx *gin.Context, fcsResponse *FCSRequ
})
if err != nil {
ans.Diagnostics = schema.NewXMLDiagnostics()
ans.Diagnostics.AddDiagnostic(general.DCGeneralSystemError, 0, err.Error(), general.DCGeneralSystemError.AsMessage())
ans.Diagnostics.AddDfltMsgDiagnostic(
general.DCGeneralSystemError, 0, err.Error())
return ans, http.StatusInternalServerError
}
waits[i] = wait
Expand All @@ -241,7 +254,8 @@ func (a *FCSSubHandlerV12) searchRetrieve(ctx *gin.Context, fcsResponse *FCSRequ
result, err := rdb.DeserializeConcExampleResult(rawResult)
if err != nil {
ans.Diagnostics = schema.NewXMLDiagnostics()
ans.Diagnostics.AddDiagnostic(general.DCGeneralSystemError, 0, err.Error(), general.DCGeneralSystemError.AsMessage())
ans.Diagnostics.AddDfltMsgDiagnostic(
general.DCGeneralSystemError, 0, err.Error())
return ans, http.StatusInternalServerError
}
if err := result.Err(); err != nil {
Expand All @@ -250,7 +264,8 @@ func (a *FCSSubHandlerV12) searchRetrieve(ctx *gin.Context, fcsResponse *FCSRequ

} else {
ans.Diagnostics = schema.NewXMLDiagnostics()
ans.Diagnostics.AddDiagnostic(general.DCQueryCannotProcess, 0, err.Error(), general.DCQueryCannotProcess.AsMessage())
ans.Diagnostics.AddDfltMsgDiagnostic(
general.DCQueryCannotProcess, 0, err.Error())
return ans, http.StatusInternalServerError
}
}
Expand All @@ -262,12 +277,14 @@ func (a *FCSSubHandlerV12) searchRetrieve(ctx *gin.Context, fcsResponse *FCSRequ
ans.NumberOfRecords = totalConcSize
if fromResource.AllHasOutOfRangeError() {
ans.Diagnostics = schema.NewXMLDiagnostics()
ans.Diagnostics.AddDiagnostic(general.DCFirstRecordPosOutOfRange, 0, fromResource.GetFirstError().Error(), general.DCFirstRecordPosOutOfRange.AsMessage())
ans.Diagnostics.AddDfltMsgDiagnostic(
general.DCFirstRecordPosOutOfRange, 0, fromResource.GetFirstError().Error())
return ans, general.ConformantUnprocessableEntity

} else if fromResource.HasFatalError() {
ans.Diagnostics = schema.NewXMLDiagnostics()
ans.Diagnostics.AddDiagnostic(general.DCQueryCannotProcess, 0, fromResource.GetFirstError().Error(), general.DCQueryCannotProcess.AsMessage())
ans.Diagnostics.AddDfltMsgDiagnostic(
general.DCQueryCannotProcess, 0, fromResource.GetFirstError().Error())
return ans, general.ConformandGeneralServerError
}

Expand All @@ -277,7 +294,8 @@ func (a *FCSSubHandlerV12) searchRetrieve(ctx *gin.Context, fcsResponse *FCSRequ
res, err := a.corporaConf.Resources.GetResource(fromResource.CurrRscName())
if err != nil {
ans.Diagnostics = schema.NewXMLDiagnostics()
ans.Diagnostics.AddDiagnostic(general.DCGeneralSystemError, 0, err.Error(), general.DCGeneralSystemError.AsMessage())
ans.Diagnostics.AddDfltMsgDiagnostic(
general.DCGeneralSystemError, 0, err.Error())
return ans, http.StatusInternalServerError
}
item := fromResource.CurrLine()
Expand Down Expand Up @@ -321,6 +339,8 @@ func (a *FCSSubHandlerV12) searchRetrieve(ctx *gin.Context, fcsResponse *FCSRequ
RecordPosition: len(records) + startRecord,
})
}
ans.Records = &records
if len(records) > 0 {
ans.Records = &records
}
return ans, http.StatusOK
}
Loading

0 comments on commit 0b9a304

Please sign in to comment.