I have soap service with method:
@Override
public SomeResponse someRequest(SomeRequest request) {
org.w3c.dom.Element any = request.getMessagePrimaryContent().getAny();
}
any - may contain any "internal request". I know all types of any. For example: ImportBooksRequest, ImportCDRequest, ImportSongsRequest
And I can unmarshale any to concrete class:
if (any.getLocalName().equals(ImportBooksRequest.class.getSimpleName())) {
ImportBooksRequest importBooksRequest = marshaler.unMarshal(any, ImportBooksRequest.class);
} else if(any.getLocalName().equals(ImportCDRequest.class.getSimpleName())){
ImportCDRequest importCDRequest = marshaler.unMarshal(any, ImportBooksRequest.class);
} else if(any.getLocalName().equals(ImportSongsRequest.class.getSimpleName())){
ImportSongsRequest importSongsRequest= marshaler.unMarshal(any, ImportSongsRequest.class);
}
And after that I need redirect request to concrete request. Now I Use Handlers:
someRequestHandlersResolver.getSomeRequestHandler(any.getLocalName()).process(request);
In someRequestHandlersResolver
I have hadlers for each concreate request
@Component
public class SomeRequestHandlersResolverImpl implements SomeRequestHandlersResolver {
private Map<String, SomeRequestHandler> map = new HashMap<>();
public SomeRequestHandlersResolverImpl(ImportBooksRequest importBooksRequest, ImportCDRequest importCDRequest, ImportSongsRequest importSongsRequest) {
map.put(ImportBooksRequest.class.getSimpleName(), importBooksRequest);
map.put(ImportCDRequest.class.getSimpleName(), importCDRequest);
map.put(ImportSongsRequest.class.getSimpleName(), importSongsRequest);
}
@Override
public SomeRequestHandler getSomeRequestHandler(String type) {
SomeRequestHandler someRequestHandler = map.get(type);
if (someRequestHandler == null)
throw new IllegalArgumentException("Invalid SomeRequest type: " + type);
return someRequestHandler;
}
}
I'm not completely sure that this is correct. Maybe there are other patterns or ways to implement this?
Aucun commentaire:
Enregistrer un commentaire