String tags = attributes.getValue(TAGS_ATTRIBUTE);
String query = attributes.getValue(QUERY_ATTRIBUTE);
if (photosetId != null) {
if (userId != null || tags != null || query != null) {
Locator locator = pipeline.getPipelineContext().getCurrentLocator();
XMLPipelineException e =
new XMLPipelineException(
"not allowed to use any of the attributes: 'user-id', 'tags', 'query' " +
"when photoset-id is specified",
locator);
getTargetProcess(pipeline).fatalError(e);
return null;
}
queryString.append("?").append(FLICKR_API_PHOTOSETS_METHOD);
queryString.append("&")
.append(MessageFormat.format(
FLICKR_PHOTOSET_ID_PARAM, photosetId));
} else {
queryString.append("?").append(FLICKR_API_PHOTOS_METHOD);
if (userId != null) {
queryString.append("&")
.append(MessageFormat.format(
FLICKR_USER_ID_PARAM, userId));
} else if (tags == null && query == null) {
// parameterless queries are not supported
Locator locator = pipeline.getPipelineContext().getCurrentLocator();
XMLPipelineException e =
new XMLPipelineException(
"at least one of the attributes: 'user-id', 'tags', 'query' should be specified",
locator);
getTargetProcess(pipeline).fatalError(e);
return null;
}
if (tags != null && query != null) {
// in order not to produce misleading results (as flickr ignores tags param
// when query/text param is set) it is considered invalid attributes
// combination
Locator locator = pipeline.getPipelineContext().getCurrentLocator();
XMLPipelineException e =
new XMLPipelineException(
"not allowed to specify both 'tags' and 'query' attributes",
locator);
getTargetProcess(pipeline).fatalError(e);
return null;
}
if (tags != null) {
// convert space separated list to comma separated list
String commaSeparatedTags = tags.trim().replaceAll("\\s+", ",");
queryString.append("&")
.append(MessageFormat.format(
FLICKR_TAGS_PARAM, commaSeparatedTags))
.append("&")
.append(FLICKR_TAG_MODE_PARAM);
}
if (query != null) {
queryString.append("&")
.append(MessageFormat.format(
FLICKR_TEXT_PARAM, query));
}
}
if (attributes.getValue(API_KEY_ATTRIBUTE) != null) {
queryString.append("&")
.append(MessageFormat.format(
FLICKR_API_KEY_PARAM,
attributes.getValue(API_KEY_ATTRIBUTE)));
} else {
Locator locator = pipeline.getPipelineContext().getCurrentLocator();
XMLPipelineException e =
new XMLPipelineException(
"api-key attribute should be specified",
locator);
getTargetProcess(pipeline).fatalError(e);
return null;
}
queryString.append("&").append(FLICKR_EXTRAS_PARAM);
// pagination parameters
String pageSize = attributes.getValue(PAGE_SIZE_ATTRIBUTE);
String pageIndex = attributes.getValue(PAGE_INDEX_ATTRIBUTE);
if (pageSize != null) {
Integer pageSizeInt = null;
try {
pageSizeInt = Integer.parseInt(pageSize);
} catch(NumberFormatException nfe) {
Locator locator = pipeline.getPipelineContext().getCurrentLocator();
XMLPipelineException e =
new XMLPipelineException(
"page-size attribute should be of integer value",
locator);
getTargetProcess(pipeline).fatalError(e);
return null;
}
if (pageSizeInt < 1 || pageSizeInt > 500) {
Locator locator = pipeline.getPipelineContext().getCurrentLocator();
XMLPipelineException e =
new XMLPipelineException(
"page-size attribute value should be in the range of [1,500]",
locator);
getTargetProcess(pipeline).fatalError(e);
return null;
}
pageSize = pageSizeInt.toString();
} else {
pageSize = DEFAULT_PAGE_SIZE;
}
queryString.append("&")
.append(MessageFormat.format(
FLICKR_PER_PAGE_PARAM, pageSize));
if (pageIndex != null) {
Integer pageIndexInt;
try {
pageIndexInt = Integer.parseInt(pageIndex);
} catch(NumberFormatException nfe) {
Locator locator = pipeline.getPipelineContext().getCurrentLocator();
XMLPipelineException e =
new XMLPipelineException(
"page-index attribute should be of integer value",
locator);
getTargetProcess(pipeline).fatalError(e);
return null;
}
if (pageIndexInt < 1) {
Locator locator = pipeline.getPipelineContext().getCurrentLocator();
XMLPipelineException e =
new XMLPipelineException(
"page-index attribute value should be in the range of [1,...]",
locator);
getTargetProcess(pipeline).fatalError(e);