/modules/mod_atom/resources/resource_atom_entry.erl

http://github.com/zotonic/zotonic · Erlang · 124 lines · 73 code · 29 blank · 22 comment · 0 complexity · ba639720e76f6fa919dedaec64811bcf MD5 · raw file

  1. %% @author Arjan Scherpenisse <arjan@scherpenisse.net>
  2. %% @copyright 2009 Arjan Scherpenisse
  3. %% Date: 2009-10-02
  4. %% @doc Atom entry resource.
  5. %% Copyright 2009 Arjan Scherpenisse
  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. -module(resource_atom_entry).
  19. -author("Arjan Scherpenisse <arjan@scherpenisse.net>").
  20. -export([
  21. init/1,
  22. service_available/2,
  23. is_authorized/2,
  24. allowed_methods/2,
  25. encodings_provided/2,
  26. resource_exists/2,
  27. last_modified/2,
  28. expires/2,
  29. content_types_provided/2,
  30. charsets_provided/2,
  31. provide_content/2
  32. ]).
  33. -include_lib("webmachine_resource.hrl").
  34. -include_lib("include/zotonic.hrl").
  35. %% Let cached versions expire in an hour.
  36. -define(MAX_AGE, 3600).
  37. init(DispatchArgs) ->
  38. {ok, DispatchArgs}.
  39. service_available(ReqData, DispatchArgs) when is_list(DispatchArgs) ->
  40. Context = z_context:new(ReqData, ?MODULE),
  41. Context1 = z_context:set(DispatchArgs, Context),
  42. Context2 = z_context:ensure_qs(Context1),
  43. ?WM_REPLY(true, Context2).
  44. allowed_methods(ReqData, Context) ->
  45. {['HEAD', 'GET'], ReqData, Context}.
  46. charsets_provided(ReqData, Context) ->
  47. {[{"utf-8", fun(X) -> X end}], ReqData, Context}.
  48. encodings_provided(ReqData, Context) ->
  49. {[
  50. {"identity", fun(X) -> X end},
  51. {"gzip", fun(X) -> zlib:gzip(X) end}
  52. ], ReqData, Context}.
  53. content_types_provided(ReqData, Context) ->
  54. {[{"application/atom+xml;type=entry", provide_content},
  55. {"application/atom+xml", provide_content}],
  56. ReqData, Context}.
  57. %% @doc Check if the id in the request (or dispatch conf) exists.
  58. resource_exists(ReqData, Context) ->
  59. Context1 = ?WM_REQ(ReqData, Context),
  60. ContextQs = z_context:ensure_qs(Context1),
  61. try
  62. ?WM_REPLY(m_rsc:exists(get_id(ContextQs), ContextQs), ContextQs)
  63. catch
  64. _:_ -> ?WM_REPLY(false, ContextQs)
  65. end.
  66. %% @doc Check if the current user is allowed to view the resource.
  67. is_authorized(ReqData, Context) ->
  68. Context1 = ?WM_REQ(ReqData, Context),
  69. ContextQs = z_context:ensure_qs(Context1),
  70. z_acl:wm_is_authorized(view, get_id(ContextQs), ContextQs).
  71. last_modified(ReqData, Context) ->
  72. RD1 = wrq:set_resp_header("Cache-Control", "public, max-age="++integer_to_list(?MAX_AGE), ReqData),
  73. Modified = m_rsc:p(get_id(Context), modified, Context),
  74. Context1 = z_context:set(last_modified, Modified, Context),
  75. {Modified, RD1, Context1}.
  76. expires(ReqData, State) ->
  77. NowSecs = calendar:datetime_to_gregorian_seconds(calendar:universal_time()),
  78. {calendar:gregorian_seconds_to_datetime(NowSecs + ?MAX_AGE), ReqData, State}.
  79. provide_content(ReqData, Context) ->
  80. Context1 = z_context:set_reqdata(ReqData, Context),
  81. Id = get_id(Context1),
  82. RscExport = m_rsc_export:full(Id, Context1),
  83. Content = atom_convert:resource_to_atom(RscExport),
  84. ?WM_REPLY(Content, Context1).
  85. %% @doc Fetch the id from the request or the dispatch configuration.
  86. %% @spec get_id(Context) -> int() | false
  87. get_id(Context) ->
  88. ReqId = case z_context:get(id, Context) of
  89. undefined -> z_context:get_q("id", Context);
  90. ConfId -> ConfId
  91. end,
  92. case m_rsc:name_to_id(ReqId, Context) of
  93. {ok, RscId} -> RscId;
  94. _ -> false
  95. end.