PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/bundle/src/main/java/com/adobe/acs/commons/quickly/operations/impl/DuckDuckGoDocsOperationImpl.java

https://github.com/bstopp/acs-aem-commons
Java | 146 lines | 99 code | 22 blank | 25 comment | 5 complexity | eba8bff4d18abf368872af423d8d3cb7 MD5 | raw file
Possible License(s): Apache-2.0
  1. /*
  2. * #%L
  3. * ACS AEM Commons Bundle
  4. * %%
  5. * Copyright (C) 2013 Adobe
  6. * %%
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * #L%
  19. */
  20. package com.adobe.acs.commons.quickly.operations.impl;
  21. import com.adobe.acs.commons.quickly.Command;
  22. import com.adobe.acs.commons.quickly.operations.AbstractOperation;
  23. import com.adobe.acs.commons.quickly.operations.Operation;
  24. import com.adobe.acs.commons.quickly.results.Result;
  25. import com.adobe.acs.commons.quickly.results.Action;
  26. import com.adobe.granite.license.ProductInfo;
  27. import com.adobe.granite.license.ProductInfoService;
  28. import org.apache.commons.lang.StringUtils;
  29. import org.apache.felix.scr.annotations.Activate;
  30. import org.apache.felix.scr.annotations.Component;
  31. import org.apache.felix.scr.annotations.Properties;
  32. import org.apache.felix.scr.annotations.Property;
  33. import org.apache.felix.scr.annotations.Reference;
  34. import org.apache.felix.scr.annotations.Service;
  35. import org.apache.sling.api.SlingHttpServletRequest;
  36. import org.apache.sling.api.SlingHttpServletResponse;
  37. import org.slf4j.Logger;
  38. import org.slf4j.LoggerFactory;
  39. import java.util.ArrayList;
  40. import java.util.HashMap;
  41. import java.util.List;
  42. import java.util.Map;
  43. /**
  44. * ACS AEM Commons - Quickly - DuckDuckGo Docs Operation
  45. */
  46. @Component
  47. @Properties({
  48. @Property(
  49. name = Operation.PROP_CMD,
  50. value = DuckDuckGoDocsOperationImpl.CMD
  51. ),
  52. @Property(
  53. name = Operation.PROP_DESCRIPTION,
  54. value = "Search Docs"
  55. )
  56. })
  57. @Service
  58. public class DuckDuckGoDocsOperationImpl extends AbstractOperation {
  59. public static final String CMD = "docs";
  60. private static final Logger log = LoggerFactory.getLogger(DuckDuckGoDocsOperationImpl.class);
  61. @Reference
  62. private ProductInfoService productInfoService;
  63. private String aemVersion = "6-0";
  64. private String productName = "aem";
  65. @Override
  66. public boolean accepts(final SlingHttpServletRequest request, final Command cmd) {
  67. return StringUtils.endsWithIgnoreCase(CMD, cmd.getOp());
  68. }
  69. @Override
  70. public String getCmd() {
  71. return CMD;
  72. }
  73. @Override
  74. protected List<Result> withoutParams(final SlingHttpServletRequest request,
  75. final SlingHttpServletResponse response,
  76. final Command cmd) {
  77. final List<Result> results = new ArrayList<Result>();
  78. final Result result = new Result.Builder("docs.adobe.com")
  79. .description("http://docs.adobe.com")
  80. .action(new Action.Builder()
  81. .uri("http://docs.adobe.com")
  82. .target(Action.Target.BLANK).build())
  83. .build();
  84. results.add(result);
  85. return results;
  86. }
  87. @Override
  88. protected List<Result> withParams(final SlingHttpServletRequest request,
  89. final SlingHttpServletResponse response,
  90. final Command cmd) {
  91. final List<Result> results = new ArrayList<Result>();
  92. final Map<String, String> params = new HashMap<String, String>();
  93. params.put("q", "site:docs.adobe.com/docs/en/" + productName + "/" + aemVersion + " AND " + cmd.getParam());
  94. final Result result = new Result.Builder("Search AEM documentation")
  95. .description("Search for: " + cmd.getParam())
  96. .action(new Action.Builder()
  97. .uri("https://duckduckgo.com")
  98. .target(Action.Target.BLANK)
  99. .params(params)
  100. .build())
  101. .build();
  102. results.add(result);
  103. return results;
  104. }
  105. @Activate
  106. protected void activate(Map<String, String> config) {
  107. ProductInfo productInfo = null;
  108. for (ProductInfo i : productInfoService.getInfos()) {
  109. String shortName = i.getShortName();
  110. // currently, this is always 'CQ' but let's futureproof a bit
  111. if (shortName.equalsIgnoreCase("cq") || shortName.equalsIgnoreCase("aem")) {
  112. productInfo = i;
  113. break;
  114. }
  115. }
  116. if (productInfo != null) {
  117. // there's a bug in 6.1 GA which causes productInfo.getShortVersion() to return 6.0,
  118. // so let's use this longer form.
  119. aemVersion = String.format("%s-%s", productInfo.getVersion().getMajor(), productInfo.getVersion().getMinor());
  120. }
  121. log.debug("AEM Version: {}", aemVersion);
  122. log.debug("Product Name: {}", productName);
  123. }
  124. }